【问题标题】:Single line JTextArea单行 JTextArea
【发布时间】:2011-12-06 10:30:27
【问题描述】:

当您输入 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,但我只能接受一行输入。

【问题讨论】:

    标签: java swing text awt


    【解决方案1】:

    但我只能接受一行输入

    Single Line Text Area 展示了如何做到这一点。相关代码是

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

    【讨论】:

      猜你喜欢
      • 2023-03-24
      • 2012-04-17
      • 2012-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多