问题描述
importjava.awt.*;importjava.awt.event.*;importjava.util.Scanner;importjavax.swing.*;publicclasssaoleiextendsJFrame{privateintrow=10;//不设初始值。按下开始按钮,就卡死了privateintcol=10;//不设初始值。按下开始按钮,就卡死了Bomb[][]buttons;//没个按钮初始看做为雷按钮privateJButtonstart;privateJLabellabel1;//雷数标签privateintBombNum=10;//设置雷的数目privateJPanelpanel1;//标签面板privateJPanelpanel2;//雷面板intfoundBomb,restBomb,restbuttons;//找到的地雷数,剩余雷数,剩余按钮privateintlei[][];privateContainercontainer=getContentPane();//构造方法publicsaolei(){this.setTitle("扫雷小游戏");JMenuBarTestJMenuBar=newJMenuBar();JMenuItemprimary,medium,senior;this.setJMenuBar(TestJMenuBar);JMenubegin=newJMenu("开始");TestJMenuBar.add(begin);primary=newJMenuItem("初级");primary.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){initButtons(10,10,10);}});begin.add(primary);begin.addSeparator();medium=newJMenuItem("中级");medium.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){initButtons(15,15,20);//超过10程序运行就有问题、、、、}});begin.add(medium);begin.addSeparator();senior=newJMenuItem("高级");senior.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){initButtons(20,20,40);}});begin.add(senior);JMenuchoose=newJMenu("选项");TestJMenuBar.add(choose);choose.addSeparator();JMenuchange=newJMenu("改变颜色");choose.add(change);change.add(newJMenuItem("红色"));change.add(newJMenuItem("黄色"));change.add(newJMenuItem("紫色"));change.add(newJMenuItem("蓝色"));choose.addSeparator();JMenuhelp=newJMenu("帮助");TestJMenuBar.add(help);help.addSeparator();JMenuexit=newJMenu("退出");TestJMenuBar.add(exit);this.setDefaultCloseOperation(EXIT_ON_CLOSE);primary.doClick();}publicvoidinitButtons(introw,intcol,intBombNum){if(panel2!=null){this.remove(panel2);}label1=newJLabel("设置当前雷数");start=newJButton("开始");panel1=newJPanel();panel1.add(label1);panel1.add(start);container.add(panel1,BorderLayout.NORTH);panel2=newJPanel();panel2.setLayout(newGridLayout(row,col,2,2));container.add(panel2,BorderLayout.CENTER);start.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){startBomb();}});buttons=newBomb[row][col];for(inti=0;i<row;i++){for(intj=0;j<col;j++){buttons[i][j]=newBomb(i,j);buttons[i][j].addMouseListener(newBomb_mouseAdapter(this));buttons[i][j].addActionListener(newBomb_actionAdapter(this));panel2.add(buttons[i][j]);}}this.setSize(1000,1000);setBounds(400,100,450,500);setResizable(false);setVisible(true);startBomb();//窗口完成后开始布雷}/*开始,布雷*/publicvoidstartBomb(){label1.setText("设置当前雷数"+":"+BombNum);for(inti=0;i<row;i++){for(intj=0;j<col;j++){buttons[i][j].isBomb=false;buttons[i][j].isClicked=false;buttons[i][j].isRight=false;buttons[i][j].BombFlag=0;buttons[i][j].BombRoundCount=8;buttons[i][j].setEnabled(true);//设置的控件(这里是按钮buttons[i][j])是否被禁用,true是可用。buttons[i][j].setText("");foundBomb=0;restBomb=BombNum;restbuttons=row*col-BombNum;}}for(inti=0;i<BombNum;){intx=(int)(Math.random()*row);inty=(int)(Math.random()*col);if(buttons[x][y].isBomb!=true){buttons[x][y].isBomb=true;i++;}}CountRoundBomb();//周围雷的个数}publicvoidCountRoundBomb(){//周围雷的个数for(inti=0;i<row;i++){for(intj=0;j<col;j++){intcount=0;//当需要检测的单元格本身无地雷的情况下,统计周围的地雷个数if(buttons[i][j].isBomb!=true){for(intx=i-1;x<i+2;x++){for(inty=j-1;y<j+2;y++){if((x>=0)&&(y>=0)&&(x<row)&&(y<col)){if(buttons[x][y].isBomb==true){count++;}}}}buttons[i][j].BombRoundCount=count;}}}}/*是否挖完了所有的雷就胜利啦*/publicvoidisWin(){restbuttons=row*col-BombNum;for(inti=0;i<row;i++){for(intj=0;j<col;j++){if(buttons[i][j].isClicked==true){restbuttons--;}}}if(foundBomb==BombNum||restbuttons==0){JOptionPane.showMessageDialog(this,"您挖完了所有的雷,您胜利了!","胜利",JOptionPane.INFORMATION_MESSAGE);startBomb();}}/*当选中的位置为空,则翻开周围的地图*/publicvoidisNull_turnRound(BombClickedButton){inti,j;i=ClickedButton.nx;//按钮的横坐标j=ClickedButton.ny;//按钮的纵坐标for(intx=i-1;x<i+2;x++){for(inty=j-1;y<j+2;y++){if(((x!=i)||(y!=j))&&(x>=0)&&(y>=0)&&(x<row)&&(y<col)){if(buttons[x][y].isBomb==false&&buttons[x][y].isClicked==false&&buttons[x][y].isRight==false){turn(buttons[x][y]);}}}}}/*翻开*/publicvoidturn(BombClickedButton){ClickedButton.setEnabled(false);ClickedButton.isClicked=true;if(ClickedButton.BombRoundCount>0){ClickedButton.setText(ClickedButton.BombRoundCount+"");}else{isNull_turnRound(ClickedButton);}}/*左键点击*/publicvoidactionPerformed(ActionEvente){if(((Bomb)e.getSource()).isClicked==false&&((Bomb)e.getSource()).isRight==false){if(((Bomb)e.getSource()).isBomb==false){turn(((Bomb)e.getSource()));isWin();}else{for(inti=0;i<row;i++){for(intj=0;j<col;j++){if(buttons[i][j].isBomb==true){buttons[i][j].setText("B");}}}((Bomb)e.getSource()).setForeground(Color.RED);//事件源的背景色((Bomb)e.getSource()).setText("X");JOptionPane.showMessageDialog(this,"你踩到地雷了,按确定重来","踩到地雷",2);startBomb();}}}/*右键点击*/publicvoidmouseClicked(MouseEvente){BombbombSource=(Bomb)e.getSource();booleanright=SwingUtilities.isRightMouseButton(e);if((right==true)&&(bombSource.isClicked==false)){bombSource.BombFlag=(bombSource.BombFlag+1)%3;if(bombSource.BombFlag==1){if(restBomb>0){bombSource.setForeground(Color.RED);bombSource.setText("F");bombSource.isRight=true;restBomb--;}else{bombSource.BombFlag=0;}}elseif(bombSource.BombFlag==2){restBomb++;bombSource.setText("");bombSource.isRight=false;}if(bombSource.isBomb==true){if(bombSource.BombFlag==1){foundBomb++;}elseif(bombSource.BombFlag==2){foundBomb--;}}label1.setText("当前雷数"+":"+restBomb);isWin();}}publicstaticvoidmain(String[]args){saoleim=newsaolei();m.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}}////////////////////////////////////////////////////////////////////////////////classBombextendsJButton{intnx,ny;//雷所在的坐标intBombRoundCount;//周围雷数booleanisBomb;//是否为雷booleanisClicked;//是否点击左键intBombFlag;//探雷标记booleanisRight;//是否点击右键publicBomb(intx,inty){nx=x;ny=y;BombFlag=0;BombRoundCount=8;isBomb=false;isClicked=false;isRight=false;}}classBomb_actionAdapterimplementsActionListener{privatesaoleiadaptee;Bomb_actionAdapter(saoleiadaptee){this.adaptee=adaptee;}publicvoidactionPerformed(ActionEvente){adaptee.actionPerformed(e);}}classBomb_mouseAdapterextendsMouseAdapter{privatesaoleiadaptee;Bomb_mouseAdapter(saoleiadaptee){this.adaptee=adaptee;}publicvoidmouseClicked(MouseEvente){adaptee.mouseClicked(e);}}