JFormattedTextField与KeyListener

3

请问为什么getValue()函数总是返回null?如果我使用getText()而不是getValue(),那么一切都很完美,但是我想要的是值而不是文本。我可以将其转换为整数,但我认为这不是最好的方法。

public class Test {    
    public static void main(String[] args) {

        JFrame jf = new JFrame("test jftf"); 
        jf.setSize(100, 100);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        jf.setVisible(true); 
        JFormattedTextField jftf = new JFormattedTextField(NumberFormat.getIntegerInstance()); 
        jftf.addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {
            }

            @Override
            public void keyPressed(KeyEvent e) {
            }

            @Override
            public void keyReleased(KeyEvent e) {
                System.out.println(jftf.getValue()); 
            }

        });
        jf.add(jftf); 
    }

}
2个回答

2

如果您依赖于getValue()方法,则需要确保当前的编辑已经提交。您可以通过调用commitEdit()方法来实现这一点,否则可能无法获得您期望的值:

@Override
public void keyReleased(KeyEvent e) {
    //Forces the current value to be taken from the AbstractFormatter 
    //and set as the current value:
    try {
        jftf.commitEdit();
    } catch (ParseException e1) {
        //Handle possible parsing error...
    }
    System.out.println(jftf.getValue()); 
}

谢谢您的回答,但首先,我需要用try和catch来包围这个语句;其次,我不明白为什么需要使用commitEdit()函数?当我使用ActionListener时,我没有遇到这个问题。 - ardayigit
已更新以处理异常。老实说,我不明白为什么在ActionListener中没有提交也能正常工作,我只能假设实现上存在差异! - StuPointerException
非常感谢您的帮助。至少现在代码可以工作了 :) - ardayigit
从代码来看,我怀疑 ActionEvent 在比 KeyPress 事件更高的级别上被触发,在此时该值已在组件内部被提交。 - StuPointerException

0

这是一个示例,展示了如何工作,值的名称不准确。 可以这样想, JFormattedTextField 有一个名为 lastValidInt 的字段,保存一个整数。 每次格式化字段中的文本时,程序将检查该字段中的文本是否为数字。如果是,则会将 lastValidInt 设置为该字符串。

getValue() 方法将检查是否已初始化 lastValidInt,如果已初始化,则返回 lastValidInt 的值。如果未初始化,则返回 null

示例: 这些示例假定每次重新启动应用程序。 输入是您将在字段中编写的内容。 输出是在输入内容后控制台将打印什么。

1:
Input: a
Ouput: null
//a is not a number therefore the output is null

2:
Input:  1
Output: 1
//1 is a number therefore the output is 1

3:
Input1: a
Ouput: null
Input2: as
Ouput: null
Input3: asd
Ouput: null
Input4: asdf
Ouput: null
//a is not a number therefore the output is null

4:
Input1: 1
Ouput: 1
Input2: 1f
Ouput: 1
//The first thing the was read here was 1 therefore lastValidInt was set to 1 then the next input is 1f which is not a number therefore lastValidInt remained 1

5:
Input1: f
Ouput: null
Input2: f1
Ouput: null
//f is not a number therefore output is null, f1 is not a number either since it has a character that is not a digit and therefore lastValidInt has not been initialized

6:
Input1: f
Ouput: null
Input2: 
Output: null
Input3: 1
Output: 1
Input4: 1f
Output: 1
//f is not a number therefore output is null, "" is also not a number therefore output is null again then the only thing that exists in the field is 1 which is a number therefore output is 1, finally by adding a f the field is no longer a number so output is same as before.

7:
Input1: f
Ouput: null
Input2: 1f (moved back to the begining without erasing f)
Output: null
//f is not a number therefore output is null, 1f is not a number either since it has a character that is not a digit and therefore lastValidInt has not been initialized

你能再具体解释一下吗?比如说,如果我按下“4”键,为什么getValue()返回null?它是一个数字,所以不应该返回null。 - ardayigit
@ardayigit 给你做了一些例子,供你参考。 - nick zoum
@ardayigit 可能是不同的版本。这取决于 actionlistener。 - nick zoum
这里有一个例子,我不需要提交。 jftf.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.out.println(jftf.getValue()); } }); - ardayigit
@ardayigit 哦,我一直在使用actionListener。没意识到你有一个keyListener。虽然我不明白为什么它们会有不同的效果。 - nick zoum
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接