如何设置JTextArea大小?

Bor*_*iev 6 java swing joptionpane jtextarea

我想设置一个固定大小的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) ;

    }
Run Code Online (Sandbox Code Playgroud)

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

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

Mad*_*mer 11

由于每个系统都可以以不同方式渲染字体,因此应尽可能避免使用像素测量

而是提供要显示的行和列

JTextArea ta = new JTextArea(5, 20);
Run Code Online (Sandbox Code Playgroud)