如何创建一个具有指定宽度和显示所有文本所需的最小可能高度的 JTextArea?

发布于 2024-09-30 20:11:57 字数 795 浏览 0 评论 0原文

在我能找到的所有使用 JTextArea 的示例中,高度和高度都为 JTextArea。宽度在构造 JTextArea 之前就已知,如果 JTextArea 需要更多高度,则将其放入 JScrollPane 内。显然,JTextArea 的高度取决于宽度和文本内容。

现在,我的情况要求我不使用 JScrollPane,而是使用 JTextArea 足够高以显示所有文本。当我创建 JTextArea 时,我知道文本内容以及它必须使用的宽度;我不知道高度 - 我希望它尽可能小而不切断任何文本。这似乎很难完成。

附带说明一下,JTextArea 将被添加到没有布局管理器的 JPanel 中 - 它根据添加的组件的首选大小使用绝对定位。这要求我的 JTextAreagetPreferredSize() 上返回正确的尺寸。正确的尺寸应该是我构建它时提供的宽度,以及显示具有提供的宽度的所有文本所需的最小高度。

我发现了一些类似的线程,讨论与 JTextArea 相关的奇怪/错误,有时可以通过在父容器上调用两次 pack() 来解决。这对我来说不是一个选择。我很想基本上创建自己的 JTextArea ,它需要宽度和字符串,并根据宽度和字体设置计算必要的最小高度,但我想在花时间之前我会先询问一下这样做。

希望我的问题很清楚。谢谢大家的帮助!

In all the examples that I can find that use a JTextArea, the height & width is known before constructing the JTextArea, and if the JTextArea would require more height, then it is put inside of a JScrollPane. Obviously, the height of JTextArea is dependent on the width and the text contents.

Now, my situation requires that I do not use a JScrollPane, but instead that the JTextArea be just tall enough to display all the text. When I create the JTextArea, I know the text contents and how much width it will have to work with; I don't know the height - I want that to be as small as possible without cutting off any of the text. This seems very difficult to accomplish.

As a side note, the JTextArea will be added to a JPanel that does not have a layout manager - it uses absolute positioning based on the added component's preferred size. This requires that my JTextArea would return the correct dimensions on getPreferredSize(). The correct dimensions should be the width that I provided when I constructed it, and the minimum height that is required to display all the text with the provided width.

I've found some similar threads that discuss the oddities/bugs involved with the JTextArea that are sometimes solved by calling pack() twice on the parent container. This is not an option for me. I'm tempted to basically create my own JTextArea that takes a width and String and computes the necessary minimum height based on the width and font settings, but I figured I would ask around first before spending the time to do that.

Hopefully my question is clear. Thank you all for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

捂风挽笑 2024-10-07 20:11:57

它根据添加组件的首选尺寸使用绝对定位。

听起来像是布局经理的工作。

这要求我的 JTextArea 在 getPreferredSize() 上返回正确的尺寸。

JTextArea textArea = new JTextArea();
textArea.setLineWrap( true );
textArea.setWrapStyleWord( true );
textArea.setText("one two three four five six seven eight nine ten");
System.out.println("000: " + textArea.getPreferredSize());
textArea.setSize(100, 1);
System.out.println("100: " + textArea.getPreferredSize());
textArea.setSize( textArea.getPreferredSize() );

it uses absolute positioning based on the added component's preferred size.

Sounds like the job of a layout manager.

This requires that my JTextArea would return the correct dimensions on getPreferredSize().

JTextArea textArea = new JTextArea();
textArea.setLineWrap( true );
textArea.setWrapStyleWord( true );
textArea.setText("one two three four five six seven eight nine ten");
System.out.println("000: " + textArea.getPreferredSize());
textArea.setSize(100, 1);
System.out.println("100: " + textArea.getPreferredSize());
textArea.setSize( textArea.getPreferredSize() );
许一世地老天荒 2024-10-07 20:11:57
import java.awt.*;
import javax.swing.*;

