同じ機能を実行するためにすべての矢印キーをバインドする必要がありますが、押されたキーを毎回取得します。現在、右矢印キーが次のように押されたときだけ持っています
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);