Cleanmine.java
Package Xjj;import Java.awt.borderlayout;import Java.awt.dimension;import java.awt.event.actionevent;import Java.awt.event.actionlistener;import Javax.swing.buttongroup;import Javax.swing.jbutton;import Javax.swing.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;import Javax.swing.jradiobuttonmenuitem;public class Cleanmine extends JFrame implements actionlistener{public static int result,shownumb;//result indicates that the mark is the number of mines, And Shownumb indicates the number of points opened private Boolean IsFirst = true;//settings must click New Game to start the game private JLabel jlbtime,jlbremain;public static JLabel time;//time public static JLabel remainmine;//number of remaining mines private JButton start;private JPanel jpane;public static int Row,col, minenumber;//, rows and columns, and total number of mines private Dimension dim;private jradiobuttonmenuitem[] difficult;//menu item Public cleanmine () {Super ("Minesweeper");d Im=this.gettoolkit (). Getscreensize ();//Get screen resolution this.setlocation (DIM.WIDTH*2/5, DIM.HEIGHT/3);Set the position, make the humanized change according to the difficulty degree this.setdefaultcloseoperation (exit_on_close); AddMenu (); jlbremain=new JLabel ("Residual mines:"); time= New JLabel ("ten"); remainmine=new JLabel ("0"); jlbtime=new JLabel ("Time-consuming:"); Start=new JButton ("New Game"); Start.setactioncommand ("new"); Start.addactionlistener (this); JPanel p=new JPanel ();p. Add (Jlbremain);p. Add (Remainmine);p. Add (Start);p. Add (Jlbtime);p. Add (Time), This.add (P, Borderlayout.north); This.row=9;this.col=9;this.minenumber=10;restart ();} private void Restart () {if (jpane!=null) {this.remove (jpane);} Jpane=new Minepanel (Row,col,minenumber,isfirst); This.add (Jpane,borderlayout.center); This.remainMine.setText ( Minenumber+ ""); This.time.setText ("0"); This.setsize (col*30, row*30+10); this.setresizable (false); This.setvisible ( true);} public void AddMenu () {JMenuBar jbar=new jmenubar (); This.setjmenubar (Jbar); JMenu game = new JMenu ("Games"); JMenu helper = new JMenu ("help"); JMenuItem jitemexit=new JMenuItem ("exit"); Jitemexit.setactioncommand ("Exit"); Buttongroup bgp=new Buttongroup (); String[] difficults={"complex", "medium"," "Simple"};d ifficult=new jradiobuttonmenuitem[difficults.length];for (int i=difficults.length-1;i>=0;i--) { Difficult[i]=new JRadioButtonMenuItem (difficults[i],true);d Ifficult[i].addactionlistener (this); Bgp.add ( Difficult[i]); Game.add (Difficult[i]);} Game.addseparator (); Jitemexit.addactionlistener (this); Game.add (jitemexit); Jbar.add (game); JMenuItem jitemview=new JMenuItem ("about Minesweeper") Jitemview.setactioncommand ("View"); Jitemview.addactionlistener (this); Help.add (Jitemview); Jbar.add (Help);} public static void Main (string[] args) {new Cleanmine ();} public void actionperformed (ActionEvent e) {if (E.getactioncommand (). Equalsignorecase ("new")) {this.result=0; This.shownumb=0;this.isfirst=false;restart (); return;} if (E.getactioncommand (). Equals ("simple")) {this.row=9;this.col=9;this.minenumber=10;this.setlocation (DIM.WIDTH*2/5, DIM.HEIGHT/3); restart (); return;} if (E.getactioncommand (). Equals ("medium")) {this.row=16;this.col=16;this.minenumber=40;this.setlocation (dim.width*2/ 5, DIM.HEIGHT/5); restart (); return;} if (E.getacTioncommand (). Equals ("complex")) {this.row=16;this.col=30;this.minenumber=99;this.setlocation (DIM.WIDTH/7, Dim.height /7); restart (); return;} if (E.getactioncommand (). Equalsignorecase ("Exit")) {system.exit (0);} if (E.getactioncommand (). Equalsignorecase ("View") {Joptionpane.showmessagedialog (this, "Rules for Minesweeper: \n\ T find all the minefields in the minefield as soon as possible without stepping on the mines. The "+" point number is a few, \ n indicates that there are several 8 locations next to the number of thunder, if the mine is dug, will lose the game. ");}}}
Minepanel.java
Package Xjj;import Java.awt.color;import Java.awt.gridlayout;import java.awt.event.actionevent;import Java.awt.event.actionlistener;import Java.awt.event.mouseadapter;import Java.awt.event.mouseevent;import Javax.swing.jbutton;import Javax.swing.joptionpane;import Javax.swing.jpanel;public class MinePanel extends JPanel Implements Actionlistener{private boolean isfirst;private int row,col,minenumber;private jbutton[][] mine;private int[ [] Mark; Thread t;public minepanel (int row,int col,int Minenumber,boolean isFirst) {This.isfirst=isfirst;this.row=row;this.col =col;this.minenumber=minenumber;mine=new Jbutton[this.row][this.col];mark=new Int[this.row][this.col]; This.setlayout (New GridLayout (Row,col)); Createmine (); createbuttons (); if (IsFirst) {firstinist ();} Else{init ();//Initialize}}private void Firstinist () {for (int. i=0;i<mine.length;i++) {for (int j=0;j<mine[i].length;j++ {mine[i][j]=new JButton (); This.add (Mine[i][j]);}} private void Init () {this.setlayout (new GridLayout (Row,col)); for (int i=0;i<mine.length;i++) {for (int j=0;j<mine[i].length;j++) {this.add (mine[i][j]);}}} private void Createbuttons () {for (int. i=0;i<mine.length;i++) {for (int j=0;j<mine[i].length;j++) {mine[i][j]=new Minebutton (I,J); Mine[i][j].setsize (6, 6); Mine[i][j].addactionlistener (this); Mine[i][j].addmouselistener (new Mouseadapter () {public void mouseclicked (MouseEvent e) {if (E.getbutton () ==mouseevent.button3) {int remain= Integer.parseint (CleanMine.remainMine.getText ()); JButton b= (JButton) E.getsource (); if (B.gettext (). Equals ("") &&remain>0) {cleanmine.result++;// System.out.println (cleanmine.shownumb+ "" +cleanmine.result); if (cleanmine.result==cleanmine.row* Cleanmine.col-cleanmine.shownumb) {Joptionpane.showmessagedialog (b, "Congratulations! Long live XJJ! ");} remain--; CleanMine.remainMine.setText (remain+ ""); B.settext ("$");} else if (B.gettext (). Equals ("$")) {cleanmine.result--; System.out.println (cleanmine.shownumb); remain++; CleanMine.remainMine.setText (remain+ ""); B.settext ("");}}});}}private void Createmine () {int N=0;while (n<minenumber) {//randomly generates a specified number of positions for the LEI's position int i= (int) (Math.random () *row); int j= (int ) (Math.random () *col); if (mark[i][j]!=-1) {mark[i][j]=-1;n++;}} for (int i=0;i<mark.length;i++) {//map mark for (int j=0;j<mark[i].length;j++) {if (mark[i][j]!=-1) {mark[i][j]= Getsurroundminenumber (MARK,I,J);}}} private int Getsurroundminenumber (int[][] mark2, int n, int m) {int numb=0;int[] direct={0,1,-1};int x,y;for (int i=0;i< direct.length;i++) {for (int j=0;j<direct.length;j++) {if (i==0&&j==0) {continue;} X=n+direct[i];y=m+direct[j];if (x>=0 && x<mark2.length && y>=0 && y<mark2[0]. Length) {//traverse the surrounding eight directions numb+= (Mark2[x][y]==-1 1:0);}} return numb;} public void actionperformed (ActionEvent e) {Minebutton b= (Minebutton) e.getsource (); int r=b.getrow (); int C=b.getcol (); if (mark[r][c]==-1) {//Step on Ray, show all the maps//string str=cleanmine.time.gettext ();//isfirst=true; CleanMine.time.setText ("0"); for (int. i=0;i<mark.length;i++) {for (int j=0;j<mark[i].length;j++) {if (mark[i][j]==-1) {Mine[i][j].settext ("@");} else if (mark[i][j]==0) {Mine[i][j].settext (""); Mine[i][j].setbackground (Color.green);} Else{mine[i][j].settext (mark[i][j]+ ""); Mine[i][j].setbackground (Color.green);}} Joptionpane.showmessagedialog (This, "called you minesweeper, not stepping thunder, the game is over!" ");} Else{showempty (MARK,R,C);}} private void Showempty (int[][] mark2, int r, int c) {Minebutton b= (Minebutton) mine[r][c];if (b.iscleared ()) {return;} Cleanmine.shownumb++;//system.out.println (CLEANMINE.SHOWNUMB); if (cleanmine.result==cleanmine.row* Cleanmine.col-cleanmine.shownumb) {Joptionpane.showmessagedialog (this, "Congratulations! xjj!");} int x,y;int[] Direct={0,1,-1};if (mark[r][c]==0) {//If it is empty, display it and continue traversing b.setbackground (Color.green); B.setcleared (True ); for (int i=0;i<direct.length;i++) {for (int j=0;j<direct.length;j++) {if (i==0&&j==0) {continue;} if (B.gettext (). Equals ("$")) {continue;} X=r+direct[i];y=c+direct[j];if (x>=0 && x<mark2.length && y>=0 && y<mark2[0]. Length) {//cleanmine.shownumb++;showempty (Mark2,x,y);}}} Else{if (B.gettext (). Equals ("$")) {//If it is marked as Lei, return is not displayed;} B.settext (mark[r][c]+ "");//If a number is displayed, the number is shown B.setbackground (Color.green); b.setcleared (True);}}
Minebutton.java
Package Xjj;import Java.awt.insets;import Javax.swing.jbutton;public class Minebutton extends JButton {private int row, Col;private boolean cleared=false;public Minebutton (int row, int col) {This.row=row;this.col=col;this.setmargin (new Insets (0,0,0,0));} public int Getcol () {return col;} public int GetRow () {return row;} public Boolean iscleared () {return cleared;} public void Setcleared (Boolean cleared) {this.cleared = cleared;}}
Minesweeper (Java graphical interface good example)