1、package sweeper;import java.awt.BorderLayout;import java.awt.FlowLayout;import java.awt.GridLayout;import java.awt.Insets;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing
2、.JFrame;import javax.swing.JLabel;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JOptionPane;import javax.swing.JPanel;public class JMine extends JFramepublic int col = 10;/扫雷面板的列数public int row = 10;/扫雷面板好行数private int mineNum = 10;/地雷的个数private
3、 int mineRemainNum = mineNum;JButton innitBoard = new JButtonrowcol;/初始化面板char board = new charrowcol;private int isClick = new introwcol;/用于记录按钮是否被点击过,点击过赋值为1,否则为 0private int count = new introwcol;/统计每个坐标周围地雷的个数private boolean ismine = false;/判断是否踩到地雷private boolean finish_click = true;private boo
4、lean iswin = true ;private int flagnum = 0;/记录插在地雷上小旗个个数private MyThread mythread = new MyThread();private static int time_start = 0;private static int time =0;/用于计时JLabel remain1 = new JLabel();/4 个标签显示剩余地雷个数;JLabel remain2 = new JLabel(new ImageIcon(mineRemainNum/100)+“.png“);JLabel remain3 = new
5、JLabel(new ImageIcon(mineRemainNum/10)+“.png“);JLabel remain4 = new JLabel(new ImageIcon(mineRemainNum%10)+“.png“);static JLabel time1 = new JLabel(new ImageIcon(“0.png“);static JLabel time2 = new JLabel(new ImageIcon(“0.png“);static JLabel time3 = new JLabel(new ImageIcon(“0.png“);JButton restart;p
6、ublic void innit()this.setSize(400,450);this.setVisible(true);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);JPanel panel = new JPanel(new BorderLayout();/主面板this.setContentPane(panel);layMines(); /随机布雷JMenuBar bar = createBar(); /调用创建菜单栏的方法this.setJMenuBar(bar); /与面板的添加的菜单栏无关,是窗体里面自带的添加菜单的方法JP
7、anel mineBoard = createBoard(); /调用创建扫雷主面板的方法JPanel state = createState(); /调用创建状态栏的方法panel.add(mineBoard,BorderLayout.CENTER);panel.add(state,BorderLayout.NORTH);for(int i=0;i=0innitBoardij.setEnabled(false);move(i,j);elseisClickij = 1;innitBoardij.setIcon( new ImageIcon(count(i,j)+“.gif“);public v
8、oid isWin()for(int i =0;iisClick.length;i+)for(int j =0;jisClicki.length;j+)if(isClickij=0)finish_click = false;break;else finish_click = true;if(finish_clickJOptionPane.showMessageDialog(JMine.this, “恭喜你,胜利了!“);mythread.stop();class MyThread extends ThreadOverridepublic void run() try while(true)ti
9、me+;JMine.time1.setIcon(new ImageIcon(time/100+“.png“);JMine.time2.setIcon(new ImageIcon(time%100/10+“.png“);JMine.time3.setIcon(new ImageIcon(time%10+“.png“);Thread.sleep(1000); catch (InterruptedException e) / TODO Auto-generated catch blocke.printStackTrace();public static void main(String args) JMine mine = new JMine();mine.innit();