Java Swing 中的键绑定与键侦听器:综合指南
键绑定和键侦听器是处理用户输入事件的两种方法来自 Java Swing 应用程序中的键。虽然按键侦听器提供了一种简单的方法来检测按键,但按键绑定具有多个优点,包括:
理解键绑定
按键绑定涉及两个对象:
按下某个键时,输入映射会搜索该键并找到操作名称。然后在操作映射中搜索操作名称并执行操作。
实现键绑定
要创建单个键绑定,请使用以下结构:
myComponent.getInputMap().put("userInput", "myAction"); myComponent.getActionMap().put("myAction", action);
使用 WHEN_IN_FOCUSED_WINDOW InputMap
WHEN_IN_FOCUSED_WINDOW 的 InputMap 允许聚焦窗口中的多个组件同时接收输入。例如,如果聚焦窗口中有多个坦克并希望它们接收输入,请使用以下结构:
tank1.getInputMap(IFW).put(KeyStroke.getKeyStroke("W"), "move up"); tank2.getInputMap(IFW).put(KeyStroke.getKeyStroke("S"), "move down"); // ... tankN.getInputMap(IFW).put(KeyStroke.getKeyStroke("T"), "fire");
重新绑定键
重新绑定使用 KeyListener 进行键:
obj1.getInputMap(IFW).remove(KeyStroke.getKeyStroke(oldKey)); // ... obj1.getInputMap(IFW).put(KeyStroke.getKeyStrokeForEvent(ke), obj1.getInputMap(IFW).get(KeyStroke.getKeyStroke(oldKey)));
代码示例
以下是演示键绑定的代码示例:
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class MyGame extends JFrame { public MyGame() { // Do layout management, create objects, etc. JLabel obj1 = new JLabel(); JLabel obj2 = new JLabel(); // Set key bindings for object 1 obj1.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("UP"), "move up"); obj1.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("DOWN"), "move down"); obj1.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("control CONTROL"), "fire"); obj1.getActionMap().put("move up", new MoveAction(1, 1)); obj1.getActionMap().put("move down", new MoveAction(2, 1)); obj1.getActionMap().put("fire", new FireAction(1)); // Set key bindings for object 2 obj2.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("W"), "move up"); obj2.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("S"), "move down"); obj2.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("T"), "fire"); obj2.getActionMap().put("move up", new MoveAction(1, 2)); obj2.getActionMap().put("move down", new MoveAction(2, 2)); obj2.getActionMap().put("fire", new FireAction(2)); // Add objects to the JFrame add(obj1); add(obj2); } // Handle key rebinding public static void rebindKey(KeyEvent ke, String oldKey) { // Find and replace key binding } // Main method public static void main(String[] args) { new MyGame(); } // Inner class for move action private class MoveAction extends AbstractAction { int direction; int player; MoveAction(int direction, int player) { this.direction = direction; this.player = player; } @Override public void actionPerformed(ActionEvent e) { // Implement move logic } } // Inner class for fire action private class FireAction extends AbstractAction { int player; FireAction(int player) { this.player = player; } @Override public void actionPerformed(ActionEvent e) { // Implement fire logic } } }
结论
键绑定提供了一种强大而灵活的方式处理 Java Swing 应用程序中的用户输入事件。通过利用按键绑定,开发人员可以增强应用程序的响应能力、可维护性和可重用性。
以上是Java Swing 键处理:键绑定或键侦听器 – 您应该选择哪一个?的详细内容。更多信息请关注PHP中文网其他相关文章!