【问题标题】:JTextPane HTML renderer wrong?JTextPane HTML 渲染器错误?
【发布时间】:2014-01-16 13:29:34
【问题描述】:

我有一个 JTextPane 并想在其中创建一个 div,其高度与 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();
}
}

每当 JTextPane 调整大小时,我都会获取 JTextPane 的高度并创建一个具有该高度的 div。

问题是,div 有时大于高度,有时小于高度。您可以通过更改框架的高度来尝试此操作。我认为 div 应该与 JTextPane 同等缩放,但它不...

注意:JTextPane 的高度计算正确(用 Gimp 仔细检查)。

有什么想法吗?

【问题讨论】:

    标签: java html swing jtextpane


    【解决方案1】:

    有什么想法吗?

    放弃追求DIV 的想法,而是将样式设置为BODY 元素。正文将填满可用空间,除非内容太长而无法容纳,否则会溢出。

    【讨论】:

    • 我安德鲁,我想要实现的是让文本在 JTextPane 的底部对齐。我的想法是在 div 中使用一个表格并说 valign=bottom。因此,我需要一个具有 JTextPane 高度的周围 div。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-27
    • 1970-01-01
    • 2019-10-05
    • 1970-01-01
    • 2017-04-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多