import javax.swing.*;
import java.awt.*;
import java.awt.event.*;public class soa implements MouseListener, ActionListener {     int row;              //行数 
     int col;               //列数 
     int mine;            //地雷数
    JButton jb1[][] ;
    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<row;i++){
                        for(int j=0;j<col;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(row,col));
        jp1.setBackground(Color.orange);
        jp1.setSize(300, 50);
        jp2.setBackground(Color.gray);
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                jb1[i][j] = new JButton();  //"("+i+","+j+")"
                jb1[i][j].setPreferredSize(new Dimension(60,30));
                jb1[i][j].addMouseListener(this);            }
        }
        enabled=false;
        for (int n = 0; n < row; n++) {
            for (int m = 0; m < col; 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[row][col];
        canShow=new boolean[row][col];
        for(int i=0;i<row;i++){
            for(int j=0;j<col;j++){
                a[i][j]=0;
                canShow[i][j]=false;
            }
        }
        number=0;
        virtualNumber=0;
        bulei();
    }
    public void showCanShow(){
        for(int i=0;i<row;i++){
            for(int j=0;j<col;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 < mine; ) {
            int x = (int) (Math.random() * row);
            int y = (int) (Math.random() * col);
            if (a[x][y] == 0) {
                a[x][y] = -1;
                i++;
            }            
        }
        leishu();
    }    void win() { //找到的地雷数
        int findBomb = 0;
        for (int c = 0; c < row; c++) {
            for (int b = 0; b < col; b++) {
                if (a[c][b] == -1) {
                    findBomb++;
                }
            }
        }
        if (findBomb == mine) {
            JOptionPane.showMessageDialog(null, "YOU WIN", "YOUWIN", JOptionPane.PLAIN_MESSAGE);
        }
    }    void leishu() { //计算周围地雷数
        for (int m = 0; m < row; m++) {
            for (int n = 0; n < col; 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<col&& a[m][n + 1] == -1) {
                        i++;
                    }
                    if ( m+1<row && n-1>=0 && a[m + 1][n - 1] == -1) {
                        i++;
                    }
                    if ( m+1<row && a[m + 1][n] == -1) {
                        i++;
                    }
                    if ( m+1<row && n+1<col&& a[m + 1][n + 1] == -1) {
                        i++;
                    }
                    a[m][n] = i;
                }
            }
        }
    } 
 

解决方案 »

  1.   

    public void actionPerformed(ActionEvent e) {              //点击事件监听的方法
       if(e.getActionCommand().equals("初级")){
       this.row=9;
       this.col=9;
       this.mine=10;
       return;
      }
      if(e.getActionCommand().equals("中级")){
       this.row=16;
       this.col=16;
       this.mine=40;
       return;
      }
      if(e.getActionCommand().equals("高级")){
       this.row=16;
       this.col=30;
       this.mine=99;
       return;
      }
     }    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 < col) {
                    showEmpty(x, y + 1);
                }
                if (x + 1 < row) {
                    showEmpty(x + 1, y);
                }        }
            else if(a[x][y]!=-1){
                canShow[x][y]=true;
            }
        }
        public void showAll(){
            for(int i=0;i<row;i++){
                for(int j=0;j<col;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==mine){
                            JOptionPane.showMessageDialog(null, "YOU WIN", "YOUWIN", JOptionPane.PLAIN_MESSAGE);
                            enabled=true;
                            bun.setText("结束");
                            for(int i0=0;i0<row;i0++){
                                for(int j0=0;j0<col;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();
        }
    }
      

  2.   

    贴成java代码模式吧,字体颜色后面那个按钮