【发布时间】:2012-07-13 04:34:46
【问题描述】:
在 Java 中调用button3_Click(sender, e); 的作用是什么?
我正在尝试使文本字段操作(按 Enter)触发按钮的代码。
谢谢!
【问题讨论】:
标签: java swing action call jbutton
在 Java 中调用button3_Click(sender, e); 的作用是什么?
我正在尝试使文本字段操作(按 Enter)触发按钮的代码。
谢谢!
【问题讨论】:
标签: java swing action call jbutton
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();
}
});
【讨论】:
对 TextField 使用 TextListener 之类的
public void textValueChanged(TextEvent e) {
// call doClick();
}
对于按钮使用 ActionListener
public void actionPerformed(ActionEvent e) {
}
调用doClick()方法参考 http://docs.oracle.com/javase/7/docs/api/javax/swing/AbstractButton.html#doClick%28%29
【讨论】: