简体   繁体   English

JTextPane HTML渲染器错误?

[英]JTextPane HTML renderer wrong?

I have a JTextPane and want to create a div within it, with exactly the height of the JTextPane. 我有一个JTextPane并想在其中创建一个div,它的高度与JTextPane的高度相同。 The div should be always the height of the JTextPane. div应该始终是JTextPane的高度。

public class Test {

JTextPane edit=new JTextPane();
public Test() {
    JFrame frame=new JFrame("Center text vertically");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(edit);
    String text = "<html><div style=\"height: 0px; border: 1px solid black;\">here is some text</div></html>";
    edit.setEditorKit(new HTMLEditorKit());
    edit.setContentType("text/html");
    edit.setText(text);

    edit.addComponentListener(new ComponentAdapter() {
        @Override
        public void componentResized(ComponentEvent e) {
            int height = edit.getHeight();
            height -= 50; //this makes the problem more clear
            String text = "<html><div style=\"height: " + height + "px; border: 1px solid black;\">here is some text</div></html>";
            edit.setText(text);
        }
    });
    frame.setSize(300,200);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}
public static void main(String[] args) throws Exception {
    new Test();
}
}

Whenever the JTextPane resizes, I take the height of the JTextPane and create a div with exactly this height. 每当调整JTextPane的大小时,我都会采用JTextPane的高度并使用该高度创建一个div。

The problem is, that the div is sometimes larger than the height and sometimes smaller than the height. 问题是div有时大于高度,有时小于高度。 You can try this by changing the height of the frame. 您可以通过更改框架的高度来尝试此操作。 I thought that the div should equally scale with the JTextPane, however it does not... 我认为div应该与JTextPane相等地缩放,但是它不...

Note: The height of the JTextPane is calculated correctly (double checked with Gimp). 注意:JTextPane的高度计算正确(使用Gimp再次检查)。

Any ideas? 有任何想法吗?

Any ideas? 有任何想法吗?

Abandon pursuing the DIV idea, and instead set the style to the BODY element. 放弃追求DIV想法,而是将样式设置为BODY元素。 The body will fill the available space unless the content is too long to fit, then it will overflow. 除非内容太长而无法容纳,否则主体将填充可用空间,然后它将溢出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM