import javax.swing.*;
import java.awt.*;
import java.awt.event.*;public class AuthenDlg extends JDialog implements ActionListener
{
    JPanel panel1 = new JPanel();
    JLabel jLabel1 = new JLabel();
    JButton jButton1 = new JButton();
    JButton jButton2 = new JButton();
    JPasswordField jPasswordField1 = new JPasswordField(10);    public AuthenDlg(JFrame frame, String title, boolean modal) {
        super(frame, title, modal);
        try {
            jbInit();
            setSize(400,170);
            setLocation(300,200);
        }
        catch(Exception ex) {
            ex.printStackTrace();
        }
    }
    public AuthenDlg() {
        this(null, "key_test", false);
    }
    void jbInit() throws Exception {
        panel1.setLayout(null);        usrName = null;
        jLabel1.setText("PASSWORD");
        jLabel1.setBounds(new Rectangle(73, 40,70,20));
        jButton1.setText("OK");
        jButton1.setBounds(new Rectangle(102, 91,85,25));
        jButton1.addActionListener(this);
        jButton1.addKeyListener(new java.awt.event.KeyListener() {
            public void keyPressed(KeyEvent e)
            {
                key_press_event(e);
            }            public void keyTyped(KeyEvent e) {}            public void keyReleased(KeyEvent e) {}           }
        );
        jButton2.setText("CANCEL");
        jButton2.setBounds(new Rectangle(199, 91,85,25));
        jButton2.addActionListener(this);
        jButton2.addKeyListener(new java.awt.event.KeyListener() {
            public void keyPressed(KeyEvent e)
            {
                key_press_event(e);
            }            public void keyTyped(KeyEvent e) {}            public void keyReleased(KeyEvent e) {}           }
        );
        getContentPane().add(panel1);
        jPasswordField1.setBounds(new Rectangle(171, 40,140,20));
        jPasswordField1.addKeyListener(new java.awt.event.KeyListener() {
            public void keyPressed(KeyEvent e)
            {
                key_press_event(e);
            }            public void keyTyped(KeyEvent e) {}            public void keyReleased(KeyEvent e) {}           }
        );
        panel1.add(jButton1, null);
        panel1.add(jButton2, null);
        panel1.add(jLabel1, null);
        panel1.add(jPasswordField1,null);
                this.addWindowListener(new MyWindowListener());
        this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
    }    public static void main(String args[]){
        AuthenDlg lock = new AuthenDlg();
        lock.show();
    }   
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource() == jButton1)
        {
           jButton1_actionPerformed();
        }        if(e.getSource() == jButton2)
        {
            cancelAct();
        }
    }    void jButton1_actionPerformed()
    {
        System.out.println("this is ok");        
    }    void key_press_event(KeyEvent e)
    {
        if (e.getKeyCode()==KeyEvent.VK_ENTER )
            jButton1_actionPerformed();
        
        if (e.getKeyCode()==KeyEvent.VK_E)
            cancelAct();
    }
    
    void cancelAct()
    {
System.out.println("this is cancel");
this.hide();
}  
class MyWindowListener extends WindowAdapter
{
public void windowClosing(WindowEvent e)
    {
        cancelAct();
}}}

解决方案 »

  1.   

    jButton.setMnemonic(KeyEvent.VK_ENTER);试试看
      

  2.   

    to alphazhao(绿色咖啡):你的可以设置快捷键,但是快捷键不是"ENTER",而是"Alt+ENTER"键,能否改进一下?
    to chinaredflag(chinaredflag):你是我试过一下,但是不行,难道是我截取错了?能不能只给出我需要的?精简一点btw:我再加点分
      

  3.   

    当button有焦点时, 按空格将激发action事件
      

  4.   

    问题是,需要的是用“ENTER”键来做快捷键,无论是有焦点……
      

  5.   

    监视按键事件咯. . .当有回车按下的时候,发送一个action事件给这个按钮
      

  6.   

    监视按键事件咯. . .当有回车按下的时候,发送一个action事件给这个按钮
      

  7.   

    to drinkant(喝酒的蚂蚁):
    天哪!如果是这样的话,那岂不是我在其他想回车的地方都不能回撤了?呵呵!你可以试试registerKeyboardAction,下面是它的api文档。
    public void registerKeyboardAction(ActionListener anAction,
                                       KeyStroke aKeyStroke,
                                       int aCondition)This method is now obsolete, please use a combination of getActionMap() and getInputMap() for similiar behavior.
      

  8.   

    感觉shinb办法不行
    你这只是增加/去除jbutton的键盘事件而已
    事实上,默认的就是有回车事件的,只不过这位老兄要做的是在发生VK_ENTER和VK_E的时候处理他自己的事情
      

  9.   

    to drinkant:
    “监视该监视的那些东东”那你说该监听什么?
    “增加/去除jbutton的键盘事件”对呀!所以在你的anAction就要抓住这个事件,并作相应的处理喽!
      

  10.   

    jButton.setMnemonic(KeyEvent.VK_ENTER);是肯定可以的,不过就是“Alt+Enter”。其它的我也不知道啊,是不是有类似的KeyEvent.?可以选择成只要回车就可以?
      

  11.   

    1。jButton.setMnemonic(KeyEvent.VK_ENTER);这是一种较简单的,要按alt+enter。
      

  12.   

    首先引入包
    import javax.swing.event.*;
    然后定义一个
        KeyStroke stroke1 = KeyStroke.getKeyStroke     (KeyEvent.VK_ENTER,ActionEvent.CTRL_MASK,true);//创建一个KeyStroke类
        最后一个参数的意思是是否在Keyrelease时触发此事件
        JButton jButton1 = new JButton();   jButton1.registerKeyboardAction(new ActionListener(){
                //要执行的方法
                public void actionPerformed(ActionEvent e){
                    System.out.println("OK");
                }
            },stroke1,JComponent.WHEN_IN_FOCUSED_WINDOW);
        }
        registerKeyboardAction方法的参数意思是这样的
        1.ActionListener对象,可以定义你要执行的方法
        2.KeyStroke 对象,定义触发事件的条件
        3.何时按Ctrl+Enter时发生此事件,比如
            JComponent.WHEN_IN_FOCUSED_WINDOW
            JComponent.WHEN_FOCUSED等
      

  13.   

    你要想用Enter直接响应,最好的办法是将每个组件都加键盘的响应事件,然后,执行你要的方法
    例如:
    class Key_Listener implements KeyListener
    {
                public void keyPressed(KeyEvent e)
                {
                    key_press_event(e);
                }            public void keyTyped(KeyEvent e) {}            public void keyReleased(KeyEvent e) {}            void key_press_event(KeyEvent e)
                 {
                    if (e.getKeyCode()==KeyEvent.VK_ENTER )
                         System.out.println("enter");
            
                     if (e.getKeyCode()==KeyEvent.VK_E)
                        System.out.println("e");
                  }            }
    在你的程序中,每个组件都加响应事件
    Key_Listener keyL = new Key_Listener();........
    jButton1.addKeyListener(keyL) ;
    jButton2.addKeyListener(keyL) ;
    ........
      

  14.   

    to all
    算了
    我先结贴
    再开一个窗口问!!