如何创建具有指定宽度和显示所有文本所需的最小可能高度的JTextArea? [英] How can I create a JTextArea with a specified width and the smallest possible height required to display all the text?

查看:197
本文介绍了如何创建具有指定宽度和显示所有文本所需的最小可能高度的JTextArea?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我能找到的所有使用 JTextArea 的例子中,高度&在构造 JTextArea 之前已知宽度,如果 JTextArea 需要更多高度,则将其放入 JScrollPane的。显然, 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.

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

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.

作为旁注, JTextArea 将被添加到 JPanel 没有布局管理器 - 它根据添加的组件的首选大小使用绝对定位。这要求我的 JTextArea 将在 getPreferredSize()上返回正确的尺寸。正确的尺寸应该是我构造时提供的宽度,以及显示所有宽度所需文本所需的最小高度。

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.

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

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!

推荐答案


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

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

听起来像布局管理员的工作。

Sounds like the job of a layout manager.


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

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() );

这篇关于如何创建具有指定宽度和显示所有文本所需的最小可能高度的JTextArea?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