import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class sao extends JButton implements MouseListener,ActionListener{
     
     JButton jb1[][] = new JButton[9][9]; 
     int a[][] ={{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0}};  
     
    
 sao(){   
     JFrame f_main = new JFrame("扫雷"); 
     f_main.setSize(300,300);
     f_main.setLocation(400,200);     JPanel jp1=new JPanel();
     JPanel jp2=new JPanel();
     JPanel jp3=new JPanel();
     JTextField jt1 = new JTextField("0",5); 
     JTextField jt2 = new JTextField("10",5); 
     JButton jb = new JButton("开始"); 
       
     JMenuBar menub = new JMenuBar(); 
     JMenu j1=new JMenu("游戏");
     JMenu j2=new JMenu("帮助");
     JMenu j3=new JMenu("关于作者");
     JMenuItem jm1=new JMenuItem("初级");
     JMenuItem jm2=new JMenuItem("中级");
     JMenuItem jm3=new JMenuItem("高级");
     JMenuItem jm4=new JMenuItem("开局");
     JMenuItem jm5=new JMenuItem("退出");
     JMenuItem jm6=new JMenuItem("帮助");
     JMenuItem jm7=new JMenuItem("扫雷游戏");     menub.add(j1);           //向菜单栏中添加菜单
     menub.add(j2); 
     menub.add(j3);      j1.add(jm4); 
     j1.addSeparator(); 
     j1.add(jm1); 
     j1.add(jm2); 
     j1.add(jm3); 
     j1.addSeparator(); 
     j1.add(jm5); 
     j2.add(jm6); 
     j3.add(jm7); 
     f_main.setJMenuBar(menub);          
     JDialog d1 = new JDialog(f_main,"恭喜",true); 
     JButton b1 = new JButton("确定"); 
     JLabel l1 = new JLabel("恭喜通关,再来一局吗?");      d1.add(l1,"Center"); 
     d1.add(b1); 
     d1.setVisible(false);      jp2.setLayout(new GridLayout(9,9));
     jp1.setBackground(Color.orange); 
     jp1.setSize(300,50);
     jp2.setBackground(Color.gray); 
     jp2.setSize(300,200);   
    for(int i =0;i < 9;i++) 
    for(int j = 0;j < 9;j++){ 
         jb1[i][j]=new JButton();
         jb1[i][j].addMouseListener(this);
         jb1[i][j].addActionListener(this);
    }
    for(int n =0;n < 9;n++) 
    for(int m = 0;m < 9;m++){ 
        jp2.add(jb1[n][m]);
    }
       jp3.add(jp1);
    jp3.add(jp2);
    jp1.add(jt1);
    jp1.add(jb);
    jp1.add(jt2);
    f_main.add(jp3);
     f_main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     f_main.setVisible(true);     jm6.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            JOptionPane.showMessageDialog(null, "本人初学,参照课本资料和网上实例编写,请多多指教","帮助主题",JOptionPane.PLAIN_MESSAGE);
        }
    });     jm7.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent e){
            JOptionPane.showMessageDialog(null,"此游戏仿制Windows中的扫雷程序而写,具体请参照Window扫雷!","扫雷游戏",JOptionPane.PLAIN_MESSAGE);
        }
    });      jm5.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e){
                  System.exit(0);
        }
  });
 }
  