class FixedWidthLabel {

    public static void main(String[] args) {

        Runnable r = new Runnable() {
            public void run() {
                String pt1 = "<html><body width='";
                String pt2 =
                    "px'><h1>Label Height</h1>" +
                    "<p>Many Swing components support HTML 3.2 &" +
                    " (simple) CSS.  By setting a body width we can cause the " +
                    " component to find the natural height needed to display" +
                    " the component.<br><br>" +
                    "<p>The body width in this text is set to " +
                    "";
                String pt3 =
                    " pixels." +
                    "";

                JPanel p = new JPanel( new BorderLayout() );

                JLabel l1 = new JLabel( pt1 + "125" + pt2 + "125" + pt3 );
                p.add(l1, BorderLayout.WEST);

                JLabel l2 = new JLabel( pt1 + "200" + pt2 + "200" + pt3 );
                p.add(l2, BorderLayout.CENTER);

                JOptionPane.showMessageDialog(null, p);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}
import java.awt.*;
import javax.swing.*;

class FixedWidthLabel {

    public static void main(String[] args) {

        Runnable r = new Runnable() {
            public void run() {
                String pt1 = "<html><body width='";
                String pt2 =
                    "px'><h1>Label Height</h1>" +
                    "<p>Many Swing components support HTML 3.2 &" +
                    " (simple) CSS.  By setting a body width we can cause the " +
                    " component to find the natural height needed to display" +
                    " the component.<br><br>" +
                    "<p>The body width in this text is set to " +
                    "";
                String pt3 =
                    " pixels." +
                    "";

                JPanel p = new JPanel( new BorderLayout() );

                JLabel l1 = new JLabel( pt1 + "125" + pt2 + "125" + pt3 );
                p.add(l1, BorderLayout.WEST);

                JLabel l2 = new JLabel( pt1 + "200" + pt2 + "200" + pt3 );
                p.add(l2, BorderLayout.CENTER);

                JOptionPane.showMessageDialog(null, p);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}
冷默言语 2024-10-07 20:11:57

FixWidthLabel 中描述的解决方案,使用
将要求程序员提供消息作为 html 字符串的一部分。

如果消息类似于 invalid integer: i<0 not allowed
那么 < 必须被转义(编码?),否则无法得知 JLabel 将如何解释 html。

这增加了该解决方案的复杂性。

只有当您知道该消息不包含任何此类字符时,您才可以。

The solution described in FixedWidthLabel , using <html><body width="..."
will require the programmer to provide the message as part of the html string.

If the message is something like invalid integer: i<0 not allowed ,
then the < will have to be escaped (encoded?), otherwise there is no telling how JLabel will interpret the html.

This adds complexity to this solution.

Only if you know that the message doesn't contain any such characters, you will be allright.

漫雪独思 2024-10-07 20:11:57

好吧,如果您知道宽度,您可以运行一些测试并计算出文本的每个字符的宽度,这样您就可以使用循环来确定每行适合多少个字符并总计要显示的字符,然后您可以根据有多少行来设置高度。

假设您的文本有 1000 个字符(包括空格),并且一个字符的宽度相当于 4 个像素,那么您可以算出,如果宽度为 400,则每行可以容纳 100 个字符,那么您将需要 10 行。现在假设字体大小的高度为 10,您现在知道您需要 10 x 10 == 100 像素,因此您的 TextArea 应该为 400x100

Well perhaps if you know your width you could run some tests and work out how wide each character of text is, that way you could use a loop to determine how many characters fit on each line and total the characters that are to be shown, then you could set the height based on how many lines there are to be.

Say your text has 1000 characters including blank spaces, and the width of a character is equivalent to 4pixels, then you can work out if the width is 400 that 100 characters fit on each line, subsequently you will need 10 lines. Now say the height is 10 for the font size, you now know you need 10 x 10 == 100 pixels, so your TextArea should be 400x100

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文