유니 (uni)를위한 임무가 주어졌으며 사용자가 방향키를 사용하여 게임을 제어 할 수 있도록해야합니다.자바 키 바인딩
지금까지 다음과 같은 사항이 있지만 작동하지 않습니다. 제가 빠진 것이 명백합니까?
// key bindings
// add the key bindings for up, down, left and right to the input map
gamePanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN,0), "down");
gamePanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_UP,0), "up");
gamePanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT,0), "left");
gamePanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT,0), "right");
// assign actions to the key bindings in the action map
gamePanel.getActionMap().put("down", new AbstractAction()
{
public void actionPerformed(ActionEvent event)
{
move("DOWN");
}
});
gamePanel.getActionMap().put("up", new AbstractAction()
{
public void actionPerformed(ActionEvent event)
{
move("UP");
}
});
gamePanel.getActionMap().put("left", new AbstractAction()
{
public void actionPerformed(ActionEvent event)
{
move("LEFT");
}
});
gamePanel.getActionMap().put("right", new AbstractAction()
{
public void actionPerformed(ActionEvent event)
{
move("RIGHT");
}
});
방향 버튼 중 하나를 누르면 아무 일도 일어나지 않습니다.
미리 도움을 주셔서 감사합니다.
MCVE은 첫 번째 below- 나는 충분히 여부를
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import com.sun.java.swing.plaf.windows.resources.windows;
import java.util.ArrayList;
public class MCVE extends JFrame
{
// Game panel
private JPanel gamePanel;
private Container window;
public static void main(String[] args)
{
MCVE frame = new MCVE();
frame.setSize(1000,700);
frame.createGUI();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private void createGUI()
{
setDefaultCloseOperation(EXIT_ON_CLOSE);
window = getContentPane();
gamePanel = new JPanel();
window.add(gamePanel);
}
private void keyBindings()
{
// key bindings
// add the key bindings for up, down, left and right to the input map
gamePanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN,0), "down");
gamePanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_UP,0), "up");
gamePanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT,0), "left");
gamePanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT,0), "right");
// assign actions to the key bindings in the action map
gamePanel.getActionMap().put("down", new AbstractAction()
{
public void actionPerformed(ActionEvent event)
{
System.out.println("down");
}
});
gamePanel.getActionMap().put("up", new AbstractAction()
{
public void actionPerformed(ActionEvent event)
{
System.out.println("up");
}
});
gamePanel.getActionMap().put("left", new AbstractAction()
{
public void actionPerformed(ActionEvent event)
{
System.out.println("left");
}
});
gamePanel.getActionMap().put("right", new AbstractAction()
{
public void actionPerformed(ActionEvent event)
{
System.out.println("right");
}
});
}
}
디버깅하면 어떻게됩니까? 'move (...) '메소드는 무엇을 하는가? 디버깅 해 봤어? 실행하고 테스트 할 수있는 [최소 예제 프로그램] (http://stackoverflow.com/help/mcve)은 어디에 있습니까? –
게임을 제어하고 동일한 방법을 사용하는 JButton이 있으므로이 방법은 완벽하게 작동합니다 – ryansin
.. MCVE는 어디에 있습니까? –