void bulei(){             //随机生雷
    for(int i=0;i<9;i++) 
    { 
           int x =(int)(Math.random()*9); 
           int y =(int)(Math.random()*9); 
           if(a[x][y] == 0) {
               a[x][y]=-1;
           } 
           i++;
    } 
}void win(){                     //找到的地雷数 
     int findBomb=0;             
     for(int c=0;c<9 ; c++) 
         for(int b=0;b<9; b++) { 
             if(a[c][b]== -1) 
                   findBomb++; 
         } 
     if( findBomb ==10 ) { 
           JOptionPane.showMessageDialog(null, "YOU WIN","YOUWIN",JOptionPane.PLAIN_MESSAGE);
     } 
}void leishu(){                    //计算周围地雷数
       for (int m = 0; m < 9; m++) {                
       for (int n = 0; n < 9; n++) {
        if (a[m][n] != -1) {
           int i = 0;
            if (a[m - 1][n - 1] == -1)
                 i++;
            if (a[m - 1][n] == -1)
                 i++;
            if (a[m - 1][n + 1] == -1)
                 i++;
           if (a[m][n - 1] == -1)
               i++;
           if (a[m][n + 1] == -1)
                i++;
            if (a[m + 1][n - 1] ==-1)
                i++;
            if (a[m + 1][n] == -1)
               i++;
            if (a[m + 1][n + 1] ==-1)
                i++;
           a[m][n] = i;
          }
       }
    }
}    public void actionPerformed(ActionEvent e) {           //点击事件监听的方法
    ImageIcon ig=new ImageIcon("lei.GIF");
    int i=0;
    int j=0;
    if(a[i][j]==-1){             //如果是地雷
    for(i=0;i<9;i++){          //把所有按钮都显示出来
        for(j=0;j<9;j++){
           if(a[i][j]==-1){            
           jb1[i][j].setIcon(ig);
           }else 
               if(a[i][j]==0){         //如果该位置为空(该位置不是地雷,周围8个位置也没有地雷)
               jb1[i][j].setText("");
               }else
               {                   //如果该位置不是地雷,但周围8个位置中有地雷
               jb1[i][j].getText();
               }
          }
       }
     }else{                     
     showEmpty(i,j);         //执行排空操作
     }
    }  void showEmpty(int x ,int y){                   //展开空白
       if(a[x][y]==0){
            if(x-1>=0&&y-1>=0){
                 showEmpty(x-1,y-1);
            }
           if(x-1>=0){
                 showEmpty(x-1,y);
           }
           if(x-1>=0&&y+1<9){
                showEmpty(x-1,y+1);
          }
          if(y-1>=0){
               showEmpty(x,y-1);
          }
          if(y+1<9){
              showEmpty(x,y+1);
          }
         if(x+1<9&&y-1>=0){
              showEmpty(x+1,y-1);
         }
         if(x+1<9){
             showEmpty(x+1,y);
        }
        if(x+1<9&&y+1<9){
            showEmpty(x+1,y+1);
       }
    }
 } public void mouseClicked(MouseEvent e) {           //点击右键显示红旗        
       ImageIcon im=new ImageIcon("qi.GIF");
       for(int k = 0;k<= 9;k++) {
            for(int l = 0; l<= 9;l++){ 
                 if (e.getModifiers() == MouseEvent.BUTTON3) {   
                      jb1[k][l].setIcon(im);
                 }
             }
        }
 }
public void mouseEntered(MouseEvent e) {}public void mouseExited(MouseEvent e) {}public void mousePressed(MouseEvent e) {}public void mouseReleased(MouseEvent e) {}
    public static void main(String[] argus)
    {
           new sao();
    }
}

