📄 mine.java
字号:
package game;
import java.awt.event.*;
import java.awt.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Mine implements ActionListener{
private JFrame frame;
private Container contentPane;
private int rows;
private int cols;
private MyButton[][] btn;
private int sum;//真实地雷数
public void actionPerformed(ActionEvent e){
for(int i=0;i<rows;i++)
for(int j=0;j<cols;j++){
//System.out.println(i+" "+j);
if (e.getSource()==btn[i][j].getBtn()){
if (btn[i][j].getBtnValue()){
btn[i][j].getBtn().setText("★");
showAll();
}
else
showOne(i,j);
}
}
}
public void showAll(){
for(int i=0;i<rows;i++)
for(int j=0;j<cols;j++)
if (!btn[i][j].getBtnValue())
showOne(i,j);
}
public boolean checkXY(int x,int y ){
return x>=0 && x<rows && y>=0 && y<cols;
}
public void showOne(int x,int y){
if(x<0 || x>=rows || y<0 || y>=cols)
return;
if (!btn[x][y].getBtn().isEnabled())
return;
int countLei=0;
if (checkXY(x-1,y-1) && btn[x-1][y-1].getBtnValue())
countLei++;
if (checkXY(x-1,y) && btn[x-1][y].getBtnValue())
countLei++;
if (checkXY(x-1,y+1) && btn[x-1][y+1].getBtnValue())
countLei++;
if (checkXY(x,y-1) && btn[x][y-1].getBtnValue())
countLei++;
if (checkXY(x,y+1) && btn[x][y+1].getBtnValue())
countLei++;
if (checkXY(x+1,y-1) && btn[x+1][y-1].getBtnValue())
countLei++;
if (checkXY(x+1,y) && btn[x+1][y].getBtnValue())
countLei++;
if (checkXY(x+1,y+1) && btn[x+1][y+1].getBtnValue())
countLei++;
btn[x][y].getBtn().setEnabled(false);
if (countLei==0){
btn[x][y].getBtn().setText("");
showOne(x-1,y-1);
showOne(x-1,y);
showOne(x-1,y+1);
showOne(x,y-1);
showOne(x,y+1);
showOne(x+1,y-1);
showOne(x+1,y);
showOne(x+1,y+1);
}
else {
btn[x][y].getBtn().setText(countLei+"");
}
}
public Mine(int rows,int cols,int sum){
this.rows=rows;
this.cols=cols;
this.sum=sum;
frame=new JFrame();
frame.setSize(rows*45+40,cols*30+40);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = frame.getContentPane();
initGUI();
}
public void initGUI(){
contentPane.setLayout(new BorderLayout());
btn = new MyButton[rows][cols];
for(int i=0;i<rows;i++)
for(int j=0;j<cols;j++){
btn[i][j]=new MyButton();
btn[i][j].setBtn(new JButton());
btn[i][j].setBtnValue(false);
btn[i][j].setFlag(false);
btn[i][j].setFlag1(false);
btn[i][j].getBtn().setText("■");
}
JPanel p=new JPanel(new GridLayout(rows,cols));
for(int i=0;i<rows;i++)
for(int j=0;j<cols;j++){
p.add(btn[i][j].getBtn());
btn[i][j].getBtn().addActionListener(this);
}
contentPane.add(p,BorderLayout.CENTER);
int intx,inty,countx;
countx=0;
while (true){
intx = (int)(Math.random()*rows);
inty = (int)(Math.random()*cols);
//System.out.print(intx+" "+inty);
if (btn[intx][inty].getBtnValue()==false){
btn[intx][inty].setBtnValue(true);
countx++;
if (countx>=sum)
break;
}
}
}
public void go(){
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((int)(d.getWidth()-frame.getWidth())/2,(int)(d.getHeight()-frame.getHeight())/2);
frame.setVisible(true);
}
public static void main(String[] args){
(new Mine(10,10,10)).go();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -