栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Java 如何使用键绑定代替键侦听器

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java 如何使用键绑定代替键侦听器

该答案说明并演示了如何使用键绑定代替键侦听器来进行教学。它不是

  • 如何用Java编写游戏。
  • 良好的代码编写外观应该如何(例如可见性)。
  • 实现键绑定的最有效的方法(基于性能或代码)。

它是

对于那些在关键听众方面遇到麻烦的人,我要发表的答案是。
回答; 阅读有关键绑定的Swing教程。

我不想阅读手册,告诉我为什么我想使用键绑定而不是已经拥有的漂亮代码!

好吧,Swing教程说明了

  • 按键绑定不需要你单击组件(使其具有焦点):
  • 从用户的角度消除意外行为。
  • 如果你有2个对象,则它们将无法同时移动,因为在给定时间只有一个对象可以具有焦点(即使将它们绑定到不同的键)也是如此。
  • 键绑定更易于维护和操作:
  • 禁用,重新绑定,重新分配用户操作要容易得多。
  • 该代码更易于阅读。

  • 好的,你说服了我尝试一下。它是如何工作的?

键绑定涉及2个对象InputMap和ActionMap。InputMap将用户输入映射到动作名称,ActionMap将动作名称映射到Action。当用户按下某个键时,将在输入映射图中搜索该键并找到一个动作名称,然后在该动作图中搜索该动作名称并执行该动作。

  1. 看起来很麻烦。为什么不将用户输入直接绑定到操作并删除操作名称?然后,你只需要一张地图,而不需要两张。

好问题!你将看到,这是使键绑定更易于管理(禁用,重新绑定等)的事情之一。

  • 我希望你给我一个完整的工作代码。

否(Swing教程中有工作示例)。

You suck! I hate you!

以下是进行单个键绑定的方法:

myComponent.getInputMap().put("userInput", "myAction");myComponent.getActionMap().put("myAction", action);

请注意,有3 InputMap秒对不同的聚焦状态做出反应:

myComponent.getInputMap(JComponent.WHEN_FOCUSED);myComponent.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);myComponent.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
  • WHEN_FOCUSED
    ,也就是没有提供任何参数时使用的组件,当组件具有焦点时使用。这类似于关键侦听器的情况。
  • WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
    当焦点组件位于已注册以接收操作的组件内部时,使用。如果你在太空飞船中有很多机组人员,并且你希望太空飞船在任何机组人员都集中注意力的情况下继续接收输入,请使用此功能。
  • WHEN_IN_FOCUSED_WINDOW
    当注册用于接收动作的组件位于焦点组件内部时使用。如果你在一个集中的窗口中有许多坦克,并且希望所有坦克同时接收输入,请使用此功能。
    假设两个对象要同时被控制,问题中呈现的代码将类似于以下内容:
public class MyGame extends Jframe {    private static final int IFW = JComponent.WHEN_IN_FOCUSED_WINDOW;    private static final String MOVE_UP = "move up";    private static final String MOVE_DOWN = "move down";    private static final String FIRE = "move fire";    static JLabel obj1 = new JLabel();    static JLabel obj2 = new JLabel();    public MyGame() {//      Do all the layout management and what not...        obj1.getInputMap(IFW).put(KeyStroke.getKeyStroke("UP"), MOVE_UP);        obj1.getInputMap(IFW).put(KeyStroke.getKeyStroke("DOWN"), MOVE_DOWN);//      ...        obj1.getInputMap(IFW).put(KeyStroke.getKeyStroke("control CONTROL"), FIRE);        obj2.getInputMap(IFW).put(KeyStroke.getKeyStroke("W"), MOVE_UP);        obj2.getInputMap(IFW).put(KeyStroke.getKeyStroke("S"), MOVE_DOWN);//      ...        obj2.getInputMap(IFW).put(KeyStroke.getKeyStroke("T"), 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));        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));//      In practice you would probably create your own objects instead of the JLabels.//      Then you can create a convenience method obj.inputMapPut(String ks, String a)//      equivalent to obj.getInputMap(IFW).put(KeyStroke.getKeyStroke(ks), a);//      and something similar for the action map.        add(obj1);        add(obj2);//      Do other GUI things...    }    static void rebindKey(KeyEvent ke, String oldKey) {//      Depends on your GUI implementation.//      Detecting the new key by a KeyListener is the way to go this time.        obj1.getInputMap(IFW).remove(KeyStroke.getKeyStroke(oldKey));//      Removing can also be done by assigning the action name "none".        obj1.getInputMap(IFW).put(KeyStroke.getKeyStrokeForEvent(ke),      obj1.getInputMap(IFW).get(KeyStroke.getKeyStroke(oldKey)));//      You can drop the remove action if you want a secondary key for the action.    }    public static void main(String[] args) {        new MyGame();    }    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) { // Same as the move method in the question pre. // Player can be detected by e.getSource() instead and call its own move method.        }    }    private class FireAction extends AbstractAction {        int player;        FireAction(int player) { this.player = player;        }        @Override        public void actionPerformed(ActionEvent e) { // Same as the fire method in the question pre. // Player can be detected by e.getSource() instead, and call its own fire method. // If so then remove the constructor.        }    }}

你可以看到,将输入映射与动作映射分开可以实现可重用的代码并更好地控制绑定。此外,如果需要功能,也可以直接控制操作。例如:

FireAction p1Fire = new FireAction(1);p1Fire.setEnabled(false); // Disable the action (for both players in this case).

有关更多信息,请参见“ 操作”教程。

我看到你对4个键(方向)使用了1个动作(移动),对1个键使用了1个动作(触发)。为什么不给每个键一个自己的动作,或者不给所有键一个相同的动作,并理清在该动作内部做什么(例如在移动情况下)?

好点子。从技术上讲,你可以同时进行这两种操作,但是你必须考虑什么才是有意义的,以及什么才能简化管理和重用代码。在这里,我假设在所有方向上的移动都是相似的,而发射是不同的,所以我选择了这种方法。

我看到很多

KeyStroke
用过的,那是什么?他们像
KeyEvent
吗?

是的,它们具有类似的功能,但更适合在此使用。请参阅其API以获取信息以及如何创建它们。

有什么问题吗 有改善吗?有什么建议吗?发表评论。有更好的答案吗?发表它。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/370690.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号