文件名称:连连看JAVA代码
文件大小:15KB
文件格式:JAVA
更新时间:2015-04-28 11:42:30
连连看
java连连看代码
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.*;
public class Game extends JFrame{
private int num[][]; //实现数组
private JButton gamebutton[][]; //游戏Button组
private Container cont; //内容面板
private JLabel timelabel; //时间标签
private JButton play; //开始游戏按钮
private JButton exit; //退出按钮
private JButton replay; //重新开始按钮
private JButton back; //返回主界面按钮
private JComboBox choice;//游戏等级
private JButton chongle; //游戏按钮重列
private JPanel gamepanel; //游戏按钮面板
private JPanel panel; //开始游戏面板
private int time=0; //时间记录
private Time T; //记时线程
private Thread t=new Thread(T); //记时线程
private int shu=1; //记录获取游戏按钮事件的个数
private int x=0,y=0,a=0,b=0;//按钮的坐标
private JButton button1=null,button2=null;//点击了的按钮
private boolean can;//能否消除
private int lvx,lvy; //游戏等级数组标列数
private int framex,framey; //框架大小
private int again=1;//记录重列次数
public Game(){
itincomponent();
}
private void itincomponent(){
//设置面板属性
if(cont!=null)
cont.removeAll();
this.setSize(500, 500);
this.setLocationRelativeTo(null);
//获取内容面板
cont=this.getContentPane();
//开始和退出按钮
panel=new JPanel();
play=new JButton("开始游戏");
choice=new JComboBox();
choice.addItem("低级");
choice.addItem("中级");
choice.addItem("高级");
choice.setSelectedItem("低级");
exit=new JButton("退出游戏");
panel.add(choice);
panel.add(play);
panel.add(exit);
cont.add(panel,"South");
exit.addActionListener(new Exit());//结束事件监听
play.addActionListener(new Play());//开始事件监听
this.setResizable(false);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
////获得新的面板
private void p(){
//设置面板属性
this.setSize(framex, framey);
this.setLocationRelativeTo(null);
//获取内容面板
cont=this.getContentPane();
//开始和退出按钮
panel=new JPanel();
exit=new JButton("退出游戏");
replay=new JButton("重新开始");
back=new JButton("返回主界面");
chongle=new JButton("重列");
panel.add(chongle);
panel.add(replay);
panel.add(exit);
panel.add(back);
cont.add(panel,"South");
exit.addActionListener(new Exit());//结束事件监听
replay.addActionListener(new Playbutton());//重新开始事件监听
back.addActionListener(new Back());//返回主界面事件监听
chongle.addActionListener(new Chongle());//重列事件监听
//时间面板
timelabel=new JLabel("游戏时间:"+time+"s",JLabel.CENTER);
cont.add(timelabel,"North");
//生成游戏操作面板
gamepanel=new JPanel();
gamepanel.setLayout(new GridLayout(lvx,lvy,3,3)); //设置为表格布局
for (int i = 0; i < lvx; i++) {
for (int j = 0; j