如何使用html内容类型在jtextpane上设置固定宽度但动态高度?

jgzswidk  于 2021-07-08  发布在  Java
关注(0)|答案(1)|浏览(476)

我有一个 JEditorPane . 我希望窗格高度与内容相对应。我已经这样做了,当窗格内容是文本时,它可以工作:

public class JEditorPaneTest extends JFrame {

    public static void main(String[] args) {
        final JFrame mainFrame = new JFrame("test");
        mainFrame.getContentPane().setLayout(new FlowLayout());
        mainFrame.setSize(500,500);

        final JEditorPane field = new JEditorPane();
        //field.setContentType("text/html");
        field.setText(htmlize("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse accumsan magna vel libero bibendum, quis hendrerit nisi rutrum. Cras placerat erat eget dictum ornare. Sed eget nisl quis nibh vehicula mollis. Vestibulum non iaculis erat, quis pulvinar magna. Suspendisse ac rhoncus purus. Quisque finibus, dolor varius tincidunt aliquet, mauris felis condimentum neque, at viverra felis nulla at justo. Duis ut dui velit. Integer vitae mollis leo. Cras quis urna odio. Suspendisse tempus, urna sed maximus fringilla, ante velit finibus massa, id commodo libero quam non ipsum. Sed id augue vitae sapien sagittis imperdiet in eget nibh. Nam semper posuere nisl, dictum efficitur ipsum aliquet ac. Phasellus eros massa, fringilla et neque maximus, pretium tempor magna."));

        field.setSize(200,40);
        field.setSize(200, field.getPreferredSize().height);

        mainFrame.getContentPane().add(field,BorderLayout.CENTER);
        mainFrame.setVisible(true);
        System.out.println(field.getPreferredSize().width+" , "+field.getPreferredSize().height);
    }

    public static String htmlize(String pMessage) {
        return "<html><body>" + pMessage + "</body></html>";
    }
}

根据文本内容知道首选大小。宽度固定为200,因此可以通过摆动轻松计算出首选高度。在控制台中,我可以看到200411(宽度是我固定的首选高度)。在对话框中,我们看到窗格具有良好的大小:

问题是我想要一个html内容。所以在代码中我取消了对行的注解 field.setContentType("text/html"); 我运行我的程序,我在控制台:5393,23:宽度不再被迫200了。
对话框结果如下:

如何解决html内容的问题?

i7uq4tfw

i7uq4tfw1#

我认为你应该再读一遍关于布局管理器的内容。
可以为的内容窗格设置布局管理器 JFrameFlowLayout 然后你加上 JEditorPane 使用 BorderLayout . 我不知道这到底是怎么回事,但我不认为这是你想要的。 FlowLayout 尊重它包含的组件的首选大小,因此我设置 JEditorPane 然后我创造了一个 JPanel 并添加了 JEditorPane 再加上 JPanelJFrame .

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class JEditorPaneTest extends JFrame {

    public static void main(String[] args) {
        final JFrame mainFrame = new JFrame("test");
        mainFrame.setSize(500,500);

        final JEditorPane field = new JEditorPane();
        JPanel panel = new JPanel();
        field.setContentType("text/html");
        field.setText(htmlize("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse accumsan magna vel libero bibendum, quis hendrerit nisi rutrum. Cras placerat erat eget dictum ornare. Sed eget nisl quis nibh vehicula mollis. Vestibulum non iaculis erat, quis pulvinar magna. Suspendisse ac rhoncus purus. Quisque finibus, dolor varius tincidunt aliquet, mauris felis condimentum neque, at viverra felis nulla at justo. Duis ut dui velit. Integer vitae mollis leo. Cras quis urna odio. Suspendisse tempus, urna sed maximus fringilla, ante velit finibus massa, id commodo libero quam non ipsum. Sed id augue vitae sapien sagittis imperdiet in eget nibh. Nam semper posuere nisl, dictum efficitur ipsum aliquet ac. Phasellus eros massa, fringilla et neque maximus, pretium tempor magna."));

        field.setSize(200,40);
        field.setPreferredSize(new Dimension(200, field.getPreferredSize().height));
        panel.add(field);

        mainFrame.getContentPane().add(panel,BorderLayout.CENTER);
        mainFrame.setVisible(true);
        System.out.println(field.getPreferredSize().width+" , "+field.getPreferredSize().height);
    }

    public static String htmlize(String pMessage) {
        return "<html><body>" + pMessage + "</body></html>";
    }
}

这就是它的样子。

相关问题