Java编写的扫雷小程序_第1页
Java编写的扫雷小程序_第2页
Java编写的扫雷小程序_第3页
Java编写的扫雷小程序_第4页
Java编写的扫雷小程序_第5页
已阅读5页,还剩15页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、new 一个新的Gamedise2dise6kbiaoji整 个 程 序 使 用 了 4 个 类Game用于启动程序,界面的布置,各种行列的算法。Exte ndButt on继承了 Jbutton 用于新建butt on的各种属性ShowNum监听到某个button后,计算出此button周围有几个雷,不同的雷对应不通的 图片,类得到butt on上应加载的图片地址ViewReSet此类用于新建一个窗口,用户输入自己想要的行、列、雷数,然后用新的行列雷数运行类创建游戏主窗体*/package com.dhl.saolei;import java.awt.BorderLayout;import

2、java.awt.Color;import java.awt.C ontainer;import java.awt.Dime nsio n;import java.awt.F ont;import java.awt.GridLayout;import java.awt.Toolkit;import java.awt.eve nt.Acti on Eve nt; import java.awt.eve nt.Actio nListe ner; import java.awt.eve nt. In putEve nt;import java.awt.eve nt.KeyEve nt; import

3、 java.awt.eve nt.MouseAdapter; import java.awt.eve nt.MouseEve nt;import javax.sw in g.JFrame;import javax.swi ng.JLabel;import javax.swi ng.JMe nu;import javax.swi ng.JMe nuBar; import javax.swi ng.JMe nultem;import javax.swi ng.JOptio nPane; import javax.swi ng.JPa nel;import javax.swing.JToolBar;

4、import javax.swing.KeyStroke;import javax.swing.Timer;public class Game extends JFrameprivateContainermyContainerprivateGridLayout myLayout ;implementsActionListenerprivate private private private private private private private private privateJPanel myPanelMain ; JPanel myPanel ;JToolBar myTool ; J

5、Label time ;JLabel labelTime ;JLabel bombNum;JLabel labelBombNum JMenuBar menuBar ;JMenu menuE, menuH; JMenuItem menuItemCZmenuItemSZprivateint numBomb,countTime ,leaveBombprivateint myRows ,myColumns ;privateint xis , yisJprivateTimer timer ;privateExtendButtonmyButton= null ;/ 初始化窗体private void In

