Java編寫的掃雷小程序_第1頁
Java編寫的掃雷小程序_第2頁
Java編寫的掃雷小程序_第3頁
Java編寫的掃雷小程序_第4頁
Java編寫的掃雷小程序_第5頁
已閱讀5頁,還剩12頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、類.new10Game用于啟動(dòng)程序,界面的布置,各種行列的算法。Exte ndButt on繼承了 Jbutton 用于新建butt on的各種屬性ShowNum監(jiān)聽到某個(gè)butt on后,計(jì)算出此butt on周圍有幾個(gè)雷,不同的雷對(duì)應(yīng)不通的圖片,得到butt on上應(yīng)加載的圖片地址ViewReSet此類用于新建一個(gè)窗口,用戶輸入自己想要的行、列、雷數(shù),然后用新的行列雷數(shù)一個(gè)新的Game0 saoleii曰”;崢STCI , jT _田 com.dhLraolei! lil Jj Exte nd Button javaS- .jj Game JavaJI,| FF- ,r RunGameJs

2、I itShowlJuTnjavs-犀 mag* ;畫 biaose.jpgI dise.Jpg :T disel jpg恤2 .jpg 回d彌Jpg1 dise4.jpg 也| dise5jpg 歸 dise6jpg dise7.jpg jjl ctiseBJpg 迥 lei.jp g S leibjjpgEl n JR.E Systerr Library 0dKl,6,O_10 L”G binbiaosedise2dise3ise5dise6dise7biaojidise8/*運(yùn)行類 創(chuàng)建游戲主窗體*/package com.dhl.saolei;import java.awt.Borde

3、rLayout;import java.awt.Color;import java.awt.Container;import java.awt.Dimension;import java.awt.Font;import java.awt.GridLayout;import java.awt.Toolkit;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.InputEvent;import java.awt.event.KeyEvent;import java

4、.awt.event.MouseAdapter;import java.awt.event.MouseEvent;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.JToolBar;import javax.swing.KeySt

5、roke;import javax.swing.Timer;privateintnumBomb ,countTime, leaveBombprivateintmyRows ,myColumns ;privateintxis , yisJprivate Timer timernumEnd ;public class Game extends JFrameimplementsprivateContainermyContainerJprivateGridLayoutmyLayout ;privateJPanelmyPanelMain ;privateJPanelmyPanel ;privateJTo

6、olBarmyTool ;privateJLabeltime ;privateJLabellabelTime ;privateJLabelbombNum ;privateJLabellabelBombNum ;privateJMenuBarmenuBar ;privateJMenu menuE , menuH ;privateJMenuItemmenuItemCZ ,menuItemSZActionListenerprivate ExtendButtonmyButtonnull/ 初始化窗體 private void InitView() / 菜單初始化this . menuBar = new

7、 JMenuBar(); this . menuE = new JMenu( 菜單 (E) ); this . menuH = new JMenu( 幫助 (H) );this . menuItemCZ = new JMenuItem( 重置 );this . menuItemCZ .setAccelerator(KeyStroke. _C, InputEvent. CTRL_MASK );this . menuItemCZ .addActionListener(this . menuE .add( this . menuItemCZ );getKeyStrokethis );(KeyEven

8、t.VKthis . menuItemSZ = new JMenuItem( 設(shè)置 );this . menuItemCZ .setAccelerator(KeyStroke. _S, InputEvent. CTRL_MASK );this . menuItemSZ .addActionListener(this . menuE .add( this . menuItemSZ );getKeyStrokethis );(KeyEvent.VKthis. menuBar .add( this . menuE );this . menuBar setJMenuBar(.add( this . m

9、enuH ); this . menuBar );/ 時(shí)間信息初始化this . myToolnew JToolBar();this . myTool .setLayout( this . myTool .setFloatable( BorderLayout borderLayout =new GridLayout(1, 4); false );new BorderLayout(); 時(shí)間: );RED);font , Font.PLAIN , 20);this. myTool.add(this. labelTime );this. myTool.add(this. time );this.

