Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JEditorPane:HTML布局已损坏_Java_Swing_Jeditorpane - Fatal编程技术网

Java JEditorPane:HTML布局已损坏

Java JEditorPane:HTML布局已损坏,java,swing,jeditorpane,Java,Swing,Jeditorpane,我正在渲染一些分层在窗格中的图像。我已经读到JEditorPane充其量只是一个很不稳定的东西,但是我希望这是我的HTML代码或其他东西的问题。以下是我的内容在浏览器中的外观: 以及它在JScrollBar(JEditorPane)中的外观: HTML代码: Java代码: File f = new File("index.html"); JEditorPane jep = new JEditorPane(f.toURI().toURL()); JScrollPane sp = new JS

我正在渲染一些分层在窗格中的图像。我已经读到JEditorPane充其量只是一个很不稳定的东西,但是我希望这是我的HTML代码或其他东西的问题。以下是我的内容在浏览器中的外观:

以及它在JScrollBar(JEditorPane)中的外观:

HTML代码:

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());

如果这个问题可以在绝地武士窗格中解决,我真的不想使用FlyingSaucer

JEditorPane不太适合CSS绝对定位。我认为您正试图通过JEditorPane获得比它所能提供的更多的东西。

JEditorPane在CSS绝对定位方面做得不太好。我认为你正试图通过JEditorPane获得比它所能提供的更多的东西。

你可以做到。。。但这并不简单。。。因为JEditorPane没有CSS绝对定位。。。因此,您必须首先识别某个元素是否具有扩展ViewFactory的position:absolute或position:fixed属性,例如:

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

让我大致解释一下我的工作:我创建了一个ComponentView,并作为一个组件返回了一个新的JEditorPane,在那里我放置了原始元素的内部代码。。。然后,将其移动到父编辑器的正确位置

要同步,允许编辑确实很困难,但如果您只想使用它们来显示,它必须更简单

好的。。视图必须类似于:

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...
    }
}
我希望这能帮助你。。。啊!!还有一件事:如果您这样做,您必须确保您的视图不是不透明的,这意味着,所有视图元素,在另一种情况下,您的元素将有一个空白的rect


另外,您可能需要检查视图的正确尺寸。。。按getAbsoluteX/getAbsoluteY获得宽度/高度属性。

您可以这样做。。。但这并不简单。。。因为JEditorPane没有CSS绝对定位。。。因此,您必须首先识别某个元素是否具有扩展ViewFactory的position:absolute或position:fixed属性,例如:

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

让我大致解释一下我的工作:我创建了一个ComponentView,并作为一个组件返回了一个新的JEditorPane,在那里我放置了原始元素的内部代码。。。然后,将其移动到父编辑器的正确位置

要同步,允许编辑确实很困难,但如果您只想使用它们来显示,它必须更简单

好的。。视图必须类似于:

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...
    }
}
我希望这能帮助你。。。啊!!还有一件事:如果您这样做,您必须确保您的视图不是不透明的,这意味着,所有视图元素,在另一种情况下,您的元素将有一个空白的rect


另外,您可能需要检查视图的正确尺寸。。。按照getAbsoluteX/getAbsoluteY获得宽度/高度属性。

是的,我也这么认为。干杯“JEditorPane不太擅长CSS绝对定位。”我想你已经搞定了+1是的,我也这么想。干杯“JEditorPane不太擅长CSS绝对定位。”我想你已经搞定了+1p.s。我通过使用第三方库FlyingSaucer.p.s解决了这个问题。我通过使用第三方库FlyingSaucer解决了这个问题。