java中文本区域的大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18767367/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 10:52:31  来源:igfitidea点击:

Size of Text Area in java

javaswingjtextareajcomponentpreferredsize

提问by seal

I am writing a code for basic GUI. There i need a Text Area. But i can not make the Text Area in my desirable size. i use setPreferredSizemethod to set the dimension of the Text Area. But it did not work. I also tried setSizemethod but did not work also. Here is my written code.

我正在为基本的 GUI 编写代码。我需要一个文本区域。但是我无法按照我想要的大小制作文本区域。我使用setPreferredSize方法来设置文本区域的尺寸。但它没有用。我也尝试过setSize方法,但也没有奏效。这是我写的代码。

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

采纳答案by Ben Dale

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

回答by Enigma

PreferredSize is what it say what it is: a preferred size. The border layout determines the actual size (taking the preferred size into considerations).

PreferredSize 就是它所说的:首选大小。边框布局决定了实际尺寸(考虑首选尺寸)。

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. E.G. flowLayout: http://docs.oracle.com/javase/tutorial/uiswing/layout/flow.html

考虑其他布局以获得所需的尺寸。EG flowLayout:http://docs.oracle.com/javase/tutorial/uiswing/layout/flow.html