⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mineframe.java

📁 模仿window自带的小游戏扫雷编的,很简单,只实现了扫雷的基本功能,现拿出来与大家分享!
💻 JAVA
字号:

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.GridLayout;
import java.awt.FlowLayout;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
        
public class MineFrame extends JFrame implements ActionListener,MouseListener {
    JButton reStart;
    Block[][] block;
    BlockView[][] blockView;
    LayMines lay;
    int row=10,colum=10,mineCount=15;//行列雷
    JPanel pCenter,pNorth;//****************************
    int tt=1;
    
    Icon icon1=new ImageIcon("位图1/截图红旗.jpg");
    Icon icon2=new ImageIcon("位图1/截图问号.jpg");
    Icon icon3=null;
    Icon icon4=new ImageIcon("位图1/雷.jpg");
    Icon icon11=new ImageIcon("位图1/截图4.jpg");
    Icon icon12=new ImageIcon("位图1/截图05.jpg");
    Icon icon13=new ImageIcon("位图1/截图06.jpg");
    Icon icon14=new ImageIcon("位图1/截图07.jpg");
    Icon icon15=new ImageIcon("位图1/截图08.jpg");
    Icon icon16=new ImageIcon("位图1/截图09.jpg");
    Icon icon17=new ImageIcon("位图1/截图10.jpg");
    Icon icon18=new ImageIcon("位图1/截图11.jpg");
    Icon icon19=new ImageIcon("位图1/截图12.jpg");
    
    /** Creates a new instance of MineFrame */
    public MineFrame(String title) {
        setTitle(title);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        reStart=new JButton("重新开始");        
        pCenter=new JPanel();
        pNorth=new JPanel();
        pNorth.setBackground(Color.red);
        block=new Block[row][colum];
                 
        for(int i=0;i<row;i++){
            for(int j=0;j<colum;j++){
                block[i][j]=new Block();
            }
        }
        lay=new LayMines();
        lay.layMinesForBlock(block,mineCount);
        blockView=new BlockView[row][colum];
        pCenter.setLayout(new GridLayout(row,colum));
        
        
        for(int i=0;i<row;i++){
            for(int j=0;j<colum;j++){
                blockView[i][j]=new BlockView();
                pCenter.add(blockView[i][j]);
                blockView[i][j].getBlockCover().addMouseListener(this); 
            }
        }
        
        initGame();
        reStart.addActionListener(this);
        pNorth.add(reStart);//**********************************************
        getContentPane().add(pNorth,BorderLayout.NORTH);
        getContentPane().add(pCenter,BorderLayout.CENTER);
        validate();
    }//构函数
 //*****************************************************************************   
    
