我希望用户在 JTextField
中输入一个值并使用监听器监听文本字段并将值直接打印到控制台而无需按键。
textfield1.addChangeListener(new ChangeListener() {
public void actionPerformed(ActionEvent e) {
System.out.println(textfield1);
}
});
错误:
<anonymous textfield$2> is not abstract and does not override abstract method stateChanged(ChangeEvent) in ChangeListener
最佳答案
将此私有(private)类(class)放入您的公共(public)类(class)中。就像一种方法。
private class textChangedListener implements KeyListener
{
public void keyPressed(KeyEvent e){}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e)
{
System.out.print(textField1.getText());
}
}
然后在您的主方法中将其调用到您的
JTextField
中,如下所示:private JTextField textField1; // just showing the name of the JTextField
textField1.addKeyListener(new textChangedListener());
关于java - 如何在用户输入后立即打印 JTextField 值?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20482940/