简体   繁体   中英

How to set JTextArea size?

I want to set a fixed size for JtextArea within JOptionPane

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM