【问题标题】:Update textfield after input输入后更新文本字段
【发布时间】:2016-04-23 01:23:20
【问题描述】:

我正在尝试编写一个带有多个 JTextField 的 GUI。其中两个字段用于浮点值。我已经通过格式化程序忽略了任何非数字输入的字段,但如果输入整数(即“5”更改为“5.00”),我还希望文本字段中的文本更新为浮点值。我尝试使用事件中的 setText() 命令更新文本字段,但显示的值没有改变。我希望在文本字段失去焦点后更改文本。

代码很长,所以我将相关的sn-p包括在内。

        heightField.addFocusListener(new FocusAdapter() {
        @Override
        public void focusLost(FocusEvent arg0) {
            heightDone = !heightField.getText().trim().equalsIgnoreCase("");

            //This is the problem code//
            if(UF.isInt(heightField.getText().trim()))
                heightField.setText(heightField.getText().trim().concat(".00"));

            System.out.println(heightField.getText());
            heightFormat = UF.isFloat(heightField.getText().trim());
            isDone();
        }

    });

【问题讨论】:

    标签: user-interface events text field


    【解决方案1】:

    也许您需要在 textField 更改后刷新框架:

    frame.repaint();
    

    【讨论】:

    • 我已经在事件的代码中尝试过了。它没有用。
    猜你喜欢
    • 2014-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多