package snake;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
/**
* <p>Title: 贪食蛇游戏</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: </p>
* @author zsb
* @version 1.0
*/
public class MainFrame extends JFrame {
private JPanel contentPane;//窗体内容网格
private JToolBar jToolBar1 = new JToolBar();//工具栏
private JButton jButton1 = new JButton();//游戏开始按钮
private JButton jButton2 = new JButton();//游戏暂停按钮
private JButton jButton3 = new JButton();//游戏退出按钮
private JButton jButton4 = new JButton();//帮助按钮
private JPanel jPanel1 = new JPanel();//游戏主体面板容器
private JPanel jPanel_PlayArea = new JPanel();//游戏区面板容器
private XYLayout xYLayout1 = new XYLayout();//容器布局管理器
private XYLayout xYLayout2 = new XYLayout();
private XYLayout xYLayout3 = new XYLayout();
private static final int UP = 1, LEFT = 2, DOWN = 3, RIGHT = 4;//贪食蛇运动方向
private static final int BEGINNER = 1, MIDDLE = 2, EXPERT = 3;//游戏级别常量
private static final int ROWS = 30;//游戏区行数
private static final int COLS = 50;//游戏区列数
private boolean isPause = false;//游戏暂停标志
private boolean isEnd;//游戏结束标志
private SnakeBody snake ;//贪食蛇
private int score = 0;//当前得分
private int level = BEGINNER;//当前游戏级别
SnakeThread thread = new SnakeThread();//游戏主线程
private GridLayout gridLayout1 = new GridLayout(ROWS, COLS, 0, 0);//游戏区布局
private JButton[][] playBlocks;//游戏区的所有方块
JPanel jPanel2 = new JPanel();
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JLabel jLabel3 = new JLabel();
JLabel jLabel4 = new JLabel();
JLabel jLabel5 = new JLabel();
JLabel jLabel6 = new JLabel();
JLabel jLabel7 = new JLabel();
ButtonGroup buttonGroup1 = new ButtonGroup();
JRadioButton jRadioButton1 = new JRadioButton();
JRadioButton jRadioButton2 = new JRadioButton();
JRadioButton jRadioButton3 = new JRadioButton();
//Construct the frame
public MainFrame() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
//setIconImage(Toolkit.getDefaultToolkit().createImage(MainFrame.class.getResource("[Your Icon]")));
contentPane = (JPanel)this.getContentPane();
contentPane.setLayout(xYLayout2);
this.setResizable(false);
this.setSize(new Dimension(512, 414));
this.setTitle("贪食蛇");
this.addKeyListener(new MainFrame_this_keyAdapter(this));
jButton1.setFont(new java.awt.Font("DialogInput", 0, 12));
jButton1.setMaximumSize(new Dimension(79, 39));
jButton1.setMinimumSize(new Dimension(79, 39));
jButton1.setPreferredSize(new Dimension(79, 39));
jButton1.setFocusPainted(false);
jButton1.setText("开始");
jButton1.addActionListener(new MainFrame_jButton1_actionAdapter(this));
jButton1.addKeyListener(new MainFrame_this_keyAdapter(this));
jButton2.setText("暂停");
jButton2.addActionListener(new MainFrame_jButton2_actionAdapter(this));
jButton2.setPreferredSize(new Dimension(79, 39));
jButton2.setFocusPainted(false);
jButton2.setMinimumSize(new Dimension(79, 39));
jButton2.setMaximumSize(new Dimension(79, 39));
jButton2.setFont(new java.awt.Font("DialogInput", 0, 12));
jButton2.addKeyListener(new MainFrame_this_keyAdapter(this));
jButton3.setText("退出");
jButton3.addActionListener(new MainFrame_jButton3_actionAdapter(this));
jButton3.setPreferredSize(new Dimension(79, 39));
jButton3.setFocusPainted(false);
jButton3.setMinimumSize(new Dimension(79, 39));
jButton3.setMaximumSize(new Dimension(79, 39));
jButton3.setFont(new java.awt.Font("DialogInput", 0, 12));
jButton3.addKeyListener(new MainFrame_this_keyAdapter(this));
jButton4.setText("帮助");
jButton4.addActionListener(new MainFrame_jButton4_actionAdapter(this));
jButton4.setPreferredSize(new Dimension(79, 39));
jButton4.setFocusPainted(false);
jButton4.setMinimumSize(new Dimension(79, 39));
jButton4.setMaximumSize(new Dimension(79, 39));
jButton4.setFont(new java.awt.Font("DialogInput", 0, 12));
jButton4.addKeyListener(new MainFrame_this_keyAdapter(this));
jPanel1.setLayout(xYLayout1);
jPanel1.addKeyListener(new MainFrame_this_keyAdapter(this));
jPanel_PlayArea.setLayout(gridLayout1);
jPanel_PlayArea.setBorder(BorderFactory.createEtchedBorder());
jPanel_PlayArea.addKeyListener(new MainFrame_this_keyAdapter(this));
jLabel1.setFont(new java.awt.Font("DialogInput", 0, 12));
jLabel1.setText("得分:");
jPanel2.setLayout(xYLayout3);
jLabel2.setFont(new java.awt.Font("DialogInput", 0, 12));
jLabel2.setToolTipText("");
jLabel2.setText("0");
jLabel3.setText("穿身:");
jLabel3.setFont(new java.awt.Font("DialogInput", 0, 12));
jLabel4.setFont(new java.awt.Font("DialogInput", 0, 12));
jLabel4.setText("0");
jLabel5.setText("穿墙:");
jLabel5.setFont(new java.awt.Font("DialogInput", 0, 12));
jLabel5.setToolTipText("");
jLabel6.setText("0");
jLabel6.setFont(new java.awt.Font("DialogInput", 0, 12));
jLabel6.setToolTipText("");
jRadioButton1.setFont(new java.awt.Font("DialogInput", 0, 12));
jRadioButton1.setText("初级");
jRadioButton1.addActionListener(new MainFrame_jRadioButton1_actionAdapter(this));
jRadioButton1.addKeyListener(new MainFrame_this_keyAdapter(this));
jRadioButton2.setText("中级");
jRadioButton2.addActionListener(new MainFrame_jRadioButton2_actionAdapter(this));
jRadioButton2.addKeyListener(new MainFrame_this_keyAdapter(this));
jRadioButton2.setFont(new java.awt.Font("DialogInput", 0, 12));
jRadioButton3.setText("高级");
jRadioButton3.addActionListener(new MainFrame_jRadioButton3_actionAdapter(this));
jRadioButton3.setFont(new java.awt.Font("DialogInput", 0, 12));
jRadioButton3.addKeyListener(new MainFrame_this_keyAdapter(this));
buttonGroup1.add(jRadioButton1);
buttonGroup1.add(jRadioButton2);
buttonGroup1.add(jRadioButton3);
jRadioButton1.setSelected(true);
jLabel7.setFont(new java.awt.Font("DialogInput", 0, 12));
jLabel7.setText("等级:");
contentPane.add(jToolBar1, new XYConstraints(0, 0, 512, -1));
jToolBar1.add(jButton1, null);
jToolBar1.add(jButton2, null);
jToolBar1.add(jButton3, null);
jToolBar1.add(jButton4, null);
jToolBar1.addKeyListener(new MainFrame_this_keyAdapter(this));
contentPane.add(jPanel1, new XYConstraints(0, 43, 512, 371));
jPanel1.add(jPanel_PlayArea, new XYConstraints(0, 33, 524, 314));
jPanel1.add(jPanel2, new XYConstraints(0, 0, 498, 30));
jPanel2.add(jRadioButton1, new XYConstraints(331, 1, -1, -1));
jPanel2.add(jRadioButton3, new XYConstraints(443, 1, -1, -1));
jPanel2.add(jRadioButton2, new XYConstraints(390, 1, -1, -1));
jPanel2.add(jLabel6, new XYConstraints(245, 6, 30, -1));
jPanel2.add(jLabel7, new XYConstraints(287, 5, 47, -1));
jPanel2.add(jLabel5, new XYConstraints(197, 6, 47, -1));
jPanel2.add(jLabel4, new XYConstraints(166, 7, 26, -1));
jPanel2.add(jLabel3, new XYConstraints(120, 6, 38, -1));
jPanel2.add(jLabel1, new XYConstraints(23, 6, 39, -1));
jPanel2.add(jLabel2, new XYConstraints(62, 6, 54, -1));
//创建并初始化游戏区方块
playBlocks = new JButton[ROWS][COLS];
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
playBlocks[i][j] = new JButton();
playBlocks[i][j].setBackground(Color.lightGray);
playBlocks[i][j].setVisible(false);
jPanel_PlayArea.add(playBlocks[i][j]);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
贪食蛇游戏,帮助熟悉Java基础的
共23个文件
class:13个
java:3个
dep2:1个
需积分: 0 17 下载量 26 浏览量
2008-11-14
20:04:44
上传
评论
收藏 29KB RAR 举报
温馨提示
java课程设计经常做的项目,贪食蛇游戏
资源详情
资源评论
资源推荐
收起资源包目录
.rar (23个子文件)
贪食蛇游戏
Snake.jpx~ 0B
Snake.jpx 4KB
Snake.jpx.local~ 2KB
bak
src
snake
MainFrame.java 20KB
Help_Dialog.java 2KB
MainFrame.java~1~ 3KB
MainFrame.java~2~ 4KB
Snake.java 1KB
Snake.jpx.local 2KB
classes
snake
Snake.class 1KB
MainFrame$snakeThread.class 1KB
MainFrame$MainFrame_jButton1_actionAdapter.class 828B
MainFrame_jRadioButton3_actionAdapter.class 680B
MainFrame_jRadioButton1_actionAdapter.class 680B
MainFrame_jRadioButton2_actionAdapter.class 680B
MainFrame_jButton2_actionAdapter.class 665B
MainFrame$MainFrame_this_keyAdapter.class 759B
MainFrame$SnakeBody.class 4KB
MainFrame.class 11KB
MainFrame_jButton3_actionAdapter.class 665B
Help_Dialog.class 3KB
MainFrame_jButton4_actionAdapter.class 665B
package cache
snake.dep2 10KB
共 23 条
- 1
ligonghaozi
- 粉丝: 1
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0