解决方案 »

  1.   


    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;public class soa implements MouseListener, ActionListener {    JButton jb1[][] = new JButton[9][9];
        ImageIcon ig = new ImageIcon("lei.GIF");
        int a[][];
        boolean canShow[][];
        int number;
        int virtualNumber;
        boolean enabled;
        JFrame f_main = new JFrame("扫雷");
        JPanel jp1 = new JPanel();
        JPanel jp2 = new JPanel();
        JPanel jp3 = new JPanel();
        JTextField jt1 = new JTextField("0", 5);
        JTextField jt2 = new JTextField("10", 5);
        JButton jb = new JButton("开始");
        soa() {        
            f_main.setLocation(400, 200);
            jb.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
                    JButton bun=(JButton)e.getSource();
                    if(bun.getText().contains("开始")){
                        enabled=true;
                        bun.setText("结束");
                        for(int i=0;i<9;i++){
                            for(int j=0;j<9;j++){
                                jb1[i][j].setText("");
                                jb1[i][j].setForeground(Color.BLACK);
                                jb1[i][j].setBackground(Color.yellow);
                                jb1[i][j].setIcon(null);
                                jb1[i][j].setText(null);
                            }
                        }
                        initialize();
                        jt1.setText(0+"");
                    }
                    else{
                        enabled=false;
                        bun.setText("开始");
                        showAll();
                    }
                }
            });        JMenuBar menub = new JMenuBar();
            JMenu j1 = new JMenu("游戏");
            JMenu j2 = new JMenu("帮助");
            JMenu j3 = new JMenu("关于作者");
            JMenuItem jm1 = new JMenuItem("初级");
            JMenuItem jm2 = new JMenuItem("中级");
            JMenuItem jm3 = new JMenuItem("高级");
            JMenuItem jm4 = new JMenuItem("开局");
            JMenuItem jm5 = new JMenuItem("退出");
            JMenuItem jm6 = new JMenuItem("帮助");
            JMenuItem jm7 = new JMenuItem("扫雷游戏");        menub.add(j1); //向菜单栏中添加菜单
            menub.add(j2);
            menub.add(j3);        j1.add(jm4);
            j1.addSeparator();
            j1.add(jm1);
            j1.add(jm2);
            j1.add(jm3);
            j1.addSeparator();
            j1.add(jm5);
            j2.add(jm6);
            j3.add(jm7);
            f_main.setJMenuBar(menub);
            JDialog d1 = new JDialog(f_main, "恭喜", true);
            JButton b1 = new JButton("确定");
            JLabel l1 = new JLabel("恭喜通关,再来一局吗?");        d1.add(l1, "Center");
            d1.add(b1);
            d1.setVisible(false);        jp2.setLayout(new GridLayout(9, 9));
            jp1.setBackground(Color.orange);
            jp1.setSize(300, 50);
            jp2.setBackground(Color.gray);
            for (int i = 0; i < 9; i++) {
                for (int j = 0; j < 9; j++) {
                    jb1[i][j] = new JButton();  //"("+i+","+j+")"
                    jb1[i][j].setPreferredSize(new Dimension(60,30));
                    jb1[i][j].addMouseListener(this);
    //                jb1[i][j].addActionListener(this);
                }
            }
            enabled=false;
            for (int n = 0; n < 9; n++) {
                for (int m = 0; m < 9; m++) {
                    jp2.add(jb1[n][m]);
                }
            }        jp3.setLayout(new BorderLayout());
            jp3.add(jp1,BorderLayout.NORTH);
            jp3.add(jp2,BorderLayout.CENTER);
            jp1.add(jt1);
            jp1.add(jb);
            jp1.add(jt2);
            f_main.add(jp3);
            f_main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f_main.pack();
            f_main.setVisible(true);        jm6.addActionListener(new ActionListener() {            public void actionPerformed(ActionEvent e) {
                    JOptionPane.showMessageDialog(null, "本人初学,参照课本资料和网上实例编写,请多多指教", "帮助主题", JOptionPane.PLAIN_MESSAGE);
                }
            });        jm7.addActionListener(new ActionListener() {            public void actionPerformed(ActionEvent e) {
                    JOptionPane.showMessageDialog(null, "此游戏仿制Windows中的扫雷程序而写,具体请参照Window扫雷!", "扫雷游戏", JOptionPane.PLAIN_MESSAGE);
                }
            });        jm5.addActionListener(new ActionListener() {            public void actionPerformed(ActionEvent e) {
                    System.exit(0);
                }
            });
        }
        public void initialize(){
            a=new int[9][9];
            canShow=new boolean[9][9];
            for(int i=0;i<9;i++){
                for(int j=0;j<9;j++){
                    a[i][j]=0;
                    canShow[i][j]=false;
                }
            }
            number=0;
            virtualNumber=0;
            bulei();
        }
        public void showCanShow(){
            for(int i=0;i<9;i++){
                for(int j=0;j<9;j++){
                    if(canShow[i][j]){
                        canShow[i][j]=false;
                        jb1[i][j].setBackground(Color.WHITE);
                        if(a[i][j]!=0){
                            jb1[i][j].setText(a[i][j]+"");
                        }
                    }
                }
            }
        }
        void bulei() { //随机生雷
            for (int i = 0; i < 10; ) {
                int x = (int) (Math.random() * 9);
                int y = (int) (Math.random() * 9);
                if (a[x][y] == 0) {
                    a[x][y] = -1;
                    i++;
                }            
            }
            leishu();
        }    void win() { //找到的地雷数
            int findBomb = 0;
            for (int c = 0; c < 9; c++) {
                for (int b = 0; b < 9; b++) {
                    if (a[c][b] == -1) {
                        findBomb++;
                    }
                }
            }
            if (findBomb == 10) {
                JOptionPane.showMessageDialog(null, "YOU WIN", "YOUWIN", JOptionPane.PLAIN_MESSAGE);
            }
        }    void leishu() { //计算周围地雷数
            for (int m = 0; m < 9; m++) {
                for (int n = 0; n < 9; n++) {
                    if (a[m][n] != -1) {
                        int i = 0;
                        if ( m-1>=0 && n-1>=0 &&a[m - 1][n - 1] == -1) {
                            i++;
                        }
                        if ( m-1>=0 && a[m - 1][n] == -1) {
                            i++;
                        }
                        if ( m-1>=0 && n+1<9 && a[m - 1][n + 1] == -1) {
                            i++;
                        }
                        if ( n-1>=0 && a[m][n - 1] == -1) {
                            i++;
                        }
                        if ( n+1<9 && a[m][n + 1] == -1) {
                            i++;
                        }
                        if ( m+1<9 && n-1>=0 && a[m + 1][n - 1] == -1) {
                            i++;
                        }
                        if ( m+1<9 && a[m + 1][n] == -1) {
                            i++;
                        }
                        if ( m+1<9 && n+1<9 && a[m + 1][n + 1] == -1) {
                            i++;
                        }
                        a[m][n] = i;
                    }
                }
            }
        }
      

  2.   

        public void actionPerformed(ActionEvent e) { //点击事件监听的方法
            
        }    void showEmpty(int x, int y) { //展开空白
            if(canShow[x][y]){
                return;
            }
            if (a[x][y] == 0) {
                canShow[x][y]=true;
                if (x - 1 >= 0) {
                    showEmpty(x - 1, y);
                }
                if (y - 1 >= 0) {
                    showEmpty(x, y - 1);
                }
                if (y + 1 < 9) {
                    showEmpty(x, y + 1);
                }
                if (x + 1 < 9) {
                    showEmpty(x + 1, y);
                }
    //            jb1[x][y].setBackground(Color.WHITE);
            }
            else if(a[x][y]!=-1){
                canShow[x][y]=true;
    //            jb1[x][y].setBackground(Color.WHITE);
    //            jb1[x][y].setText(a[x][y]+"");
            }
        }
        public void showAll(){
            for(int i=0;i<9;i++){
                for(int j=0;j<9;j++){
                    jb1[i][j].setBackground(Color.WHITE);
                    if( a[i][j]==-1 && jb1[i][j].getIcon()==null ){
                        jb1[i][j].setForeground(Color.red);
                        jb1[i][j].setText(a[i][j]+"");
                    }
                    else if(a[i][j]>0){
                        jb1[i][j].setText(a[i][j]+"");
                        if(jb1[i][j].getIcon()!=null){
                            jb1[i][j].setIcon(null);
                            jb1[i][j].setForeground(Color.GREEN);
                        }
                    }
                }
            }
        }
        public void mouseClicked(MouseEvent e) { //点击右键显示红旗
            
        }    public void mouseEntered(MouseEvent e) {
        }    public void mouseExited(MouseEvent e) {
        }    public void mousePressed(MouseEvent e) {
            if(!enabled){
                return;
            }
            int i = 0;
            int j = 0;
            JButton bun=(JButton)e.getSource();
            boolean flag=false;
            for(i=0;i<jb1.length;i++){
                for(j=0;j<jb1[i].length;j++){
                    if(bun.equals(jb1[i][j])){
                        flag=true;
                        break;
                    }
                }
                if(flag){
                    break;
                }
            }
            System.out.println("i:"+i+",j:"+j);
            //如果是左击,左击是确认此处无雷
            if(e.getButton()==MouseEvent.BUTTON1){
                if (a[i][j] == -1) { //如果是地雷
                    jb.setText("开始");
                    showAll();
                }
                else if(a[i][j]!=0){
                    jb1[i][j].setText(a[i][j]+"");
                    jb1[i][j].setBackground(Color.WHITE);
                }
                else {
                    showEmpty(i, j); //执行排空操作
                    showCanShow();
                }
            }
            else if(e.getButton()==MouseEvent.BUTTON3){
                if(bun.getIcon()==null){
                    bun.setIcon(ig);
                    bun.setText(null);
                    virtualNumber++;
                    jt1.setText(""+virtualNumber);
                    if(a[i][j]==-1){
                        number++;
                        if(number==10){
                            JOptionPane.showMessageDialog(null, "YOU WIN", "YOUWIN", JOptionPane.PLAIN_MESSAGE);
                            enabled=true;
                            bun.setText("结束");
                            for(int i0=0;i0<9;i0++){
                                for(int j0=0;j0<9;j0++){
                                    jb1[i0][j0].setText("");
                                    jb1[i0][j0].setForeground(Color.BLACK);
                                    jb1[i0][j0].setBackground(Color.yellow);
                                    jb1[i0][j0].setIcon(null);
                                    jb1[i0][j0].setText(null);
                                }
                            }
                            initialize();
                            jt1.setText(0+"");
                        }
                    }
                }else{
                    bun.setIcon(null);
                    virtualNumber--;
                    jt1.setText(""+virtualNumber);
                    if(a[i][j]==-1){
                        number--;
                    }
                }
            }
        }    public void mouseReleased(MouseEvent e) {
        }    public static void main(String[] argus) {
            new soa();
        }
    }
      

  3.   

    改的匆忙,代码有点乱。对一些不合理操作也没处理。大体上还行。说一下玩法:
    1。初始时是点击无效的。点击开始,分部地雷,重新显示,监听点击事件。左击表示确认此处无雷,右击标记此处为地雷,再次右击可以取消标记的地雷。标记对所有地雷后显示成功,确认后重新开始。
    2。点击结束可以显示出地雷分布。如果左击地雷,也显示地雷分布。
    如有问题,可以联系我QQ:815611030(注明:Java)