2012-02-20 130 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