2011-10-12 22 views
4

當你鍵入一個JTextArea,它會自動調整它的大小,使其適合在鍵入的文本。單線的JTextArea

一個JTextField然而,似乎不這樣做。 下面是這表明了問題的SSCCE:

public static void main(String[] args) { 

    JFrame frame = new JFrame(); 
    frame.setLayout(new FlowLayout(FlowLayout.LEFT)); 
    frame.add(new JTextArea("I resize as you type.")); 
    frame.add(new JTextField("I don't.")); 
    frame.setSize(200,100); 
    frame.setVisible(true); 
} 

Automatic resizing

我在想,如果有現成的一種方法,使一個JTextField調整它的寬度。我希望我只能使用JTextArea,但我只能接受一行輸入。

回答

8

,但我只能接受輸入

Single Line Text Area的一行說明你如何能做到這一點。相關代碼是

textArea.getDocument().putProperty("filterNewlines", Boolean.TRUE);