我需要绑定所有箭头键以执行相同的功能,但每次都按下哪个键。目前我只有通过以下方式按下右箭头键时
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);