2014年5月27日星期二

Help , why add another panel after listening to the keyboarduseless ?

I learn how to make the Snake game , when the control panel and without explanation panels, game panel buttons change according to the direction of the snake :
public class MainFrame {

public static void main(String[] args) {
// TODO 自动生成的方法存根
GamePanel gamePanel = new GamePanel();
OptionsPanel optionsPanel = new OptionsPanel();
JPanel infoPanel = new InfoPanel();
Snake snake = new Snake();
Food food = new Food();
Ground ground = new Ground();
Controller controller = new Controller(
snake, food, ground, gamePanel);

JFrame jf = new JFrame();
jf.setTitle("HQS贪吃蛇");

//不使用布局管理器!运用绝对布局!
jf.setLayout(null);
jf.setResizable(false);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(Global.WIDTH * Global.CELL_SIZE + 20
, Global.HEIGHT * Global.CELL_SIZE + 250);
gamePanel.setSize(Global.WIDTH * Global.CELL_SIZE
, Global.HEIGHT * Global.CELL_SIZE);
gamePanel.setBorder(new EtchedBorder(EtchedBorder.LOWERED));

//添加游戏面板
gamePanel.setBounds(5, 10, Global.WIDTH * Global.CELL_SIZE
, Global.HEIGHT * Global.CELL_SIZE);
jf.getContentPane().add(gamePanel);

//添加控制面板
/*optionsPanel.setBounds(5, 415, 460, 200);
jf.getContentPane().add(optionsPanel);

//添加说明面板
infoPanel.setBounds(470, 415, 235, 200);
jf.getContentPane().add(infoPanel);
*/
gamePanel.addKeyListener(controller);
snake.addSnakeListener(controller);
jf.addKeyListener(controller);

jf.setVisible(true);
controller.newGame();
System.out.println("After running!");
}

}After running!");
}

}



but the control panel and the rear panel plus instructions , keyboard sniffers useless. Key smugglers did not respond !



This is the listener class :
public class Controller extends KeyAdapter implements SnakeListener {

private Snake snake;
private Food food;
private Ground ground;
private GamePanel gamePanel;

public Controller(Snake snake, Food food, Ground ground, GamePanel gamePanel) {
super();
this.snake = snake;
this.food = food;
this.ground = ground;
this.gamePanel = gamePanel;
}
public void keyPressed(KeyEvent e) {
// TODO 自动生成的方法存根
super.keyPressed(e);

System.out.println("key press~!");
//根据键盘按键,来改变方向
switch(e.getKeyCode()){
case KeyEvent.VK_UP:
System.out.println("UP");
snake.changeDirection(Snake.UP);
break;
case KeyEvent.VK_DOWN:
System.out.println("DOWN");
snake.changeDirection(Snake.DOWN);
break;
case KeyEvent.VK_LEFT:
System.out.println("LEFT");
snake.changeDirection(Snake.LEFT);
break;
case KeyEvent.VK_RIGHT:
System.out.println("RIGHT");
snake.changeDirection(Snake.RIGHT);
break;
default:
System.out.println("null");
break;
}
}
@Override
public void SnakeMove(Snake snake) {
// TODO 自动生成的方法存根

if(food.isEatFood(snake)){
snake.eatFood();
food.newFood(ground.getPoint());
}

if(ground.isSnakeEatRock(snake)){
snake.die(false);
}

if(snake.isEatBody()){
snake.die(false);
}
gamePanel.dispaly(snake, food, ground);
}

public void newGame(){
snake.start();
food.newFood(ground.getPoint());
}
}

help it, this is where the problem is !
------ Solution ---------------------------------------- ----
JComponent and its subclasses have a grabFocus (), you try

------ For reference only ---------------------------------- -----
to set the display image components are always active.
------ For reference only -------------------------------------- -
or listen on you most of the top-level container inside
------ For reference only ----------------------- ----------------

Thank However , JFrame not a top-level container it? "Jf.addKeyListener (controller);" should be added to the top of the container , right ?
------ For reference only -------------------------------------- -

how to make components always active ?
------ For reference only -------------------------------------- -
online help, nobody ah
------ For reference only ---------------------------- -----------
Thank you, has been resolved, because there was no let gamePanel component settings to get the focus method "this.setFocusable (true);".

没有评论:

发表评论