62,614
社区成员
发帖
与我相关
我的任务
分享
package com.huowolf.game;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import com.huowolf.controller.Controller;
import com.huowolf.entities.Food;
import com.huowolf.entities.Ground;
import com.huowolf.entities.Snake;
import com.huowolf.util.Global;
import com.huowolf.view.BottonPanel;
import com.huowolf.view.GameMenu;
import com.huowolf.view.GamePanel;
public class GameFrame extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
new GameFrame(new Controller(new Snake(), new Food(), new Ground(),
new GamePanel(), new GameMenu()));
}
private GamePanel gamePanel;
private GameMenu gameMenu;
private Snake snake;
//private Food food;
//private Ground ground;
private Controller controller;
//private JPanel buttonPanel;
public GameFrame(Controller c) {
this.controller = c;
snake = controller.getSnake();
gameMenu = controller.getGameMenu();
gamePanel = controller.getGamePanel();
setTitle("我的贪吃蛇");
setBounds(300,100,Global.WIDTH*Global.CELL_SIZE+100,Global.HEIGHT*Global.CELL_SIZE+150);
setLayout(new BorderLayout());
setJMenuBar(gameMenu);
add(gamePanel,BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(new BottonPanel(),BorderLayout.SOUTH);
//setResizable(false);
setVisible(true);
gamePanel.addKeyListener(controller);
snake.addSnakeListener(controller);
controller.newGame();
}
}
package com.huowolf.view;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JSeparator;
public class BottonPanel extends JPanel{
/**
*
*/
private static final long serialVersionUID = 1L;
private JButton startButton;
private JButton pauseButton;
private JButton endButton;
public BottonPanel() {
//setLayout(new GridLayout(3, 1));
//setSize(200, 150);
setBounds(10, 10, 100, 150);
setFocusable(false);
setLayout(null);
startButton = new JButton("开始游戏");
startButton.setBounds(30,10, 80, 40);
add(startButton);
pauseButton = new JButton("暂停游戏");
pauseButton.setBounds(30, 40, 80, 40);
add(pauseButton);
endButton = new JButton("结束游戏");
endButton.setBounds(30, 70, 80, 40);
add(endButton);
/*final JSeparator separator = new JSeparator();
separator.setBounds(20, 30, 20, 30);
add(separator);*/
}
}