【问题标题】:Show a default text in a jTextField在 jTextField 中显示默认文本
【发布时间】:2023-03-14 21:16:01
【问题描述】:

我想在我的文本字段中有一个默认的“$(美元符号)”,即使有另一个输出要显示并且即使我需要在同一个文本字段中输入一些内容,它也不应该消失。

nevermindddd,我自己解决了,呵呵:DDDDD

【问题讨论】:

    标签: java swing default jtextfield


    【解决方案1】:

    如果您要在文本字段中显示某些内容,只需在要显示的内容的开头添加 $。

    如果您想让它始终出现在输入中,您可以尝试以下操作:

    textBox.setText("$ ");
    textBox.textProperty().addListener((observable, oldValue, newValue) -> {
        try {
            if(textBox.getText().charAt(0) == "$".charAt(0) && textBox.getText().charAt(1) == " ".charAt(0)) {
                // there is a $ at the beginning
            } else {
                // there is no $ and we have to insert it
                textBox.setText("$ " + textBox.getText());
            }
        }
        catch (Exception e) {
            textBox.setText("$ ");
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-17
      • 1970-01-01
      • 2017-12-04
      • 1970-01-01
      相关资源
      最近更新 更多