当我想要执行某些操作时,如何对其进行编码。假设当用户同时按下 ctrl + alt + 退格时,会弹出对JTextField和消息框的关注。 / p>
答案 0 :(得分:4)
使用键绑定的示例:
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;
public class JTextFieldMagic {
public static final String CTRL_ALT_BACK_SPACE = "ctrlAltBackspace";
public static void main(String[] args) {
JTextField field = new JTextField(10);
int condition = JComponent.WHEN_FOCUSED;
InputMap inputmap = field.getInputMap(condition);
ActionMap actionMap = field.getActionMap();
KeyStroke ctrlAltBackSpaceKeyStroke = KeyStroke.getKeyStroke(
KeyEvent.VK_BACK_SPACE,
KeyEvent.CTRL_DOWN_MASK | KeyEvent.ALT_DOWN_MASK);
inputmap.put(ctrlAltBackSpaceKeyStroke, CTRL_ALT_BACK_SPACE);
actionMap.put(CTRL_ALT_BACK_SPACE, new CtrlAltBackspaceAction());
JOptionPane.showMessageDialog(null, field);
}
}
class CtrlAltBackspaceAction extends AbstractAction {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(JTextFieldMagic.CTRL_ALT_BACK_SPACE);
}
}
答案 1 :(得分:2)
另外,如果你在“应用程序全局密钥监听器”之后,可以使用KeyboardFocusManager.addKeyEventDispatcher
或Toolkit.addAWTEventListener
。
public class GloablKeyListener {
public static void main(String[] args) {
new GloablKeyListener();
}
public GloablKeyListener() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setLayout(new BorderLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.setSize(200, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
protected class TestPane extends JPanel {
private JLabel label;
public TestPane() {
setLayout(new BorderLayout());
label = new JLabel("Nothing happening here");
label.setHorizontalAlignment(JLabel.CENTER);
add(label);
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher() {
@Override
public boolean dispatchKeyEvent(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE && e.isControlDown() && e.isAltDown()) {
label.setText("Hit me");
} else {
label.setText("Nothing to see here...");
}
return false;
}
});
}
}
}
public class GloablKeyListener {
public static void main(String[] args) {
new GloablKeyListener();
}
public GloablKeyListener() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setLayout(new BorderLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.setSize(200, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
protected class TestPane extends JPanel {
private JLabel label;
public TestPane() {
setLayout(new BorderLayout());
label = new JLabel("Nothing happening here");
label.setHorizontalAlignment(JLabel.CENTER);
add(label);
Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
@Override
public void eventDispatched(AWTEvent event) {
if (event instanceof KeyEvent) {
KeyEvent ke = (KeyEvent) event;
if (ke.getID() == KeyEvent.KEY_TYPED) {
if (ke.getKeyCode() == KeyEvent.VK_BACK_SPACE && ke.isControlDown() && ke.isAltDown()) {
label.setText("Hit me");
} else {
label.setText("Nothing to see here...");
}
}
}
}
}, AWTEvent.KEY_EVENT_MASK);
}
}
}
就个人而言,我更喜欢KeyboardFocusManager.addKeyEventDispatcher
。它更简单易用。
您可以设置自己的singlton管理器,您可以在KeyStroke
s上分配Action
s,就像键绑定一样。