2012-07-13 52 views

回答

5
final JButton btn = new JButton("Click Me!"); 
JTextField txt = new JTextField(10); 
InputMap inputMap = txt.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); 
ActionMap actionMap = txt.getActionMap(); 
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), 13); 
actionMap.put(13, new AbstractAction() 
{ 
    public void actionPerformed(ActionEvent e) 
    { 
     btn.doClick(); 
    } 
}); 
相關問題