我目前正在尝试实现一个 Action 监听器,它从一个文本字段input
获取文本并将其设置为字符串direction
,如下所示:
//Creates textField
JTextField input=new JTextField(20);
input.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
direction=input.getText();
}
});
但是,此代码会引发错误:
The method addActionListener(java.awt.event.ActionListener) in the type javax.swing.JTextField is not applicable for the arguments (new ActionListener(){})
如何为我想要做的事情正确创建一个 Action 监听器?
最佳答案
根据您提供的内容,您似乎导入或实现了名为 ActionListener 的 java.awt.event.ActionListener 以外的类(类名冲突)。尝试将参数限定为 java.awt.event.ActionListener:
//Creates textField
JTextField input=new JTextField(20);
input.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent event) {
direction=input.getText();
}
});
关于java - 实现 JTextField 的 Action 监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38062716/