如何在 HTML 内容类型的 JTextPane 上设置固定宽度但动态高度?

How to set fixed width but dynamic height on JTextPane with HTML content type?

我有一个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,因此可以通过 swing 轻松计算出首选高度。在控制台中,我可以看到 200、411(我将宽度固定为首选高度)。在对话框中,我们看到窗格的大小合适:

问题是我想要 HTML 内容。所以在代码中我取​​消注释行 field.setContentType("text/html");

我运行我的程序,我在控制台中有:5393、23:宽度不再强制为200。

这里是对话结果:

如何解决 HTML 内容的问题?

我认为你应该再读一遍 layout managers

您将 JFrame 的内容窗格的布局管理器设置为 FlowLayout,然后使用 BorderLayout 添加 JEditorPane。我不知道那实际上做了什么,但我认为这不是你想要的。

FlowLayout 尊重它包含的组件的首选大小,因此我设置了 JEditorPanepreferred 大小,然后我创建了一个 JPanel 并将 JEditorPane 添加到其中,然后将 JPanel 添加到 JFrame.

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>";
    }
}

这是它的样子。