資源描述:
《java課程設計-掃雷游戲》由會員上傳分享,免費在線閱讀,更多相關(guān)內(nèi)容在行業(yè)資料-天天文庫。
1、packagesweeper;importjava.awt.BorderLayout;importjava.awt.FlowLayout;importjava.awt.GridLayout;importjava.awt.Insets;importjava.awt.event.MouseAdapter;importjava.awt.event.MouseEvent;importjava.awt.event.MouseListener;importjavax.swing.ImageIcon;importjavax.swing.JButton;import
2、javax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JMenu;importjavax.swing.JMenuBar;importjavax.swing.JMenuItem;importjavax.swing.JOptionPane;importjavax.swing.JPanel;publicclassJMineextendsJFrame{publicintcol=10;//掃雷面板的列數(shù)publicintrow=10;//掃雷面板好行數(shù)privateintmineNum=10
3、;//地雷的個數(shù)privateintmineRemainNum=mineNum;JButton[][]innitBoard=newJButton[row][col];//初始化面板char[][]board=newchar[row][col];privateintisClick[][]=newint[row][col];//用于記錄按鈕是否被點擊過,點擊過賦值為1,否則為0privateintcount[][]=newint[row][col];//統(tǒng)計每個坐標周圍地雷的個數(shù)privatebooleanismine=false;//判斷是否踩到地雷p
4、rivatebooleanfinish_click=true;privatebooleaniswin=true;privateintflagnum=0;//記錄插在地雷上小旗個個數(shù)privateMyThreadmythread=newMyThread();privatestaticinttime_start=0;privatestaticinttime=0;//用于計時JLabelremain1=newJLabel();//4個標簽顯示剩余地雷個數(shù);JLabelremain2=newJLabel(newImageIcon((mineRemainNum
5、/100)+".png"));JLabelremain3=newJLabel(newImageIcon((mineRemainNum/10)+".png"));JLabelremain4=newJLabel(newImageIcon((mineRemainNum%10)+".png"));staticJLabeltime1=newJLabel(newImageIcon("0.png"));staticJLabeltime2=newJLabel(newImageIcon("0.png"));staticJLabeltime3=newJLabel(new
6、ImageIcon("0.png"));JButtonrestart;publicvoidinnit(){this.setSize(400,450);this.setVisible(true);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);JPanelpanel=newJPanel(newBorderLayout());//主面板this.setContentPane(panel);layMines();//隨機布雷JMenuBarbar=createBar();//調(diào)用創(chuàng)建菜單欄的方法thi
7、s.setJMenuBar(bar);//與面板的添加的菜單欄無關(guān),是窗體里面自帶的添加菜單的方法JPanelmineBoard=createBoard();//調(diào)用創(chuàng)建掃雷主面板的方法JPanelstate=createState();//調(diào)用創(chuàng)建狀態(tài)欄的方法panel.add(mineBoard,BorderLayout.CENTER);panel.add(state,BorderLayout.NORTH);for(inti=0;i8、Click[i][j]=0;}}publicJPanelcreateBoard(){//創(chuàng)建地雷主面板的方法