[英]Size of Text Area in java
I am writing a code for basic GUI. 我正在为基本GUI编写代码。 There i need a Text Area.
那里我需要一个文本区域。 But i can not make the Text Area in my desirable size.
但是我无法使文本区域达到我想要的大小。 i use
setPreferredSize
method to set the dimension of the Text Area. 我使用
setPreferredSize
方法设置文本区域的尺寸。 But it did not work. 但这没有用。 I also tried
setSize
method but did not work also. 我也尝试了
setSize
方法,但是也没有用。 Here is my written code. 这是我的书面代码。
private void textArea() {
setTitle("TextArea");
setSize(700, 500);
setLayout(new BorderLayout());
JTextArea textArea = new JTextArea();
textArea.setPreferredSize(new Dimension(100,100));
System.out.println(textArea.getSize());
textArea.setBackground(Color.GREEN);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(false);
add(textArea,BorderLayout.CENTER);
}
setPreferredSize won't always work, plus, it's strongly advised that you use the built in layout managers to deal with any sizing issues. setPreferredSize并不总是有效,此外,强烈建议您使用内置的布局管理器来处理任何大小调整问题。
Try and set the columns and rows on the text area: 尝试在文本区域中设置列和行:
new JTextArea(5, 10);
PreferredSize is what it say what it is: a preferred size. PreferredSize是它所说的:首选大小。 The border layout determines the actual size (taking the preferred size into considerations).
边框布局确定实际大小(考虑首选大小)。
See: http://docs.oracle.com/javase/tutorial/uiswing/layout/border.html 参见: http : //docs.oracle.com/javase/tutorial/uiswing/layout/border.html
Consider other layouts to get your desired size. 考虑其他布局以获得所需的尺寸。 EG flowLayout: http://docs.oracle.com/javase/tutorial/uiswing/layout/flow.html
EG flowLayout: http ://docs.oracle.com/javase/tutorial/uiswing/layout/flow.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.