⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 minesweeper.java

📁 a very good example of how to use java for a minesweeper game that is much like the windows one. Th
💻 JAVA
字号:
import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.util.*;/***designed & build by Grahame batte email: g.batte@lancaster.ac.uk*last modified on 19-05-03 *used in my minesweeper aplication*/public class MineSweeper extends JFrame implements ActionListener{		private JPanel options= new JPanel();/** *face displays the simle in its difrent forms and adds functions to the game such as new game */	public static JButton face = new JButton();	/** *one displays the units of the number of flags left digital display */	public static JButton one;/** *ten displays the tens of the number of flags left digital display */	public static JButton ten;/** *hund displays the hundreds of the number of flags left digital display */	public static JButton hund;/** *time1 displays the units of the amount of time past */	public static JButton time1;/** *time10 displays the tens of the amount of time past */	public static JButton time10;/** *time100 displays the hundreds of the amount of time past */	public static JButton time100;	private JPanel gui= new JPanel();		private String beg = "beginer";	private String in = "Intermediate";	private String exp = "Expert";	private String cus = "Custom";	private String Ab = "About";	private String pl = "play";		JMenuBar menuBar = new JMenuBar();	JMenu menu = new JMenu("Game");	JMenu help = new JMenu("help");	ButtonGroup group = new ButtonGroup();	ButtonGroup helpgroup = new ButtonGroup();	JRadioButtonMenuItem beginer = new JRadioButtonMenuItem("BEGINER   (9 rows, 9 columns, 10 mines)");	JRadioButtonMenuItem Intermediate = new JRadioButtonMenuItem("INTERMEDIATE    (16 rows, 16 columns, 30 mines)");	JRadioButtonMenuItem Expert = new JRadioButtonMenuItem("EXPET    (16 rows, 16 columns, 50 mines)");	JRadioButtonMenuItem Custom = new JRadioButtonMenuItem("Custom   (enter your own number of rows,columns and mines)");	JRadioButtonMenuItem About = new JRadioButtonMenuItem("About");	JRadioButtonMenuItem play = new JRadioButtonMenuItem("Instructions");/** *MineSweeper constructs and displays the menu options,the optins panel * then calls the game class the construct a game with x row y columns and z mines */	public MineSweeper()		{		 super("MineSweeper");		 //--------------menu bar--------------------------------------------------------		setJMenuBar(menuBar);		menu.setMnemonic(KeyEvent.VK_G);		menu.getAccessibleContext().setAccessibleDescription("Game");		menuBar.add(menu);		menuBar.add(help);//-----------------game options--------------------------------------------------				beginer.setMnemonic(KeyEvent.VK_B);		beginer.setActionCommand(beg);		beginer.setSelected(true);				Intermediate.setMnemonic(KeyEvent.VK_I);		Intermediate.setActionCommand(in);				Expert.setMnemonic(KeyEvent.VK_E);		Expert.setActionCommand(exp);				Custom.setMnemonic(KeyEvent.VK_C);		Custom.setActionCommand(cus);				group.add(beginer);		group.add(Intermediate);		group.add(Expert);		group.add(Custom);				menu.add(beginer);		menu.add(Intermediate);		menu.add(Expert);		menu.add(Custom);				beginer.addActionListener(this);		Intermediate.addActionListener(this);		Expert.addActionListener(this);		Custom.addActionListener(this);//-----------help option-------------------------------------------------------		About.setMnemonic(KeyEvent.VK_A);		About.setActionCommand(Ab);				play.setMnemonic(KeyEvent.VK_P);		play.setActionCommand(pl);					helpgroup.add(About);		helpgroup.add(play);							help.add(About);		help.add(play);				About.addActionListener(this);		play.addActionListener(this);		//-------------------frame settings and gui panel-------------------------------		 setSize(200,100);      	 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     	 setLocation(150,150);     	 setVisible(true);     	 setResizable(false);     //-----------------options panel------------------------------------------------	  				hund=new JButton(new ImageIcon("digits/0.gif"));					ten=new JButton(new ImageIcon("digits/0.gif"));					one=new JButton(new ImageIcon("digits/0.gif"));						  				time100=new JButton(new ImageIcon("digits/0.gif"));					time10=new JButton(new ImageIcon("digits/0.gif"));					time1=new JButton(new ImageIcon("digits/0.gif"));						  				options.setLayout(new GridLayout(1,7));	  				options.add(hund);	  				options.add(ten);	  				options.add(one);	  				options.add(face);	  				face.addActionListener(this);	  				options.add(time100);	  				options.add(time10);	  				options.add(time1);	  				      				ImageIcon smiley1 = new ImageIcon("images/smiley1.jpg"); 	  				face.setIcon(smiley1);	  								 				setContentPane(gui);	 				gui.setLayout(new BorderLayout());	 					 				gui.add(options,BorderLayout.NORTH);	 				setVisible(true);	 						}//end constructor	/** *ActionEvent e tells the menu what to do and when. *adds functions to the face button and calls the game class to creat a game  */			//--------------------action preformed------------------------------------------	public void actionPerformed(ActionEvent e)		{ //try{			if (e.getActionCommand().equals(pl))				{				 JOptionPane.showMessageDialog(this,"Minesweeper overview.");			   	 JOptionPane.showMessageDialog(this,"The object of Minesweeper is to locate all the mines as quickly as possible without uncovering any of them. If you uncover a mine, you lose the game.");				 JOptionPane.showMessageDialog(this,"Minesweeper overview.");				 JOptionPane.showMessageDialog(this,"1. On the Game menu, click New. ");				 JOptionPane.showMessageDialog(this,"2.To start the timer, click any square on the playing field. ");				 JOptionPane.showMessageDialog(this,"NOTE!!!");				 JOptionPane.showMessageDialog(this,"You can uncover a square by clicking it. If you uncover a mine, you lose the game.");				 JOptionPane.showMessageDialog(this,"If a number appears on a square, it indicates how many mines are in the eight squares that surround the numbered one.");				 JOptionPane.showMessageDialog(this,"To mark a square you suspect contains a mine, right-click it. ");				 JOptionPane.showMessageDialog(this,"The game area consists of the playing field, a mine counter, and a timer. ");				}// if					if (e.getActionCommand().equals(Ab))				{				JOptionPane.showMessageDialog(this,"this is my version of minesweeper"); //				JOptionPane.showMessageDialog(this,"if you what more info mail me at  g.batte@lancs.ac.uk");  // the about menu option					}// if//-----------------options stuuf face etc-------------------------------------------------------			if (e.getSource()==face)							// what to do if the face is cliked				{				 	 gui.removeAll();							//removes old game				 	 gui.repaint();						//paints new game ready for leval selection				 	 gui.add(options,BorderLayout.NORTH);				 	 ImageIcon smiley1 = new ImageIcon("images/smiley1.jpg");	 				 face.setIcon(smiley1);	 				 Square.GameOver=false;	 				 Square.flagused=0;	 				 Square.numclicks=0;				 					 	 hund.setIcon(new ImageIcon("digits/0.gif"));					 ten.setIcon(new ImageIcon("digits/0.gif"));					 one.setIcon(new ImageIcon("digits/0.gif"));						 					 time100.setIcon(new ImageIcon("digits/0.gif"));					 time10.setIcon(new ImageIcon("digits/0.gif"));					 time1.setIcon(new ImageIcon("digits/0.gif"));					}//end if(e.getSource()==face)				//--------------------beginner preformed------------------------------------------	 						if (e.getActionCommand().equals(beg))				{			 	 	 int row = 9;										//sets row columns and mines	 			 	 int column = 9;	 			 	 int mines = 10;	 			 	 this.resize(22*row,22*column+30);	 			 	 game one = new game (row,column,mines);// calls the game class to display a game with x rows y columns and z mines	 			 	 gui.add(one,BorderLayout.CENTER);	 			 	 setVisible(true);	 			}// if//------------Intermediate game panel------------------------------------------- 				 		 if (e.getActionCommand().equals(in))				{			 	 	 int row = 16;										//sets row columns and mines	 			 	 int column = 16;	 			 	 int mines = 40;	 			 	 this.move(100,100);	 			 	 this.resize(22*row,22*column+30);	 			 	 game one = new game (row,column,mines);// calls the game class to display a game with x rows y columns and z mines	 			 	 gui.add(one,BorderLayout.CENTER);	 			 	 setVisible(true); 		 	 			}// end if// -------------------Expert game panel----------------------------------------- 				 	     if (e.getActionCommand().equals(exp))				{			 	 	 int row = 16;										//sets row columns and mines	 			 	 int column = 16;	 			 	 int mines = 40;	 			 	 this.move(20,20);	 			 	 this.resize(22*row,22*column+30);	 			 	 game one = new game (row,column,mines);// calls the game class to display a game with x rows y columns and z mines	 			 	 gui.add(one,BorderLayout.CENTER);	 			 	 setVisible(true);	 			 	 	 					 			}// end if// -------------------Custom game panel----------------------------------------- 				 	     if (e.getActionCommand().equals(cus))				{				 String input = JOptionPane.showInputDialog("How many rows do you want?");// shows input box asking for number of rows      			 int row = Integer.parseInt(input);										// sets the number entered equal to row      							 input = JOptionPane.showInputDialog("How many columns do you want?");	// shows input box asking for number of columns      			 int column = Integer.parseInt(input);									// sets the number entered equal to columns      			      			 input = JOptionPane.showInputDialog("How many mines do you want?");	// shows input box asking for number of mines      			 int mines = Integer.parseInt(input);									// sets the number entered equal to mines	 				 			 this.move(2*row,2*column);	 			 this.resize(22*row,22*column+30);	 			 game one = new game (row,column,mines);// calls the game class to display a game with x rows y columns and z mines	 			 gui.add(one,BorderLayout.CENTER);	 			 setVisible(true);	 			 	 			 }// end if	 //	}catch(Exception ex){}			}// end action preformed			//---------------------mian method----------------------------------------------	/** * the main methoud to run the program. */			public static void main(String [] args)    		{        MineSweeper x = new MineSweeper();										} //end main method}//end class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -