首页>代码>java Swing编写扫雷游戏>/cleanMine/src/cleanMine/CleanMine.java
package cleanMine;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 * 扫雷游戏主界面
 * @author tony.tang
 *
 */
public class CleanMine extends JFrame implements ActionListener{
	private JLabel text1,text2;
	public static JLabel remainMine;//剩余地雷数
	private JLabel time;//消耗时间
	private JButton reset;//重新开始
	private JPanel center;
	private int row,col,mine;
	
	
	public CleanMine(){
		text1=new JLabel("剩余地雷:");
		text2=new JLabel("消耗时间:");
		remainMine=new JLabel("10");
		time=new JLabel("0");
		reset=new JButton("重新开始");
		reset.addActionListener(this);
		JMenuBar bar=new JMenuBar();
		JMenu game=new JMenu("游戏");
		JMenu help=new JMenu("帮助");
		JMenuItem item;
		game.add(item=new JMenuItem("开局"));item.addActionListener(this);
		game.addSeparator();
		ButtonGroup bg=new ButtonGroup();
		game.add(item=new JCheckBoxMenuItem("初级",true));bg.add(item);item.addActionListener(this);
		game.add(item=new JCheckBoxMenuItem("中级"));bg.add(item);item.addActionListener(this);
		game.add(item=new JCheckBoxMenuItem("高级"));bg.add(item);item.addActionListener(this);
		game.add(item=new JCheckBoxMenuItem("自定义..."));bg.add(item);item.addActionListener(this);
		game.addSeparator();
		game.add(item=new JMenuItem("退出"));item.addActionListener(this);
	
		help.add(item=new JMenuItem("查看帮助"));item.addActionListener(this);
		help.add(item=new JMenuItem("关于扫雷..."));item.addActionListener(this);
		
		bar.add(game);
		bar.add(help);
		
		this.setJMenuBar(bar);
		
		init();
	}
	
	private void init(){
		JPanel north=new JPanel();
		north.add(text1);
		north.add(remainMine);
		north.add(reset);
		north.add(text2);
		north.add(time);
		
		this.add(north,BorderLayout.NORTH);
		this.row=9;
		this.col=9;
		this.mine=10;
		restart();
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		new Thread(){
			public void run(){
				while(Integer.parseInt(remainMine.getText())>0){
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					time.setText((Integer.parseInt(time.getText())+1)+"");
				}
			}
		}.start();
	}
	
	
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals("初级")){
			this.row=9;
			this.col=9;
			this.mine=10;
			restart();
			return;
		}
		if(e.getActionCommand().equals("中级")){
			this.row=16;
			this.col=16;
			this.mine=40;
			restart();
			return;
		}
		if(e.getActionCommand().equals("高级")){
			this.row=16;
			this.col=30;
			this.mine=99;
			restart();
			return;
		}
		if(e.getActionCommand().equals("重新开始")){
			restart();
			return;
		}
	}

	private void restart(){
		if(center!=null){
			this.remove(center);
		}
		center=new AllButtonPanel(row,col,mine);
		this.add(center,BorderLayout.CENTER);
		this.remainMine.setText(mine+"");
		this.time.setText("0");
		this.setSize(col*30,row*30+10);
		this.setResizable(false);
		this.setVisible(true);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new CleanMine();

	}

}
最近下载更多
张德志  LV6 2023年12月27日
刘地带  LV11 2023年5月26日
luo110012  LV9 2023年5月16日
18973881965  LV1 2023年2月23日
微信网友_6194851142488064  LV1 2022年10月28日
zhkai163  LV5 2022年10月27日
rodert  LV14 2022年8月6日
java代写  LV7 2022年6月4日
liangge2115  LV27 2022年5月30日
flygrass  LV12 2022年4月8日
最近浏览更多
张德志  LV6 2023年12月27日
lshlsh 2023年12月25日
暂无贡献等级
COD824  LV1 2023年11月29日
koichenchen  LV1 2023年11月3日
吉萨VUK 2023年11月1日
暂无贡献等级
矿泉水  LV30 2023年8月11日
hougui  LV1 2023年6月20日
刘地带  LV11 2023年5月26日
luo110012  LV9 2023年5月16日
1739332236  LV1 2023年2月23日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友