JTextArea
かなり特殊な副作用があり、適切な条件では自然に成長します。シンプルな 2 行のテキスト エディター (1 行あたりの文字数制限、最大 2 行) をセットアップしようとしたときに、偶然これに出くわしました...
基本的に、適切なレイアウト マネージャーがあれば、このコンポーネントは自然に成長します。これは理にかなっていますが、私は驚きました...


さらに、興味がある場合は、コンポーネントのサイズが変更されたときに監視するために aComponentListener
を使用することもできます...
public class TestTextArea extends JFrame {
public TestTextArea() {
setLayout(new GridBagLayout());
JTextArea textArea = new JTextArea();
textArea.setColumns(10);
textArea.setRows(1);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
add(textArea);
setSize(200, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
textArea.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent ce) {
System.out.println("I've changed size");
}
});
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new TestTextArea();
}
}