2012-02-20 131 views
0

我正在渲染一些在JEditorPane中分层的图像。我已经读过JEditorPane是最好的,但我希望这是一个问题,或者我的HTML代码或其他东西。这里是我的内容看起来如何在浏览器中:JEditorPane:HTML布局损坏

enter image description here

而且它的外观在JScrollBar中(JEditorPane中):

enter image description here

的HTML代码:http://pastebin.com/EixG3WLH

的Java代码:

File f = new File("index.html"); 
JEditorPane jep = new JEditorPane(f.toURI().toURL()); 
JScrollPane sp = new JScrollPane(jep); 

JFrame frame = new JFrame(); 
frame.add(sp); 
jep.setEditable(false); 

frame.setVisible(true); 
frame.setSize(500, 500); 
frame.setTitle(wpj.getParse().getTitle()); 

如果这个问题可以在JEditorPane中解决,我真的宁愿不使用FlyingSaucer!

回答

2

你可以做到这一点......但它并不是很简单......因为JEditorPane没有CSS绝对定位......所以,你必须首先确认某个元素是否有位置:absolute或position :固定属性扩展的ViewFactory,是这样的:

public class ExtendedHTMLEditorKit extends HTMLEditorKit{ 
//.... other code here 
    public class MyHTMLFactory extends HTMLFactory{ 
     //other code here 
     @Override 
     public View create(Element elem) { 
      if (isLayered(elem)){ //it means, it has position attribute 
      return new PositionedView(elem); 
      } 
      else 
      return super.create(elem); 
     } 

     boolean isLayered(Element elem){ 
      SimpleAttributeSet sas = new SimpleAttributeSet(elem); 
      StyleSheet styles = (HTMLDocument elem.getDocument).getStyleSheet(); 
      Tag tag = element.getAttributes().getAttribute(AttributeSet.NameAttribute); 
      sas.addAttributes(styleSheet.getRule(tag, element)); 
      return sas.isDefined("position") 
      && !sas.getAttribute("position").toString().equalsIgnorecase("static"); 
     } 
    } 
} 

在这种情况下,我们需要再建立一个正确的观点你的元素...我不知道,如果你只是定位图像(在此例如,它可能很简单)或很多东西...我可以看到你的代码,你正在使用div ...

让我来解释一下我做什么:我已经创建了一个组件查看并返回一个新的JEditorPane作为组件,在这里我放置原始元素的innerCode ...之后,将它移动到父编辑器的正确位置上...

要同步这一点真的很难编辑,但如果你只whant使用它们来显示,就必须更加简单...

确定..认为必须像:

public class PositionedView extends ComponentView{ 
    private JEditorPane view; 
    private JEditorPane parent; 

    @Override 
    public Component createComponent(){ 
     if (view == null){ 
     view = new JEditorPane(); 
     } 
     String innerText = dumpInnerText(getElement()); 
     view.setText(innerText); 
     view.setLocation(getAbsoluteX(), getAbsoluteY()); 
     parent.add(view); 
    } 

    @Override 
    public void setParent(View parent) { 
     if (parent != null) { 

     java.awt.Container host = parent.getContainer(); 
     if (host != null && host instanceof JEditorPane) { 
      parent = (JEditorPane) host; 
     } 
     } 

    super.setParent(parent); 
    } 

    protected int getAbsoluteX() { 
    //search for the attribute left or right and calculate the position over parent 
    } 

    protected int getAbsoluteY(){ 
    //search for the attribute top or bottom and calculate the position over parent 
    } 

    protected String dumpInnerText(Element element){ 
    //there are several ways to do it, I used my own reader/writer, 
    //because I've need add special tags support... 
    } 
} 

我希望这有助于你......啊!还有一件事:如果你这样做,你必须确保你的视图不是不透明的,这意味着所有的视图元素,在另一种情况下,你的元素将有一个空白的矩形。

另一件事,你可能需要检查视图的正确维度...做为getAbsoluteX/getAbsoluteY来获得宽度/高度属性。

+0

感谢您的明确答复。 – krslynx 2012-04-26 18:37:25

+1

p.s.我通过使用名为FlyingSaucer的第三方库解决了这个问题。 – krslynx 2012-04-26 18:38:19

2

JEditorPane对CSS绝对定位不太好。我认为您正试图通过JEditorPane实现更多的功能。

+0

是的,我想到了很多。干杯! – krslynx 2012-02-20 13:21:59

+0

*“JEditorPane在CSS绝对定位方面不太好。”*我认为你已经明确了它。 +1 – 2012-02-20 14:16:22