A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 蒙奇.D.路飞 中级黑马   /  2014-5-21 17:54  /  1232 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. public class GameFrame extends JFrame{
  2.         //定义几个必要的按钮
  3.         JButton startButton,pauContiButton,overButton;
  4.         JPanel upPanel;
  5.         JPanel downPanel;
  6.         public static void main(String[] args) {
  7.                 new GameFrame().launch();
  8.         }
  9.         /*
  10.          * 设置游戏界面的一些属性的方法,
  11.          * 1,名称 setTitle();
  12.          * 2,JFrame的大小:setSize();
  13.          * 3,可见的:setVisible();
  14.          * 等等
  15.          * */
  16.         public void launch()
  17.         {
  18.                 upPanel=new JPanel();
  19.                 downPanel=new JPanel();
  20.                 this.setLayout(null);
  21.                 this.setTitle("贪吃蛇");
  22.                 this.setLocation(EntireBean.LOCATION_WIDTH, EntireBean.LOCATION_HEIGHT);
  23.                 this.setSize(EntireBean.GAME_WIDTH,EntireBean.GAME_HEIGHT);
  24.                
  25.                 this.setResizable(false);
  26.                 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  27.                 //在上面加上游戏的面板
  28.                 this.add(upPanel);
  29.                 //设置游戏面板的位置和大小
  30.                 upPanel.setBounds(5, 5, EntireBean.CELL_COLS*EntireBean.CELL_SIZE, EntireBean.CELL_ROWS*EntireBean.CELL_SIZE);
  31.                 //加边界线
  32.                 upPanel.setBorder(new EtchedBorder(EtchedBorder.RAISED));
  33.                 //加入下面的面板,有一些按钮
  34.                 this.add(downPanel);
  35.                 //设置按钮面板的位置和大小
  36.                 downPanel.setBounds(5, EntireBean.CELL_ROWS*EntireBean.CELL_SIZE+15, 280, 250);
  37.                 //加边界线
  38.                 downPanel.setBorder(new EtchedBorder(EtchedBorder.RAISED));
  39.                 //downPanel的设为自由布局
  40.                 downPanel.setLayout(null);
  41.                 //创建几个按钮的对象
  42.                 startButton=new JButton();
  43.                 //设置按钮的大小
  44.                 startButton.setBounds(15, EntireBean.CELL_ROWS*EntireBean.CELL_SIZE+15, 80, 50);
  45.                 //把按钮放到面板上
  46.                 downPanel.add(startButton);
  47.                 startButton.setText("开始游戏");
  48.                 overButton=new JButton();
  49.                 downPanel.add(overButton);
  50.                 overButton.setBounds(15, EntireBean.CELL_ROWS*EntireBean.CELL_SIZE+25, 80, 50);
  51.                 pauContiButton=new JButton();
  52.                 downPanel.add(pauContiButton);
  53.                 overButton.setBounds(15, EntireBean.CELL_ROWS*EntireBean.CELL_SIZE+35, 80, 50);
  54.                 this.setVisible(true);
  55.         }
  56.         //构造器
  57.        
  58.        
  59. }
复制代码

卡在这了 运行不出现按钮啊 除了add方法外还需要什么吗?

4 个回复

倒序浏览
来学习了。。。。谢谢楼主
回复 使用道具 举报
小小6456 发表于 2014-5-21 18:10
来学习了。。。。谢谢楼主

可是我这是提问啊 我也不会呢 求助呢 不过大家一起学 嘻嘻
回复 使用道具 举报
  1. public class GameFrame extends JFrame{
  2.         //定义几个必要的按钮
  3.         JButton startButton,pauContiButton,overButton;
  4.         JPanel upPanel;
  5.         JPanel downPanel;
  6.         public static void main(String[] args) {
  7.                 new GameFrame().launch();
  8.         }
  9.         /*
  10.          * 设置游戏界面的一些属性的方法,
  11.          * 1,名称 setTitle();
  12.          * 2,JFrame的大小:setSize();
  13.          * 3,可见的:setVisible();
  14.          * 等等
  15.          * */
  16.         public void launch()
  17.         {
  18.                 upPanel=new JPanel();
  19.                 downPanel=new JPanel();
  20.                 this.setLayout(null);
  21.                 this.setTitle("贪吃蛇");
  22.                 this.setLocation(EntireBean.LOCATION_WIDTH, EntireBean.LOCATION_HEIGHT);
  23.                 this.setSize(EntireBean.GAME_WIDTH,EntireBean.GAME_HEIGHT);
  24.                
  25.                 this.setResizable(false);
  26.                 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  27.                 //在上面加上游戏的面板
  28.                 this.add(upPanel);
  29.                 //设置游戏面板的位置和大小
  30.                 upPanel.setBounds(5, 5, EntireBean.CELL_COLS*EntireBean.CELL_SIZE, EntireBean.CELL_ROWS*EntireBean.CELL_SIZE);
  31.                 //加边界线
  32.                 upPanel.setBorder(new EtchedBorder(EtchedBorder.RAISED));
  33.                 //加入下面的面板,有一些按钮
  34.                 this.add(downPanel);
  35.                 //设置按钮面板的位置和大小
  36.                 downPanel.setBounds(5, EntireBean.CELL_ROWS*EntireBean.CELL_SIZE+15, 280, 250);
  37.                 //加边界线
  38.                 downPanel.setBorder(new EtchedBorder(EtchedBorder.RAISED));
  39.                 //downPanel的设为自由布局
  40.                 downPanel.setLayout(null);
  41.                 //创建几个按钮的对象
  42.                 startButton=new JButton();
  43.                 //设置按钮的大小
  44.                [color=Red] startButton.setBounds(15, EntireBean.CELL_ROWS*EntireBean.CELL_SIZE+15, 80, 50);[/color]这可能是出边界了, EntireBean.CELL_ROWS*EntireBean.CELL_SIZE+15[color=Red]看看是不是出了界面。[/color]
  45.                 //把按钮放到面板上
  46.                 downPanel.add(startButton);
  47.                 startButton.setText("开始游戏");
  48.                 overButton=new JButton();
  49.                 downPanel.add(overButton);
  50.                 overButton.setBounds(15, EntireBean.CELL_ROWS*EntireBean.CELL_SIZE+25, 80, 50);
  51.                 pauContiButton=new JButton();
  52.                 downPanel.add(pauContiButton);
  53.                 overButton.setBounds(15, EntireBean.CELL_ROWS*EntireBean.CELL_SIZE+35, 80, 50);
  54.                 this.setVisible(true);
  55.         }
  56.         //构造器
  57.         
  58.         
  59. }
复制代码
回复 使用道具 举报
找到了按钮的坐标设置出界了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马