首页>代码>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();

	}

}
最近下载更多
刘地带  LV11 5月26日
luo110012  LV9 5月16日
18973881965  LV1 2月23日
微信网友_6194851142488064  LV1 2022年10月28日
zhkai163  LV5 2022年10月27日
rodert  LV14 2022年8月6日
java代写  LV7 2022年6月4日
liangge2115  LV26 2022年5月30日
flygrass  LV12 2022年4月8日
四季夏目  LV6 2022年3月19日
最近浏览更多
刘地带  LV11 5月26日
luo110012  LV9 5月16日
1739332236  LV1 2月23日
wwfl02  LV2 2022年12月16日
wdasfgas 2022年11月27日
暂无贡献等级
微信网友_6195329201262592  LV1 2022年10月28日
微信网友_6194851142488064  LV1 2022年10月28日
zhkai163  LV5 2022年10月27日
微信网友_6193867173089280 2022年10月27日
暂无贡献等级
微信网友_6190641661054976  LV2 2022年10月26日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友