JTextPane - 适合其内容的大小

时间:2023-01-27 20:10:10

What I have - JTextPane  - 适合其内容的大小

是)我有的 -

What I need - JTextPane  - 适合其内容的大小

我需要的 -

Code:

JFrame frame = new JFrame();
JPanel panel = new JPanel();
JTextPane pane = new JTextPane();
pane.setText("Long string Long string Long string Long string Long string Long string");
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
panel.add(pane);
panel.add(new JButton("Button"));
frame.add(panel);
frame.show();

If I do frame.pack(), I get very long window without word wrap!

如果我做frame.pack(),我得到很长的窗口没有自动换行!

1 个解决方案

#1


For any HTML aware Swing component, width can be set for the body using HTML styles (CSS). This in turn will determine the number of lines to render and, from that, the preferred height of the text component. Setting the width in CSS avoids the need to compute where line breaks should occur in (or the best size of) the component.

对于任何支持HTML的Swing组件,可以使用HTML样式(CSS)为主体设置宽度。这反过来将决定要渲染的行数,并从中确定文本组件的首选高度。在CSS中设置宽度可以避免计算组件中的换行符(或最佳大小)的位置。

More generally, see How to Use HTML in Swing Components.

更一般地,请参阅如何在Swing组件中使用HTML。

E.G.

import javax.swing.*;

public class FixedWidthLabel {

    public static void main(String[] srgs) {
        final String s = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean eu nulla urna. Donec sit amet risus nisl, a porta enim. Quisque luctus, ligula eu scelerisque gravida, tellus quam vestibulum urna, ut aliquet sapien purus sed erat. Pellentesque consequat vehicula magna, eu aliquam magna interdum porttitor. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Sed sollicitudin sapien non leo tempus lobortis. Morbi semper auctor ipsum, a semper quam elementum a. Aliquam eget sem metus.";
        final String html1 = "<html><body style='width: ";
        final String html2 = "px'>";

        Runnable r = new Runnable() {

            @Override
            public void run() {
                JOptionPane.showMessageDialog(
                        null, new JLabel(html1 + "200" + html2 + s));
                JOptionPane.showMessageDialog(
                        null, new JLabel(html1 + "300" + html2 + s));
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

JTextPane  - 适合其内容的大小JTextPane  - 适合其内容的大小

#1


For any HTML aware Swing component, width can be set for the body using HTML styles (CSS). This in turn will determine the number of lines to render and, from that, the preferred height of the text component. Setting the width in CSS avoids the need to compute where line breaks should occur in (or the best size of) the component.

对于任何支持HTML的Swing组件,可以使用HTML样式(CSS)为主体设置宽度。这反过来将决定要渲染的行数,并从中确定文本组件的首选高度。在CSS中设置宽度可以避免计算组件中的换行符(或最佳大小)的位置。

More generally, see How to Use HTML in Swing Components.

更一般地,请参阅如何在Swing组件中使用HTML。

E.G.

import javax.swing.*;

public class FixedWidthLabel {

    public static void main(String[] srgs) {
        final String s = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean eu nulla urna. Donec sit amet risus nisl, a porta enim. Quisque luctus, ligula eu scelerisque gravida, tellus quam vestibulum urna, ut aliquet sapien purus sed erat. Pellentesque consequat vehicula magna, eu aliquam magna interdum porttitor. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Sed sollicitudin sapien non leo tempus lobortis. Morbi semper auctor ipsum, a semper quam elementum a. Aliquam eget sem metus.";
        final String html1 = "<html><body style='width: ";
        final String html2 = "px'>";

        Runnable r = new Runnable() {

            @Override
            public void run() {
                JOptionPane.showMessageDialog(
                        null, new JLabel(html1 + "200" + html2 + s));
                JOptionPane.showMessageDialog(
                        null, new JLabel(html1 + "300" + html2 + s));
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

JTextPane  - 适合其内容的大小JTextPane  - 适合其内容的大小