button3_Click(sender,e)的等价物是什么;在Java?

时间:2012-07-13 04:34:47

标签: java swing action call jbutton

在Java中调用button3_Click(sender, e);相当于什么?

我正在尝试进行文本字段操作(按 Enter )触发按钮的代码。

谢谢!

2 个答案:

答案 0 :(得分: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();
    }
});

答案 1 :(得分:0)

将TextLield用于TextField,如

public void textValueChanged(TextEvent e) { 
  // call doClick();
}

对于Button使用ActionListener

public void actionPerformed(ActionEvent e) {
}

调用doClick()方法引用 http://docs.oracle.com/javase/7/docs/api/javax/swing/AbstractButton.html#doClick%28%29