Java 如何设置 JTextArea 大小?

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

How to set JTextArea size?

javaswingjtextareajoptionpane

提问by Borat Sagddiev

I want to set a fixed size for JtextAreawithin JOptionPane

我想设置为一个固定大小的JtextAreaJOptionPane

public static void main(String[] args) {

        JTextArea headersTxt = new JTextArea();
        for (int i = 0 ; i < 50 ; i ++ ) {
            headersTxt.append("test \n") ;
        }
        JScrollPane scroll = new JScrollPane(headersTxt); 
        scroll.setSize (300,600) ;  // this line silently ignored
        int test = JOptionPane.showConfirmDialog(null,  scroll,"test",  JOptionPane.OK_CANCEL_OPTION) ;

    }

However, the above code ignores scroll.setSize (300,600) ;

但是,上面的代码忽略了 scroll.setSize (300,600) ;

It works fine but the size is not fixed . What is the problem with scroll.setSize (300,600) ;?

它工作正常,但大小不固定。有什么问题scroll.setSize (300,600) ;

采纳答案by MadProgrammer

Because each system can render fonts differently, you should avoid using pixel measurements where possible

因为每个系统可以以不同的方式呈现字体,所以您应该尽可能避免使用像素测量

Instead, provide the rows and columns you want to display

相反,提供要显示的行和列

JTextArea ta = new JTextArea(5, 20);