10、myTool.add(this. labelBombNum );this. myTool.add(this. bombNum );this. myPanelMain.add(this . myTool , borderLayout.this . bombNum .setForeground(Color. RED);this . bombNum .setFont(new Font( font , Font. PLAIN , 20);NORTH);this . myPanelMain = new JPanel(borderLayout);this . labelTime = new JLabel(

11、 this . time = new JLabel();this . time .setForeground(Color. this . time .setFont( new Font( this . labelBombNum = new JLabel( 雷數(shù): );this . bombNum = new JLabel();this . bombNum.setText(String.valueOf ( this . numBomb );/ 初始化 myButtonprivate void InitButton() BorderLayout borderLayout =new BorderLa

12、yout();this . leaveBomb = this . numBomb ;this . numEnd = ( this . myRows * this . myColumns ) - this . leaveBomb this . myContainer = this .getContentPane();this . myLayout = new GridLayout( this . myRows , this . myColumns , 1,1);this . myPanel = new JPanel();true ); myLayout );this . myPanel .set

13、Visible( this . myPanel .setLayout(this . myPanelMain .add( this this . myContainer .add( this. myPanel , borderLayout. CENTER); . myPanelMain );this . myButtonnew ExtendButton(this . myRows *this . myColumns );for ( int i = 0; i ( this . myButton this . myButton this . myButton this . myButton this

14、 . myButton this . myButton / 設(shè)置右鍵監(jiān)聽標(biāo)記雷this . myButtonthis . myRows * this . myColumns ); i+) i = new ExtendButton();i.SetPostion(i);i.setIcon( new ShowNum(10).getImageIcon(); i.setButton_num(0);i.SetVisited( false );i.addMouseListener(new MouseAdapter() i.SetStatus( false );BUTTON3_MASK)public void

15、 mouseClicked(MouseEvent evt) if (evt.getModifiers() = InputE n =(ExtendButton)evt.getSource().getButton_num();if (n = 1) if (!(ExtendButton)evt.getSource().GetVisited() (ExtendButton)evt.getSource().setButton_num(0);(ExtendButton)evt.getSource().setIcon( newShowNum(10).getImageIcon();leaveB

16、omb +;bombNum.setText(String. valueOf ( leaveBomb ); else if (!(ExtendButton)evt.getSource().GetVisited() new(Exte ndButt on )evt.getSource().setButt on_nu m(1);(Exte ndButt on) evt.getSource().setIc on( ShowNum(11).getlmagelco n();leaveBombvalueOf (leaveBomb ); bombNum.setText(Stri ng. );this . myP

17、anel .add( this . myButton i);this . myButton i.addActionListener(this );System. gc ();this .SetBomb( this . numBomb); show();/時(shí)鐘設(shè)置int delay = 1000;/創(chuàng)建一個(gè)監(jiān)聽事件Action Liste ner drawClock =new Actio nListe ner()public void action Performed(Act ionEvent evt) coun tTime +;time .setText(String.valueOf ( co

18、untTime );repai nt();/創(chuàng)建一個(gè)時(shí)間計(jì)數(shù)器,每一秒觸發(fā)一次this . timer = new Timer(delay,drawClock);this . timer .start();/重新初始化方法(inti = 0; i (this .myRows * this . myColumns ); i+) this.myButtoni.SetVisited(false );this.myButtoni.SetStatus(false );this.myButtoni.setText(n ull);this.myButtoni.setIco n(new ShowNum(10)

19、.getImageIcon()private voidSetExte ndButto n() 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 ) - leaveBomb / 布雷方法private void SetBomb( int

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

21、olumns ); i+) if (this . myButton i.GetStatus() this . myButton i.setIcon( newShowNum(9).getImageIcon(); / 監(jiān)聽點(diǎn)擊掃雷方法 private void CheckButton(ExtendButton TempButton) if (TempButton.GetStatus() this . timer .stop();JOptionPane. showMessageDialog ( null , You Failed! , Game ,JOptionPane. INFORMATION_M

22、ESSAGE );消息this .ShowBomb();int i = JOptionPane.showConfirmDialog(null, 是否要繼續(xù)? , , JOptionPane.YES_NO_OPTION);if(JOptionPane.YES_OPTION = i) SetExtendButton(); else this.dispose(); return ;int CircleNum =new int 8;int temp = 0;if (!TempButton.GetVisited() CircleNum0 = TempButton.GetPostion()CircleNu

23、m0 = (CircleNum0 0 | (CircleNum0 + 1) % this . myColumns = 0) ? -1 : CircleNum0;this . myColumns - 1;CircleNum1 = TempButton.GetPostion() -CircleNum1 = (CircleNum1 0) ? -1 : CircleNum1;this . myColumnsCircleNum2 = TempButton.GetPostion()CircleNum2 = (CircleNum2 ( thisthis . myColumns -1; . myRows *

24、this . myColumns- 1) | (CircleNum5 + 1) %this . myColumns = 0) ? -1 : CircleNum5;CircleNum6 = TempButton.GetPostion() +CircleNum6 = (CircleNum6 ( this - 1) ? -1 : CircleNum6;this . myColumns ;. myRows * this . myColumnsCircleNum7 = TempButton.GetPostion() +CircleNum7 = (CircleNum7 ( thisthis . myCol

25、umns +1; . myRows * this . myColumns- 1) | CircleNum7 %this . myColumns = 0) ? -1 : CircleNum7;for ( int i = 0; i 0) TempButton.SetVisited(TempButton.setIcon(true );new ShowNum(temp).getImageIcon();temp = 0; else if (temp = 0) TempButton.SetVisited( TempButton.setIcon( for ( int i = 0; i 8; i+) true

26、 );new ShowNum(0).getImageIcon();if (CircleNumi != -1& ! myButtonCircleNumi.GetVisited() CheckButt on(myButton CircleNumi);this . numEndif(this . numEnd = 0) this . timer .stop();JOptionPane.showMessageDialog(this ,恭喜你! ”,” 消息,JOpti onPane.INFORMATION_MESSAGE );ShowBomb();/ 構(gòu)造方法public Game() Dime ns

27、i on dime nsion =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 = 10;this . myRows = 9;

28、this . myColumns = 9;this n itView();this ni tButto n();this .setDefaultCloseOperation(this . EXIT_ON_CLOSE ); -public Game( int row, int column,int numbomb) Dime nsi on dime nsion =Toolkit. getDefaultToolkit().getScree nSize();int x = (int ) (dimension.getWidth() - 400) / 2);int y = (int ) (dimensi

29、on.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;thisn itView();thisni tButto n();this .setDefaultCloseOperation(this . EXIT_O

30、N_CLOSE );/監(jiān)聽方法public void acti on Performed(Act ionEvent e) / 對(duì)(this.myRows * this.myColum ns)個(gè) butt on的監(jiān)聽進(jìn)行處理for ( int i = 0; i = 0) ? pos : 0;retur nthis . Butt on _pos; - public intGetPostio n() retur nthis . Butt on _pos;sta) public boolea n SetStatus( boolea n this . Button_Status = sta; retur

31、n this . Button_Status ; -GetStatus() .Button_Statuspublic boolea n return thispublic boolea n SetVisited(boolea nvis) this . Butt on _Visited= vis;return this . Butt on Visitedpublic boolea n return thisGetVisited() .Button_Visitedt*設(shè)置butt on顯示圖片*/package com.dhl.saolei;import java.awt.lmage;import

32、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:./class/images/dise.jpg);./class/images

33、/disel.jpg);imageIcon = new ImageIcon( break ;case 3:imageIcon = new ImageIcon( break ;case 4:imageIcon = new ImageIcon( break ;case 5:imageIcon = new ImageIcon( break ;case 6:imageIcon = new ImageIcon( break ;case 7:imageIcon = new ImageIcon( break ;case 8:imageIcon = new ImageIcon( break ;case 9:i

34、mageIcon = new ImageIcon( break ;case 10:imageIcon = new ImageIcon( break ;case 11: imageIcon = new ImageIcon( break ;./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);

35、./class/images/lei.jpg );./class/images/biaose.jpg );./class/images/leibj.jpg );default :imageIcon = new ImageIcon( ./class/images/dise.jpg ) break ;return imageIcon;*new 一個(gè) Game窗 口創(chuàng)建一個(gè)新窗口,輸入新的行、列、雷數(shù),然后用新數(shù)據(jù)重新*/package com.dhl.saolei;import java.awt.Container;import java.awt.Dimension;import java.awt

36、.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.Toolkit;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JDialog;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JTex

37、tField;public class ViewReSet extends JDialog implements ActionListenerprivateJFrame frame = nullprivate JPanel private JLabel private JLabel private JLabel private JLabelmyPanel = null ; myLabel = null ; myLabelRow = null ; myLabelColum = null ; myLabelBombNum = nullprivateJTextFieldmyFieldRow =nul

38、l ;privateJTextFieldmyFieldColum= null ;privateJTextFieldmyFieldBombNum= nullprivate JButton private JButtonbuttonOK = null ; buttonReset = nullprivateintrow ;privateintcolumn ;privateintbombNum ;privateint myrcb =new int 3;private void createView() Container container =thisthis . myPanel = new JPan

39、el( container.add( this . myPanel.getContentPane(); new GridBagLayout(); );GridBagConstraints gbc =new GridBagConstraints();gbc. gridx = 1;gbc. gridy = 0;this . myLabel = new JLabel( 窗口行列設(shè)置 ); this . myPanel .add( this . myLabel , gbc);new JLabel( 行數(shù): );gbc. gridx = 0; gbc. gridy = 1; this . myLabel

40、Rowthis. myPanel .add(this . myLabelRow, gbc);gbc.gridx = 1;gbc.gridy = 1;this. myFieldRow =new JTextField(10);this. myPanel .add(this . myFieldRow, gbc);gbc.gridx = 0;gbc.gridy = 2;this. myLabelColum= new JLabel( 列數(shù): );this. myPanel .add(this . myLabelColum, gbc);gbc.gridx = 1;gbc.gridy = 2;this. m

41、yFieldColum= new JTextField(10);this. myPanel .add(this . myFieldColum, gbc);gbc.gridx = 0;gbc.gridy = 3;this. myLabelBombNum = new JLabel( 雷數(shù): this. myPanel .add(this . myLabelBombNum , gbc);gbc.gridx = 1;gbc.gridy = 3;this. myFieldBombNum = new JTextField(10);this. myPanel .add(this . myFieldBombNum , gbc);JPanel btnPanel =new JPanel();btnPanel.setOpaque(false );this. buttonOK =new JButton( 確

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論