2

除了乘以行和列之外,Java 中还有什么方法可以在更改其文本后获得 JTextArea 的首选大小?

这是一个例子:

JTextArea a = new JTextArea("sooooooooooooooooooooooooooooooooooo long line"+
        "\nfoo\nboo\nbar\nmoo\nso\nlong\ntext");
Dimension p1 = a.getPreferredSize();
Dimension s1 = a.getSize();

a.append("some\nmore\ntext");
//a.recomputeSize(); I need something like this
Dimension p2 = a.getPreferredSize();
Dimension s2 = a.getSize();

System.out.println(p1); //[width=302,height=128]
System.out.println(s1); //[width=0,height=0]
System.out.println(p2); //[width=302,height=128]
System.out.println(s2); //[width=0,height=0]
4

0 回答 0