当按下回车键时,JFrame 中的默认按钮不触发
我有一个带有三个JButtons的东西。我已经设置(一个组件)在加载时具有焦点。其中一个按钮设置为默认按钮。这是我的代码:JFrame
txtSearch
JTextField
JFrame
private void formWindowOpened(java.awt.event.WindowEvent evt)
{
// btnRefresh.setMnemonic(KeyEvent.VK_R); // Even if this line
// is not commented, but
// still the event wouldn't fire.
this.getRootPane().setDefaultButton(btnRefresh);
}
加载时,该按钮只是被选中,但在按下键时它没有执行任何操作。如何正确实现它?Enter
btnRefresh.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnRefreshActionPerformed(evt);
}
});
private void btnRefreshActionPerformed(java.awt.event.ActionEvent evt) {
JOptionPane.showMessageDialog(this, "Pressed!");
// Other codes here (Replace by JOptionPane)
}