【发布时间】:2014-04-10 12:23:36
【问题描述】:
我有一个 JButton,我希望它在按下按钮时做一些事情,我希望它在按下一个键时做同样的事情,我该怎么做?
【问题讨论】:
-
请问是什么 --
when a key is pressed,-- 因为只有ENTER和TAB键是内置加速键
标签: java swing jbutton actionlistener
我有一个 JButton,我希望它在按下按钮时做一些事情,我希望它在按下一个键时做同样的事情,我该怎么做?
【问题讨论】:
when a key is pressed, -- 因为只有 ENTER 和 TAB 键是内置加速键
标签: java swing jbutton actionlistener
要在按下按钮时执行某些操作,您应该向该按钮添加 ActionListener,如下所示:
JButton button = new JButton();
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
});
为了响应按键按下做这样的事情:(例如,如果用户输入控制 alt 7)
Action actionListener = new AbstractAction() {
public void actionPerformed(ActionEvent actionEvent) {
JButton source = (JButton) actionEvent.getSource();
System.out.println("Activated: " + source.getText());// just for test
}
};
//.....
KeyStroke controlAlt7 = KeyStroke.getKeyStroke("control alt 7");
InputMap inputMap = button.getInputMap();
inputMap.put(controlAlt7, ACTION_KEY);
ActionMap actionMap = button.getActionMap();
actionMap.put(ACTION_KEY, actionListener);
【讨论】:
Action,而不是 ActionListener。然后 Action 可以被按钮和键绑定使用。
Action,它的名字只是为了有意义。无论如何,我编辑我的答案以使其更清楚。
据我所知(几年来还没有弄乱 Swing...)唯一可以作用于按钮的键按钮事件是 Tab 改变元素焦点,Enter 触发actionPerformed 方法和助记符,它们也会触发 actionPerformed。
要处理按钮点击事件,您可以这样做:
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
//Execute when button is pressed
}
});
【讨论】:
您可能想查看Action API。您可以在How to Use Actions 看到更多信息。您可以将Action 用于按钮(类似于ActionListener),您可以为其添加快捷键。
您可以看到this example 将快捷键添加到工具栏按钮以及菜单项的位置。您需要注意的有趣的事情是菜单项和工具栏按钮共享相同的Action,因此执行相同的操作。
【讨论】:
Action 而不是 ActionListener。现在 Action 可以被按钮和键盘使用了。
JButton button = new JButton("Submit");
//Add action listener to button
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
//Execute when button is pressed
//do your work here
System.out.println("You clicked the submit button");
}
});
这是如何使用带有 jbutton 的 actionlistener
【讨论】:
试试这个:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class EventedButtonExample extends JFrame {
private JButton simpleButton;
private static final long serialVersionUID = 42L;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
EventedButtonExample me = new EventedButtonExample();
me.initialize();
me.setVisible(true);
}
});
}
void initialize() {
simpleButton = new JButton("I am an simple, evented button!");
CompoundEventHandler eh = new CompoundEventHandler();
simpleButton.addActionListener(eh);
simpleButton.addKeyListener(eh);
getContentPane().add(simpleButton, BorderLayout.CENTER);
pack();
}
class CompoundEventHandler extends KeyAdapter implements ActionListener {
public void keyReleased(KeyEvent e) {
int keyCode = e.getKeyCode();
System.out.println("Key pressed: " + keyCode + " (" + KeyEvent.getKeyText(keyCode) + ")");
}
public void actionPerformed(ActionEvent e) {
System.out.println("A button is pressed!");
}
}
}
【讨论】:
Key Bindings 一起使用。所有 Swing 组件都使用 Key Bindings 来处理键盘事件。