我有一个默认按钮设置为btnClose_(关闭按钮:此按钮关闭窗口)的Jframe应用程序。 我有2个文本框,当用户单击文本框上的Enter键时也必须触发一个事件。会发生什么情况是,当光标位于文本字段上时,如果按Enter键,则关闭按钮上的事件将被触发,导致窗口关闭。在java中删除defaultButton的监听器
如果在文本字段上按下Enter键,是否可以删除默认按钮的侦听器?这是我的文本框听者
/**
* Receives the two textfield instance
*/
private void addFilterListener(JTextField txf) {
txf.addKeyListener(new KeyAdapter() {
/**
* Invoked when a key has been pressed.
*/
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
ActionListener al = btnClose_.getActionListeners()[0];
btnClose_.removeActionListener(al);
btnFilter_.doClick();
e.consume();
btnClose_.addActionListener(al);
}
}
});
}
private JButton getBtnClose(){
if(btnClose == null){
btnClose.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
getWindow().dispose();
}
});
}
return btnClose;
}
}
`会发生什么情况是当光标在文本框上时按下Enter键时,关闭按钮上的事件被触发,导致窗口关闭。您正在调用按钮单击文本框的Enter键火灾事件, – 2010-12-04 10:01:53
嗯,对于问题的描述不清楚抱歉。文本框应该听取Enter键。但是因为有一个默认按钮,即btnClose_,窗口关闭。顺便说一下,当用户按下两个文本框上的Enter键时,必须点击btnFilter_ – doku 2010-12-04 10:08:56