Java密钥绑定
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java密钥绑定,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2767字,纯文字阅读大概需要4分钟。
内容图文
![Java密钥绑定](/upload/InfoBanner/zyjiaocheng/709/d94e2727c0de4c17bf9e5bda0fc82301.jpg)
我需要绑定所有箭头键来执行相同的功能,但每次都按下哪个键.目前我只有通过以下方式按下右箭头键时才有
DoneImg.getInputMap(JLabel.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "UpArrow");
Action MvRight = new AbstractAction()
{
public void actionPerformed(ActionEvent e)
{
//Do whatever here
}
};
DoneImg.getActionMap().put("RightArrow", MvRight);
但我需要类似的东西
DoneImg.getInputMap(JLabel.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "RightArrow");
DoneImg.getInputMap(JLabel.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), "LeftArrow");
DoneImg.getInputMap(JLabel.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), "DownArrow");
DoneImg.getInputMap(JLabel.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "UpArrow");
Action MvAll = new AbstractAction()
{
public void actionPerformed(ActionEvent e)
{
if (e.keypressed == "LeftArrow")
{System.out.println("The left arrow was pressed!");}
if (e.keypressed == "RightArrow")
{System.out.println("The right arrow was pressed!");}
//and so forth
}
};
DoneImg.getActionMap().put("RightArrow", MvAll);
DoneImg.getActionMap().put("LeftArrow", MvAll);
DoneImg.getActionMap().put("UpArrow", MvAll);
DoneImg.getActionMap().put("DownArrow", MvAll);
解决方法:
您所要求的实际上是反直觉的,并且违背了键绑定API的设计.
目的是为每个击键提供单个工作单元.在我看来,这意味着你应该对每个箭头键分别采取行动.
它使得更容易遵循逻辑,进行更改,根据需要规避操作.
但我该说谁是对的:P
如果你看不到它的方法,一种方法很简单,就是为每个动作分配一个“命令”,然后你可以在触发actionPerformed时查询它.
public TestKeyBindings02() {
JPanel panel = new JPanel();
InputMap im = panel.getInputMap(JPanel.WHEN_IN_FOCUSED_WINDOW);
ActionMap am = panel.getActionMap();
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "RightArrow");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), "LeftArrow");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "UpArrow");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), "DownArrow");
am.put("RightArrow", new ArrowAction("RightArrow"));
am.put("LeftArrow", new ArrowAction("LeftArrow"));
am.put("UpArrow", new ArrowAction("UpArrow"));
am.put("DownArrow", new ArrowAction("DownArrow"));
}
public class ArrowAction extends AbstractAction {
private String cmd;
public ArrowAction(String cmd) {
this.cmd = cmd;
}
@Override
public void actionPerformed(ActionEvent e) {
if (cmd.equalsIgnoreCase("LeftArrow")) {
System.out.println("The left arrow was pressed!");
} else if (cmd.equalsIgnoreCase("RightArrow")) {
System.out.println("The right arrow was pressed!");
} else if (cmd.equalsIgnoreCase("UpArrow")) {
System.out.println("The up arrow was pressed!");
} else if (cmd.equalsIgnoreCase("DownArrow")) {
System.out.println("The down arrow was pressed!");
}
}
}
内容总结
以上是互联网集市为您收集整理的Java密钥绑定全部内容,希望文章能够帮你解决Java密钥绑定所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。