loginButton.addActionListener(new KeyAdapter(){
 public void keyReleased(KeyEvent e){
  if(e.getKeyCode()==10){
    // do something you wanted to do.
  }
 }
});

解决方案 »

  1.   

    好象不行啊,楼上的兄弟?首先有错,对于KeyAdapter应该用addKeyListener,我改成了
    loginButton.addKeyListener(new KeyAdapter(){
     public void keyReleased(KeyEvent e){
      if(e.getKeyCode()==10){
        // do something you wanted to do.
      }
     }
    });
    后没有错了,但是敲回车没反应,怎么回事呀?
      

  2.   

    看看JAVA书 你能找到答案的.....
    启动界面的时候你想选中的话
    你可以在JAVA初始化方法里面设置成按下状态之类的都行啊
      

  3.   

    // 用setDefaultButton();import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;public class SetDefaultButton { 

    public static void main(String[] args) {   

    final JDialog dlg = new JDialog();        
    dlg.addWindowListener(new WindowAdapter(){            
    public void windowClosing(WindowEvent evt) {                
    System.exit(0);            
    }        
    }); 
          

    Container p = Box.createVerticalBox();     
         

    JButton can = new JButton("Cancel");        
    can.setVerifyInputWhenFocusTarget(false);        
    can.addActionListener(new ActionListener(){           
    public void actionPerformed(ActionEvent evt) {                
    System.out.println("cancel");            
    }        
    });        

    p.add(can);        
    final JButton ok = new JButton("OK");        
    ok.addActionListener(new ActionListener(){            
    public void actionPerformed(ActionEvent evt) {                
    System.out.println("OK");            
    }        
    });        

    p.add(ok);        
          
    dlg.setContentPane(p);        
    dlg.getRootPane().setDefaultButton(ok);        
    dlg.pack();        
    dlg.setLocationRelativeTo(null);        
    dlg.setVisible(true);    
    }
    }
      

  4.   

    dlg.getRootPane().setDefaultButton(ok); 这一句是实质!呵呵,这样就得到了焦点,然后给按钮增加键盘监听事件就ok啦!
      

  5.   

    还有就是用Tab键也可以得到某一个控件的焦点