


版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、newE ssolei-白;爭srcj 曰圧 comdhl.saoleiH -ifi ExtendButtonjava + 山 GamejavaE- RunGameJava 民:L Show NumjavaViewReSet.jM曰 E& innages訶 biaose.jpg旦 disejpg回 ctisel.jpg- tl dise2Jpg:tj dise3Jp5:-目 dise4Jpg21 dise5-psp-jij di號6jpgr dise7jp?j-jJ diseBJpg日 leijpg! i 日 febj.jpg由 * JRE System Library Ddkl.6.
2、0_10D blnI - I M 合舐 I_-UiJ iJtllW離單忙 3WWH)時間:7雷數:10biaosedise4dise81Sdisedise2整個程序使用了 4個類:Game用于啟動程序,界面的布置,各種行列的算法。Exte ndButt on繼承了 Jbutton 用于新建butt on的各種屬性ShowNum監聽到某個butt on后,計算出此butt on周圍有幾個雷, 不同的雷對應不通的圖片,類得到butt on上應加載的圖片地址ViewReSet此類用于新建一個窗口,用戶輸入自己想要的行、列、雷數,然后用新的行列雷數一個新的Game/*運行類創建游戲主窗體*/publi
3、c class Game extends JFrame implements ActionListenerprivate Container myContainerprivateGridLayout myLayout;privateJPanelmyPa nelMai n;privateJPanelmyPa nel;privateJToolBar-myTool;privateJLabeltime ;privateJLabellabelTime ;privateJLabelbombNumprivateJLabellabelBombNum;private JMenuBar menuBar;priva
4、te JMenu menuE menuHprivate JMenultem menultemC乙 menultemSZ;private int numBomb countTime , leaveBomb, numEndprivateint myRows myColumnsprivateint xis , yis ;private Timer timer ;privateExte ndButt onmyButt on = null/初始化窗體private void InitView() /菜單初始化this . menuBar = new JMenuBar();this . menuE= ne
5、w JMenu("菜單(E)"); this . menuH= new JMenu("幫助(H)");this . menultemCZ = new JMenultem( "重置");this . menultemCZ.setAccelerator(KeyStroke.getKeyStroke (KeyEvent. VK_CIn putEve nt. CTRL_MASKthis . menultemCZ.addActionListener(this );this . menuEadd( this . menultemCZ);this
6、. menultemSZ = new JMenultem("設置”);this . menultemCZ.setAccelerator(KeyStroke.getKeyStroke (KeyEvent. VK_SIn putEve nt. CTRL_MASKthis . menultemSZ.addActionListener(this );this . menuEadd( this . menultemSZ);this . menuBar.add( this . menuE; this . menuBar.add( this . menuH; setJMenuBar( this .
7、 menuBar);/時間信息初始化this . myTool = new JToolBar();this . myTool.setLayout( new GridLayout(1,4);this . myTool.setFloatable( false );BorderLayout borderLayout = new BorderLayout(); this . myPanelMain = new JPanel(borderLayout);this . labelTime = new JLabel("時間:”);this . time = new JLabel();this .
8、time .setForeground(Color.REDthis . time .setFont( new Font( "font" , Font. PLAIN 20);this . labelBombNum = new JLabel("雷數:”);this . bombNun= new JLabel();this . bombNunsetText(String.valueOf (this . numBomb;this . bombNunsetForeground(Color.REDthis . bombNunsetFont(new Font( "fo
9、nt" , Font. PLAIM 20);thismyTool .add( this . labelTime );this . myTool .add( this . time );this . myTool .add( this this . myTool .add( this.labelBombNum);.bombNumthis . myPanelMain.add( this . myTool, borderLayout. NORTH/ 初始化 myButtonprivate void InitButton() BorderLayout borderLayout = new B
10、orderLayout();this . leaveBomb = this . numBombthis . 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();this . myPanel.setVisible( true );this
11、 . myPanel.setLayout( myLayout);this . myPanelMain.add( this . myPanel, borderLayout. CENTERthis . myContainer .add( this . myPanelMain);this . myButton = new ExtendButton(this . myRows" this . myColumn3;for ( int i = 0; i < ( this . myRows" this . myColumn$; i+) this . myButton i =new
12、ExtendButton();this . myButton i.SetPostion(i);this . myButton i.setlcon(new ShowNum(10).getImageIcon();this . myButton i.setButton_num(0);this . myButton i.SetVisited( false );this . myButton i.SetStatus( false );/設置右鍵監聽標記雷this . myButton i.addMouseListener(new MouseAdapter() public void mouseClick
13、ed(MouseEvent evt) if (evt.getModifiers() = InputEvent.BUTTON3_MAQKint n = (ExtendButton)evt.getSource().getButton_num();if (n = 1) if (!(ExtendButton)evt.getSource().GetVisited() (Exte ndButt on )evt.getSource().setButt on_num (0);(Exte ndButt on )evt.getSource().setlc on (newShowNum(10).getlmagelc
14、o n();leaveBomb+;bombNunsetText(Stri ng.valueOf (leaveBomb); else if (!(ExtendButton)evt.getSource().GetVisited() (Exte ndButt on )evt.getSource().setButt on_nu m(1);(Exte ndButt on )evt.getSource().setlc on (newShowNum(11).getImageIco n();leaveBomb-;bombNunsetText(Stri ng.valueOf (leaveBomb););this
15、 . myPanel.add( this . myButton i);this . myButton i.addActionListener(this );System. gc();this .SetBomb( this . numBombshow();/時鐘設置int delay = 1000;/創建一個監聽事件Action Liste ner drawClock =new Actio nListe ner()public void actionPerformed(ActionEvent evt) coun tTime +;time .setText(String.valueOf (coun
16、tTime );repai nt();/創建一個時間計數器,每一秒觸發一次this . timer = new Timer(delay,drawClock);this . timer .start();/重新初始化方法void (int this this this thisprivate forSetExte ndButto n() i = 0; i < (.myButton i.SetVisited(.myButton i.SetStatus(.myButton i.setText(.myButton i.setlcon(this . myRowS" this . myCo
17、lumnS; i+) false );false );null );new ShowNum(10).getlmagelc on(); thisthisthisthisthisthis.SetBomb( numBomb.countTime = 0;.timer .start();.leaveBomb = numBomb.bombNunsetText(String.numEnd= ( this . myRowS*valueOf (this . numBomb;this . myColumns) - leaveBomb;/布雷方法private void SetBomb( int count) in
18、t counter = 0;int tempi nt;while (counter != count) tempint = ( int ) (Math. randonl)*( this . myRows* this . myColumn3);if (! this . myButton tempint.GetStatus() this . myButton tempint.SetStatus(true );coun ter+;/顯示雷得方法private void ShowBomb() for (int i = 0; i < ( this . myRows* this . myColumn
19、s); i+) if (this . myButton i.GetStatus() this . myButton i.setlcon(new ShowNum(9).getImageIcon();/監聽點擊掃雷方法private void CheckButton(ExtendButton TempButton) if (TempButton.GetStatus() this . timer .stop();JOptionPane. showMessageDialoqnull , "You Failed!","Game",JOptio nPan e. IN
20、FORMATION_MESSAGEthis .ShowBomb();int i = JOptionPane.showConfirmDialog(null,"是否要繼續?","消息",JOptio nPan e.YES_NO_OPTION);if(JOpti onPan e.YES_OPTION = i) SetExte ndButto n(); else this.dispose();return ;int CircleNum = new int 8;int temp = 0;if (!TempButton.GetVisited() CircleNum0
21、 = TempButton.GetPostion() -this . myColumns- 1;CircleNum0 = (CircleNum0 < 0 | (CircleNum0 + 1) %this . myColumns=0) ? -1 : CircleNum0;CircleNum1=CircleNum1=TempButton.GetPostion() -this . myColumns;=(CircleNum1 < 0) ? -1 : CircleNum1;CircleNum2=CircleNum2=TempButton.GetPostion() -this . myCol
22、umns+ 1;=(CircleNum2 < 0 |CircleNum2 %this . myColumns= 0) ?1 : CircleNum2;CircleNum3 = TempButton.GetPostion() - 1;this . myColumns= 0) ? -1 :CircleNum3 = (CircleNum3 + 1) %CircleNum3;CircleNum4 = TempButto n.GetPostio n() + 1;CircleNum4 = (CircleNum4 % this . myColumns= 0) ? -1 : CircleNum4;Cir
23、cleNum5 = TempButton.GetPostion() +this . myColumns-1;CircleNum5 = (CircleNum5 > (this . myRows* this . myColumns - 1) |(CircleNum5 + 1) % this . myColumns= 0) ? -1 : CircleNum5;CircleNum6 = TempButton.GetPostion() +this . myColumns;CircleNum6 = (CircleNum6 > (this . myRows* this . myColumns-
24、1) ? -1 CircleNum6;CircleNum7 = TempButton.GetPostion() +this . myColumns+1;CircleNum7 = (CircleNum7 > (this . myRows* this . myColumns - 1) |CircleNum7 % this . myColumns= 0) ? -1 : CircleNum7;for ( int i = 0; i < 8; i+) if (CircleNumi != -1) if (myButton CircleNumi.GetStatus()&& ! my
25、Button CircleNumi.GetVisited() temp+;II顯示butt on圖片過程if (temp > 0) TempButton.SetVisited(true );TempButt on. setIc on(new ShowNum(temp).getlmagelc on();temp = 0; else if (temp = 0) TempButton.SetVisited( true );TempButt on. setlc on(new ShowNum(0).getImageIc on();for ( int i = 0; i < 8; i+) if
26、(CircleNumi != -1 &&! myButton CircleNumi.GetVisited()CheckButton( myButton CircleNumi);this . numEnd-;if (this . numEnd= 0) this . timer .stop();JOptionPane. showMessageDialoq this,” 恭喜你! ”,” 消息",JOptionPane. INFORMATION_MESSAGEShowBomb();II構造方法().getScree nSize();public Game() Dime ns
27、ion dime nsion = Toolkit. getDefaultToolkit int x = ( int ) (dimension.getWidth() - 400) I 2);int y = ( int ) (dimension.getHeight() - 500) I 2); this . xis = 300;this . yis = 400;this .setBounds(x, y,this . xis , this . yis );this .setTitle( "掃雷");this.numBomb= 10;this.myRows= 9;this.myCo
28、lumns= 9;thisn itView();thisn itButto n();this.setDefaultCloseOperati on(this . EXIT_ON_CLOSE().getScree nSize();public Game(int row, int column, int numbomb) Dime nsion dime nsion = Toolkit.getDefaultToolkitint x = ( int ) (dimension.getWidth() -400)/ 2); int y = ( int ) (dimension.getHeight() - 50
29、0) / 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;thisnitView();this . EXIT_ON_CLOSEthis .InitButton();this .setDefaultCloseOperation
30、( /監聽方法public void actionPerformed(ActionEvent e) / 對(this.myRows * this.myColu mns)個 butt on 的監聽進行處理for (int i = 0; i < ( this . myRows" this . myColu mn$; i+) if (e.getSource() =myButton i) CheckButton(ExtendButton) e.getSource();/重置按鈕監聽if (e.getSource() =menultemCZ) SetExte ndButto n();/設
31、置按鈕進行監聽 else if (e.getSource() = menultemSZ) new ViewReSet( this . myRows this . myColumns, this . numBomb this .dispose();public static void main(String args) new Game();/*創建一個butt on的實體類*/publicclass ExtendButtonextends JButton private intButt on _pos ;private int Button_num; private boolea n Butt
32、 on _Status private boolea n Butt on _Visitedpublic int getButton_num() returnButton_num ;public void setButton_num( int button_num) this . Button_num = button_num; - -publicint SetPostion(int pos) this . Butt on _pos = (pos >= 0) ? pos : 0; retur nthis . Butt on _pos ;public int GetPostion() ret
33、ur nthis . Butt on _pos ;public boolean SetStatus( boolean sta) this . Butt on _Status= sta;return this . Button_Status ; -public boolean GetStatus() return this . Button_Status ;publicboolean SetVisited( boolean vis) this . Butt on _Visited= vis;return this . Butt on _Visited;publicboolean GetVisit
34、ed() return this . Butt on _Visited; -t*設置butt on顯示圖片public class ShowNum private int numpublic ShowNum(nt n) this . num= n;public ImageIc on getImageIc on() ImageIcon imagelcon ;switch ( num) case 0:imageIcon = new ImageIcon( "./class/images/dise.jpg" ); break ;case 1:imageIcon = new Imag
35、eIcon( "./class/images/dise1.jpg" ); break ;case 2:imageIcon = new ImageIcon( "./class/images/dise2.jpg" ); break ;case 3:imageIcon = new ImageIcon( "./class/images/dise3.jpg" ); break ;case 4:imageIcon = new ImageIcon( "./class/images/dise4.jpg" ); break ;cas
36、e 5:imageIcon = new ImageIcon( "./class/images/dise5.jpg" ); break ;case 6:imagelcon =new Imagelcon( "./class/images/dise6.jpg" );break ;case 7:imagelcon =new Imagelcon( "./class/images/dise7.jpg" );break ;case 8:imagelcon =new Imagelcon( "./class/images/dise8.jpg&
37、quot; );break ;case 9:imagelcon =new lmagelcon( "./class/images/lei.jpg");break ;case 10:imagelcon =new lmagelcon( "./class/images/biaose.jpg" );break ;case 11:imagelcon =new lmagelcon( "./class/images/leibj.jpg");break ;default :imagelcon =new lmagelcon( "./class/
38、images/dise.jpg" );break ; retur n imagelc on;new一個 Gam窗口t*創建一個新窗口,輸入新的行、列、雷數,然后用新數據重新public class ViewReSet extends JDialogimplements ActionListenerprivateJFrame frame = nullprivateJPa nel myPa nel = nullprivate JLabelmyLabel = nullprivate JLabel private JLabelmyLabelRow = null ; myLabelColum
39、= nullprivateJLabel myLabelBombNum= nullprivate JTextField private JTextField private JTextFieldmyFieldRow = null ; myFieldColum = null ; myFieldBombNum= n ullprivate JButtonbuttonOK = null ;private JButt onbutt on Reset = nullprivateintrow;privateintcolumn;privateintbombNumprivateint myrcb = new in
40、t 3;private void createView()Container container = this .getContentPane(); this . myPanel = new JPanel( new GridBagLayout(); container.add(this . myPanel);GridBagC on stra ints gbc =new GridBagC on strai nts();gbc. gridx = 1;gbc. gridy = 0;this . myLabel = new JLabel("窗口行列設置”);this . myPanel.ad
41、d( this . myLabel, gbc);gbc. gridx = 0;gbc. gridy = 1;this . myLabelRow = new JLabel("行數:”); this . myPanel.add( this . myLabelRow, gbc);gbc. gridx = 1;gbc. gridy = 1;this . myFieldRow = new JTextField(IO);this . myPanel.add( this . myFieldRow, gbc);gbc. gridx = 0;gbc. gridy = 2;this . myLabelC
42、olum = new JLabel("列數:”); this . myPanel.add( this . myLabelColum, gbc); gbc. gridx = 1;gbc. gridy = 2;this . myFieldColum = new JTextField(10);this . myPanel.add( this . myFieldColum , gbc);gbc. gridx = 0;gbc. gridy = 3;this . myLabelBombNum= new JLabel("雷數:”); this . myPanel.add( this . myLabelBombNumgbc); gbc. gridx = 1;gbc. gridy = 3;this . myFieldBombNum= new JTextField(10);this . myPanel.add( this . myFieldBombNum gbc);JPa nel btnPanel =new JPa nel();btnPanel.setOpaque( false );this . buttonOK = new JButton("
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 四個人合伙協議3篇
- 礦物寶石展會策劃與組織考核試卷
- 信用風險管理考核試卷
- 稀土金屬在汽車尾氣凈化中的應用考核試卷
- 罐頭食品行業競爭格局與市場分析考核試卷
- 2025企業租賃合同范本下載
- 2025商務談判設備租賃合同策劃書
- 2025餐廳裝修設計合同
- 2025標準的技術授權合同
- 2025《產品銷售合同范本》
- 石油石化硫化氫培訓
- 新生兒貧血的護理查房
- CQI-9 第四版 熱處理系統審核表中文完整版-
- 小紅書食用農產品承諾書示例
- 水果店投資項目可行性分析報告
- CQI-23模塑系統評估審核表-中英文
- 《顱內壓增高的臨床表現》教學課件
- 胸痹心痛護理查房
- DB34T 4912-2024二手新能源汽車鑒定評估規范
- 會計記賬服務合同
- 四下第五單元課件
評論
0/150
提交評論