Java 如何创建具有指定宽度和显示所有文本所需的最小高度的 JTextArea?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4083322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I create a JTextArea with a specified width and the smallest possible height required to display all the text?
提问by Nate W.
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.
在我能找到的所有使用 a 的示例中JTextArea
,高度和宽度在构造 之前是已知的JTextArea
,如果JTextArea
需要更多高度,则将其放在 a 内JScrollPane
。显然,的高度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.
现在,我的情况要求我不使用 a JScrollPane
,而是使用 a ,而是JTextArea
足够高以显示所有文本。当我创建 时JTextArea
,我知道文本内容以及它必须使用的宽度;我不知道高度 - 我希望在不切断任何文本的情况下尽可能小。这似乎很难实现。
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
将被添加到JPanel
没有布局管理器的一个 - 它使用基于添加组件的首选大小的绝对定位。这要求 myJTextArea
将返回正确的尺寸getPreferredSize()
。正确的尺寸应该是我在构建它时提供的宽度,以及以提供的宽度显示所有文本所需的最小高度。
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.
我发现了一些类似的线程,它们讨论了JTextArea
有时通过pack()
在父容器上调用两次来解决的奇怪之处/错误。这对我来说不是一个选择。我很想基本上创建自己JTextArea
的宽度和字符串,并根据宽度和字体设置计算必要的最小高度,但我想在花时间这样做之前我会先询问一下。
Hopefully my question is clear. Thank you all for your help!
希望我的问题很清楚。谢谢大家的帮助!
采纳答案by camickr
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 将在 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() );
回答by DaneAU
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
假设你的文本有1000个字符,包括空格,一个字符的宽度相当于4个像素,那么你可以计算出如果宽度为400,每行100个字符,那么你需要10行。现在说字体大小的高度是 10,你现在知道你需要 10 x 10 == 100 像素,所以你的 TextArea 应该是 400x100
回答by Andrew Thompson
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);
}
}
回答by pvbemmelen62
The solution described in FixedWidthLabel , using <html><body width="..."
will require the programmer to provide the message as part of the html string.
FixedWidthLabel 中描述的解决方案,使用<html><body width="..."
将要求程序员提供消息作为 html 字符串的一部分。
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.
如果消息类似于invalid integer: i<0 not allowed
,则<
必须对其进行转义(编码?),否则不知道 JLabel 将如何解释 html。
This adds complexity to this solution.
这增加了该解决方案的复杂性。
Only if you know that the message doesn't contain any such characters, you will be allright.
只有当您知道该消息不包含任何此类字符时,您才会没事。