坚持尝试实现KeyListener

时间:2012-10-29 00:30:48

标签: java swing coding-style jpanel keylistener

我在尝试让我的代码在屏幕上移动播放器(Ship)时遇到了很多麻烦。我可以让行星画在屏幕和玩家的船上,但我无法弄清楚如何实现keyListener至少打印出来的东西。提前感谢您的帮助!

   import javax.swing.JFrame;
   import javax.swing.JPanel;
   import java.awt.Canvas;
   import java.awt.Color;
   import java.awt.Dimension;
   import java.awt.Graphics;
   import java.awt.Point;
   import java.awt.Toolkit;
   import java.awt.event.KeyAdapter;
   import java.awt.event.KeyEvent;
   import java.awt.event.KeyListener;


  public class MapPanel extends JPanel {
  public static final int WIDTH = 25;
  public static final int HEIGHT = 20;
  int zone = 0;
  private int xValue;
  private int yValue;
  private Color color;
  public Planet[][] planetGrid = new Planet[WIDTH][HEIGHT];
  static Player currPlayer = new Player("h");
  static Universe universe = new Universe(currPlayer);

  /**
  * Create the panel.
  */
  public MapPanel(Universe univ, Player p) {
 this.universe = univ;
 currPlayer = p;
 int i = 0;
 this.setSize(new Dimension(450,450));
 setVisible( true );
 //this.addKeyListener(new KeyController());
 KeyController kc = new KeyController();
 this.addKeyListener(kc);
 repaint();


  }


  /**
  * Draw method to draw the playing field
  * @param g Graphics object
  * @param tileDimension dimension of the tile
  */
   public void draw(Graphics g)
   {
    universe.draw(g);

  //    KeyController key = new KeyController();
  }
  public void paintComponent(Graphics g) {
   super.paintComponent(g);
  draw(g);
   }
   public static void main(String[] args)
   {
    MapPanel mp = new MapPanel(universe, currPlayer);
   JFrame f = new JFrame();
   f.add(mp);
   f.setSize(new Dimension(450,450));
   f.setVisible(true);
   f.setFocusable(true);


    }
    private class KeyController implements KeyListener {
    public KeyController()
    {
     System.out.println("ghgh");
     setFocusable(true);
    // addKeyListener(this);
    }
    @Override
    public void keyPressed(final KeyEvent key) {
        System.out.println("fgfgf");
        if (currPlayer != null) {

            int oldX = currPlayer.getPosition().x;
            int oldY = currPlayer.getPosition().y;

            switch (key.getKeyCode()) {
                case KeyEvent.VK_RIGHT:
                  currPlayer.setPosition(new Point(oldX+1, oldY)); //move right
                  System.out.println("RIGHT");
                    break;
                case KeyEvent.VK_LEFT:
                      currPlayer.setPosition(new Point(oldX-1, oldY));         //move             left
                    break;
                case KeyEvent.VK_DOWN:
                      currPlayer.setPosition(new Point(oldX, oldY+1));         //move           down
                    break;
                case KeyEvent.VK_UP:
                      currPlayer.setPosition(new Point(oldX, oldY-1));         //move up
                    break;
            }

          }
          repaint();
        }
       @Override
    public void keyReleased(KeyEvent e) {
        System.out.println("ggg");
    }
    @Override
    public void keyTyped(KeyEvent e) {
        System.out.println("typeeeddd");
    }
     }





   }

2 个答案:

答案 0 :(得分:0)

JPanel不会自动获得键盘焦点,你应该在构造函数中调用它:

addMouseListener(new MouseAdapter(){
    public void mousePressed(MouseEvent evt){
        requestFocus();
    }
});

答案 1 :(得分:0)

您可能必须这样做

MapPanel panel = new MapPanel();
panel.setFocusable(true);

我遇到了同样的问题,我必须为所有其他组件(例如JButton)做以下事情:

myButton.setFocusable(false);

为我工作。