    public void mouseClicked(MouseEvent arg0) { }
    public void mouseEntered(MouseEvent arg0) { }
    public void mouseExited(MouseEvent arg0) { }
    public void mouseReleased(MouseEvent arg0) { }
    public void mousePressed(MouseEvent arg0){
        
        JButton b=(JButton)arg0.getSource();
        if(arg0.getButton()==MouseEvent.BUTTON3){        	                         
            if(b.getBackground().getRGB()==Color.PINK.getRGB()){                      
                b.setBackground(Color.CYAN);     	
                b.setIcon(icon1);
    
            }
            else if(b.getBackground().getRGB()==Color.CYAN.getRGB()){
        
            	b.setBackground(Color.BLACK);
                b.setIcon(icon2);
            
            }
            else if(b.getBackground().getRGB()==Color.BLACK.getRGB()){
            	tt=1;
            	b.setBackground(Color.PINK);
                b.setIcon(null);
            }      
       }
    }
 //*************************************************************************   
    public void actionPerformed(ActionEvent e){
       
        JButton source=(JButton)e.getSource();
        if(source!=reStart){
            int m=-1,n=-1;
            for(int i=0;i<row;i++){
                for(int j=0;j<colum;j++){
                    if(source==blockView[i][j].getBlockCover()){
                        m=i;
                        n=j;
                        break;
                    }
                }
            }
            if(block[m][n].getIsMine()){//为雷
                for(int i=0;i<row;i++){
                    for(int j=0;j<colum;j++){
                        blockView[i][j].getBlockCover().removeActionListener(this);
                        if(block[i][j].getIsMine()){
                            blockView[i][j].showBlockName();
                        }
                    }
                }
            }
            else{//非雷 循环遍历为空的位置并显示出来
                if(block[m][n].getNumber()>0){
                    blockView[m][n].showBlockName();
                }
                else if(block[m][n].getNumber()==0){
                    for(int k=Math.max(m-1,0);k<=Math.min(m+1,row-1);k++){
                        for(int t=Math.max(n-1,0);t<=Math.min(n+1,colum-1);t++){
                            blockView[k][t].showBlockName();
                            
                            if(block[k][t].getNumber()==0){
                                for(int k1=Math.max(k-1,0);k1<=Math.min(k+1,row-1);k1++){
                                    for(int t1=Math.max(t-1,0);t1<=Math.min(t+1,colum-1);t1++){
                                        blockView[k1][t1].showBlockName();
                                        
                                        if(block[k1][t1].getNumber()==0){
                                             for(int k2=Math.max(k1-1,0);k2<=Math.min(k1+1,row-1);k2++){
                                                 for(int t2=Math.max(t1-1,0);t2<=Math.min(t1+1,colum-1);t2++){
                                                     blockView[k2][t2].showBlockName();
                                                     
                                                     if(block[k2][t2].getNumber()==0){
                                                         for(int k3=Math.max(k2-1,0);k3<=Math.min(k2+1,row-1);k3++){
                                                             for(int t3=Math.max(t2-1,0);t3<=Math.min(t2+1,colum-1);t3++){
                                                                 blockView[k3][t3].showBlockName();
                                                                 
                                                                 if(block[k3][t3].getNumber()==0){
                                                                     for(int k4=Math.max(k3-1,0);k4<=Math.min(k3+1,row-1);k4++){
                                                                         for(int t4=Math.max(t3-1,0);t4<=Math.min(t3+1,colum-1);t4++){
                                                                             blockView[k4][t4].showBlockName();
                                                                             
                                                                             if(block[k4][t4].getNumber()==0){
                                                                                 for(int k5=Math.max(k4-1,0);k5<=Math.min(k4+1,row-1);k5++){
                                                                                     for(int t5=Math.max(t4-1,0);t5<=Math.min(t4+1,colum-1);t5++){
                                                                                         blockView[k5][t5].showBlockName();
                                                                                     }
                                                                                 }
                                                                             }
                                                                         }
                                                                     }
                                                                 }
                                                             }
                                                         }
                                                     }
                                                 }
                                             }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        if(source==reStart){
            initGame(); 
        }
        } 
        
  //**************************************************************************    
  
  public void initGame(){
             for(int i=0;i<row;i++){
                    for(int j=0;j<colum;j++){
                        block[i][j].setIsMine(false);
                    }
                }
                lay.layMinesForBlock(block,mineCount);//重新布雷
                for(int i=0;i<row;i++){
                    for(int j=0;j<colum;j++){
                    	 blockView[i][j].getBlockCover().setIcon(null);
                    	
                    	 if(block[i][j].getName().equals("")){//*******************
                    	     blockView[i][j].getBlockName().setIcon(icon3);
                    	 }
                    	 else if(block[i][j].getName().equals("*")){
                    	 	 blockView[i][j].getBlockName().setIcon(icon4);
                    	 }
                    	 else if(block[i][j].getName().equals("1")){
                    	  	 blockView[i][j].getBlockName().setIcon(icon11);
                    	 }
                    	 else if(block[i][j].getName().equals("2")){
                    	 	 blockView[i][j].getBlockName().setIcon(icon12);
                    	 }
                    	 else if(block[i][j].getName().equals("3")){
                    	 	 blockView[i][j].getBlockName().setIcon(icon13);
                    	 }
                    	 else if(block[i][j].getName().equals("4")){
                    	 	 blockView[i][j].getBlockName().setIcon(icon14);
                    	 }
                    	 else if(block[i][j].getName().equals("5")){
                    	 	 blockView[i][j].getBlockName().setIcon(icon15);
                    	 }
                    	 else if(block[i][j].getName().equals("6")){
                    	 	 blockView[i][j].getBlockName().setIcon(icon16);
                    	 }
                    	 else if(block[i][j].getName().equals("7")){
                    	 	 blockView[i][j].getBlockName().setIcon(icon17);
                    	 } 
                    	 else if(block[i][j].getName().equals("8")){
                    	 	 blockView[i][j].getBlockName().setIcon(icon18);
                    	 }
                    	 else if(block[i][j].getName().equals("9")){
                    	 	 blockView[i][j].getBlockName().setIcon(icon19);
                    	 }
                    	 else{}
                         blockView[i][j].showBlockCover();
                         blockView[i][j].getBlockCover().addActionListener(this);
                         blockView[i][j].getBlockCover().setBackground(Color.PINK);
                    }
               }	
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -