public NewJDialog1(Frame parent, boolean modal) { super(parent, modal); initComponents(); //设置文本框为光标焦点 jTextField1.requestFocusInWindow(); //SymListener symListener = new SymListener(); //确定按钮为回车键"ENTER" ActionListener ok = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { btnOkActionPerformed(e); } }; btnOk.addActionListener(ok); btnOk.registerKeyboardAction(ok, KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), JComponent.WHEN_IN_FOCUSED_WINDOW); //取消按钮为退出键"Escape" ActionListener cancle = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { btnOkActionPerformed(e); } }; btnCancle.addActionListener(cancle); btnCancle.registerKeyboardAction(cancle, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_IN_FOCUSED_WINDOW); } void btnOkActionPerformed(ActionEvent e) { System.out.println("ok"); this.dispose(); } void btnCancleactionPerformed(ActionEvent e) { System.out.println("cancle"); this.dispose(); }