当您在 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);
}
我想知道是否有一种现成的方法可以使 JTextField 调整其宽度。我希望我可以只使用 JTextArea,但我只能接受一行输入。
最佳答案
but I can only accept one line of input
Single Line Text Area展示了如何做到这一点。相关代码为
textArea.getDocument().putProperty("filterNewlines", Boolean.TRUE);
关于java - 单行 JTextArea,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7745690/