6、itView() numEnd ;/ 菜单初始化this. menuBar= new JMenuBar();this. menuE =new JMenu( 菜单 (E)this. menuH =new JMenu( 帮助 (H)this . menuItemCZ = new JMenuItem();); 重置 );ent. VK_C, InputEvent.CTRL_MASK);this. menuItemCZ.addActionListener(thisthis. menuE .add(this . menuItemCZ );1this. menuItemSZ= new JMenuItem(

7、 设置this . menuItemCZ .setAccelerator(KeyStroke.););getKeyStroke(KeyEvgetKeyStrokethis );(KeyEvthis . menuItemCZ .setAccelerator(KeyStroke. ent. VK_S, InputEvent. CTRL_MASK);this . menuItemSZ .addActionListener(this . menuE .add( this . menuItemSZ ); this . menuBar .add( this . menuE); this . menuBar

8、 .add( this . menuH); setJMenuBar( this . menuBar );/ 时间信息初始化this . myTool = new JToolBar();this . myTool .setLayout( this . myTool .setFloatable( BorderLayout borderLayout = new GridLayout(1, 4); false );new BorderLayout();this . myPanelMain = new JPanel(borderLayout);this . labelTime = new JLabel(

9、 时间: );this . time = new JLabel();this . time .setForeground(Color. RED);PLAIN , 20);this . time .setFont( new Font( font , Font.this . labelBombNum = new JLabel( 雷数: ); this . bombNum = new JLabel();valueOf ( this . numBomb);thisthis. bombNum.setFont(new Font( font . labelTime );. myTool.add(thisth

10、is. myTool.add(this. time );this. myTool.add(this. labelBombNum )this. myTool.add(this. bombNum);this . bombNum.setText(String.this . bombNum.setForeground(Color. RED);, Font.this . myPanelMain .add( this . myTool , borderLayout.PLAIN , 20);NORTH);/ 初始化 myButtonprivate void InitButton() BorderLayout

11、 borderLayout = new BorderLayout(); this . leaveBomb = this . numBomb;this . numEnd = ( this . myRows * this . myColumns ) -this . leaveBombthisthis. myContainer = this .getContentPane();. myColumns ,. myLayout = new GridLayout( this . myRows , this1, 1);thisthis this this this this this. myPanel =

12、new JPanel(); . myPanel .setVisible( . myPanel .setLayout( . myPanelMain . myContainer . myButton =.add( .add( newtrue );myLayout );this . myPanel , borderLayout. this . myPanelMain );ExtendButton( this . myRows *CENTER);. myColumns );for ( int i = 0; i ( this . myButton this . myButton this . myBut

13、tonthis . myRows * this . myColumns ); i+) new ExtendButton();i =i.SetPostion(i);i.setIcon(newShowNum(10).getImageIcon();this . myButton this . myButton this . myButton / 设置右键监听标记雷i.setButton_num(0);i.SetVisited( false ); i.SetStatus( false );this . myButton i.addMouseListener(new MouseAdapter() pub

14、lic void mouseClicked(MouseEve nt evt) if (evt.getModifiers()=In putEve nt.BUTTON3_MASK) int n 二(Exte ndButto n)evt.getSource().getButton_num();if (n = 1) -if (!(ExtendButton)evt.getSource().GetVisited() (Exte ndButton )evt.getSource().setButton_nu m(0);(Exte ndButt on )evt.getSource().setlc on (new

15、ShowNum(10).getImageIco n();leaveBomb +;bombNum.setText(String.valueOf ( leaveBomb ); else if (!(ExtendButton)evt.getSource().GetVisited() (Exte ndButton )evt.getSource().setButton_nu m(1);(Exte ndButt on )evt.getSource().setlc on (newShowNum(11).getImageIco n();leaveBomb -;bombNum.setText(String.va

16、lueOf ( leaveBomb ););this . myPanel .add( this . myButton i);this . myButton i.addActionListener(this );System. gc ();this .SetBomb( this . numBomb);show();/时钟设置int delay = 1000;/创建一个监听事件Acti on Liste ner drawClock =new Actio nListe ner()public void actio nPerformed(Actio nEve nt evt)countTime +; t

17、ime .setText(String. repaint();valueOf ( countTime );/ 创建一个时间计数器,每一秒触发一次this . timer = new Timer(delay,drawClock); this . timer .start();/ 重新初始化方法( inti = 0; i (this .myRows * thisthis .myButtoni.SetVisited(false )this .myButtoni.SetStatus(false );this .myButtoni.setText(null );this .myButtoni.setIc

18、on(new. myColumns ); i+) private void SetExtendButton() forthis.SetBomb( numBomb);this. countTime = 0;this. timer .start();this. leaveBomb = numBomb;this. bombNum.setText(String.valueOf ( this . numBomb);this/ 布雷方法. numEnd = ( this . myRows *this . myColumns ) - leaveBombprivate int intvoid SetBomb(

19、 int count) counter = 0; tempint;ShowNum(10).getImageIcon();while (counter != count) tempint = ( int ) (Math. random ()*( this . myRows * this . myColumns );if (! this . myButton tempint.GetStatus() this . myButton tempint.SetStatus( true ); counter+;/ 显示雷得方法private void ShowBomb() for ( int i = 0;

20、i ( if ( this . myButton this . myButton ShowNum(9).getImageIcon();this . myRows * this i.GetStatus() i.setIcon( new. myColumns ); i+) / 监听点击扫雷方法showMessageDialog ( null , INFORMATION_MESSAGE);You Failed!是否要继续? private void CheckButton(ExtendButton TempButton) if (TempButton.GetStatus() this . timer

21、 .stop(); JOptionPane.Game ,JOptionPane.this .ShowBomb();IIint i = JOptionPane.showConfirmDialog(null, 消息, JOptionPane.YES_NO_OPTION);if(JOptionPane.YES_OPTION = i) SetExtendButton(); else this.dispose();return ;- 1;this+ 1;intint if (!TempButton.GetVisited() CircleNum0 = TempButton.GetPostion() Cir

22、cleNum = temp = 0;new int 8;this . myColumns 0 |CircleNum0 = (CircleNum0. myColumns = 0) ? -1 : CircleNum0;CircleNum1CircleNum1 = (CircleNum1 0) ? -1 : CircleNum1;CircleNum2 = TempButton.GetPostion()= TempButton.GetPostion()(CircleNum0+ 1) %this . myColumns ;this . myColumnsCircleNum2 = (CircleNum2

23、( this . myColumns - 1) | (CircleNum5 -1 : CircleNum5;CircleNum6this . myRows *+ 1) % this . myColumns = 0) ?= TempButton.GetPostion()+ this . myColumns ;CircleNum6 = (CircleNum6 ( this . myRows * this . myColumns - 1) ? -1 : CircleNum6;CircleNum7 = TempButton.GetPostion() + this . myColumns +1;Circ

24、leNum7 = (CircleNum7 ( this . myRows * this . myColumns - 1) | CircleNum7 % this . myColumns = 0) ? -1 CircleNum7;for ( int i = 0; i 0) TempButton.SetVisited( true );TempButton.setIcon( newShowNum(temp).getImageIcon();temp = 0; else if (temp = 0) TempButton.SetVisited( true );TempButton.setIcon( new

25、ShowNum(0).getImageIcon();for ( int i = 0; i 8; i+) if (CircleNumi != -1& ! myButton CircleNumi.GetVisited() CheckButton( myButton CircleNumi);this . numEnd -;if ( this . numEnd = 0) this . timer .stop(); 恭喜你! , 消息JOptionPane. showMessageDialog ( this , JOptionPane. INFORMATION_MESSAGE);ShowBomb();/

26、 构造方法public Game() Dimension dimension =Toolkit. getDefaultToolkit().getScree nSize();int x = (int ) (dimension.getWidth() -400)/ 2);int y = (int ) (dimension.getHeight() - 500) / 2);this . xis = 300;this . yis = 400;this .setBounds(x, y,this . xis , this . yis );this .setTitle( 扫雷);this . numBomb =

27、 10;this . myRows = 9;this . myColumns = 9;this .I ni tView();this .Ini tButto n();this .setDefaultCloseOperation(this . EXIT_ON_CLOSE ); 一 一publicGame( int row, int column,int numbomb) Dime nsio n dime nsion 二Toolkit. getDefaultToolkit().getScree nSize();int x = (int ) (dimension.getWidth() - 400)

28、/ 2);int y = (int ) (dimension.getHeight() - 500) / 2);this . xis = 310 * column /9;this . yis = 300 * row / 9 + 100;this .setBounds(x, y,this . xis , this . yis );this .setTitle( 扫雷);this . myRows = row;this . myColumns = column;this . numBomb = numbomb;this .I ni tView();this .Ini tButto n();this

29、.setDefaultCloseOperation(this . EXIT_ON_CLOSE ); 一 一/监听方法public void acti on Performed(Acti on Eve nt e) / 对(this.myRows * this.myColu mns)个 butt on的监听进行处理for (int i = 0; i = 0) ? pos : 0;return this . Butt on _pos; _public int GetPostio n() return this . Butt on _pos; public this return public ret

30、urn publicboolea n SetStatus(boolea n=sta;.Butt on Status.Butt on Statusthisboolea n thisboolea nthis . Buttonsta) GetStatus() .Butt on StatusSetVisited(boolea nVisited = vis;vis) return this public boolea nreturn this.Button_VisitedGetVisited() .Button_Visited/*设置butt on 显示图片*/package com.dhl.saole

31、i;import java.awt .Im age;import javax.swi ng.lm ageIc on;public class ShowNum private int num;public ShowNum( int n) this . num = n;publicImageIc on getImageIc on() ImageIc onimagelc on;switch ( num) case 0:imageIc on 二new ImageIc on(break ;case 1:imageIc on 二new ImageIc on(break ;case 2:imageIc on

32、 二new ImageIc on(break ;case 3:imageIc on 二new ImageIc on(break ;case 4:imageIc on 二new ImageIc on(break ;case 5:imageIc on 二new ImageIc on(break ;case 6:imageIc on 二new ImageIc on(break ;case 7:imageIc on 二new ImageIc on(break ;case 8:imageIc on 二new ImageIc on(break ;case 9:./class/images/dise.jpg

33、);./class/images/dise1.jpg);./class/images/dise2.jpg);./class/images/dise3.jpg);./class/images/dise4.jpg);./class/images/dise5.jpg);./class/images/dise6.jpg);./class/images/dise7.jpg);./class/images/dise8.jpg);imageIcon = new ImageIcon( break ;case 10:imageIcon = new ImageIcon( break ;case 11:imageI

34、con = new ImageIcon( break ;default :imageIcon = new ImageIcon( ./class/images/dise.jpg break ;./class/images/lei.jpg./class/images/biaose.jpg./class/images/leibj.jpg););););return imageIcon;new 一个 Game窗 口java.awt.Container; java.awt.Dimension; java.awt.GridBagConstraints; java.awt.GridBagLayout; ja

35、va.awt.Toolkit; java.awt.event.ActionEvent; java.awt.event.ActionListener; javax.swing.JButton; javax.swing.JDialog; javax.swing.JFrame; javax.swing.JLabel; javax.swing.JPanel; javax.swing.JTextField; class ViewReSet extends private private private private private private private private private pri

36、vate privateActionListenerJDialog implements frame = null ;myPanel = null ; myLabel = null ;myLabelRow = null ; myLabelColum = null ; myLabelBombNum = null ;myFieldRow = null ; myFieldColum = null ; myFieldBombNum = null ; buttonOK = null ; buttonReset = null ;*创建一个新窗口,输入新的行、列、雷数,然后用新数据重新 */package

37、com.dhl.saolei; import import import import import import import import import import import import import publicJFrame JPanel JLabel JLabel JLabel JLabel JTextField JTextField JTextField JButton JButtonprivateintrow ;privateintcolumn ;privateintbombNum;privateint myrcb = new int 3;privatevoidcreate

38、View()Container container = this this . myPanel = new JPanel( container.add( this . myPanel GridBagConstraints gbc = gbc. gbc. this this gbc. gbc. this this gbc. gbc. this this gbc. gbc. this this gbc. gbc. this this gbc. gbc. this this gbc. gbc. this this JPanel btnPanel = btnPanel.setOpaque( this

39、. buttonOK = this . buttonOK .addActionListener( btnPanel.add( this . buttonOK ); this . buttonReset = new JButton( this . buttonReset .addActionListener(.getContentPane(); new GridBagLayout(); );new GridBagConstraints();gridx = 1; gridy = 0;. myLabel. myPanel .add( gridx = 0; gridy = 1;. myLabelRow

40、. myPanel .add( gridx = 1; gridy = 1;. myFieldRow. myPanel .add( gridx = 0;gridy = 2;. myLabelColum. myPanel .add( gridx = 1; gridy = 2;. myFieldColum. myPanel .add( gridx = 0;gridy = 3;. myLabelBombNum. myPanel .add( this gridx = 1;gridy = 3;. myFieldBombNum. myPanel .add(new JLabel( 窗口行列设置 ); this . myLabel , gbc);new JLabel( this . myLabelRow 行数: ); , gbc);new JTextField(10); this. myFieldRow, gbc);thisthisnew JLabel( . myLabelColum列数: ); , gbc);new JTextField(10); . myFieldColum, g

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论