- public class GameFrame extends JFrame{
- //定义几个必要的按钮
- JButton startButton,pauContiButton,overButton;
- JPanel upPanel;
- JPanel downPanel;
- public static void main(String[] args) {
- new GameFrame().launch();
- }
- /*
- * 设置游戏界面的一些属性的方法,
- * 1,名称 setTitle();
- * 2,JFrame的大小:setSize();
- * 3,可见的:setVisible();
- * 等等
- * */
- public void launch()
- {
- upPanel=new JPanel();
- downPanel=new JPanel();
- this.setLayout(null);
- this.setTitle("贪吃蛇");
- this.setLocation(EntireBean.LOCATION_WIDTH, EntireBean.LOCATION_HEIGHT);
- this.setSize(EntireBean.GAME_WIDTH,EntireBean.GAME_HEIGHT);
-
- this.setResizable(false);
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- //在上面加上游戏的面板
- this.add(upPanel);
- //设置游戏面板的位置和大小
- upPanel.setBounds(5, 5, EntireBean.CELL_COLS*EntireBean.CELL_SIZE, EntireBean.CELL_ROWS*EntireBean.CELL_SIZE);
- //加边界线
- upPanel.setBorder(new EtchedBorder(EtchedBorder.RAISED));
- //加入下面的面板,有一些按钮
- this.add(downPanel);
- //设置按钮面板的位置和大小
- downPanel.setBounds(5, EntireBean.CELL_ROWS*EntireBean.CELL_SIZE+15, 280, 250);
- //加边界线
- downPanel.setBorder(new EtchedBorder(EtchedBorder.RAISED));
- //downPanel的设为自由布局
- downPanel.setLayout(null);
- //创建几个按钮的对象
- startButton=new JButton();
- //设置按钮的大小
- startButton.setBounds(15, EntireBean.CELL_ROWS*EntireBean.CELL_SIZE+15, 80, 50);
- //把按钮放到面板上
- downPanel.add(startButton);
- startButton.setText("开始游戏");
- overButton=new JButton();
- downPanel.add(overButton);
- overButton.setBounds(15, EntireBean.CELL_ROWS*EntireBean.CELL_SIZE+25, 80, 50);
- pauContiButton=new JButton();
- downPanel.add(pauContiButton);
- overButton.setBounds(15, EntireBean.CELL_ROWS*EntireBean.CELL_SIZE+35, 80, 50);
- this.setVisible(true);
- }
- //构造器
-
-
- }
复制代码
卡在这了 运行不出现按钮啊 除了add方法外还需要什么吗? |
|