常用的语句:
1.关闭窗口则工程构建完成
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
2.当前窗体相对于显示器(null)居中
this.setLocationRelativeTo(null);
3.添加点击事件监听器
this.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
...
}
});
4.控件放在哪个区域
this.add(xXX, BorderLayout.NORTH);
this.add(xXX, BorderLayout.CENTER);
this.add(xXX, BorderLayout.SOUTH);
5.添加鼠标监听器
this.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
...
}
});
完整的程序:
Game.java
package team;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
public class Game extends JFrame {
private final JPanel jPanelMainField; //容器1
private final JPanel jPanelButtonField; //容器2
private final Cell[][] cellBoard; //Cell类是内部类
private final JButton jButtonReset; //重置按钮
private final int landscapedSize; //主窗体的横向长度
private final int portraitedSize; //主窗体的纵向长度
private final int landscapedCellNum; //横向格子数目
private final int portraitedCellNum; //纵向格子数目
private final int mineNum; //地雷数目
public static void main(String args[]) {
new Game(600, 600, 20, 20, 60);
}
public Game(int landscapedSize, int portraitedSize, int landscapedCellNum,
int portraitedCellNum, int mineNum) {
this.landscapedSize = landscapedSize;
this.portraitedSize = portraitedSize;
this.landscapedCellNum = landscapedCellNum;
this.portraitedCellNum = portraitedCellNum;
this.mineNum = mineNum;
this.setTitle("扫雷游戏");
this.setSize(this.landscapedSize, this.portraitedSize);
this.setResizable(false); //窗体大小固定
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //窗体关闭则工程构建完成
this.setLocationRelativeTo(null); //窗体相对于显示器居中
this.cellBoard = new Cell[landscapedCellNum + 2][portraitedCellNum + 2];
for (int i = 0; i < landscapedCellNum + 2; i++) {
this.cellBoard[i][0] = new Cell(false);
this.cellBoard[i][portraitedCellNum+1] = new Cell(false);
}
for (int i = 1; i < portraitedCellNum + 1; i++) {
this.cellBoard[0][i] = new Cell(false);
this.cellBoard[landscapedCellNum+1][i] = new Cell(false);
}
this.jPanelMainField = new JPanel();
for (int i = 1; i < landscapedCellNum + 1; i++) {
for (int j = 1; j < portraitedCellNum + 1; j++) {
Random r = new Random();
//按照格子数和地雷数的比例
int flag = r.nextInt(1000) %
(landscapedCellNum * portraitedCellNum / mineNum);
if(flag == 0) {
this.cellBoard[i][j] = new Cell(true); //地雷
} else {
this.cellBoard[i][j] = new Cell(false);
}
this.cellBoard[i][j].setX(i); //设置横坐标
this.cellBoard[i][j].setY(j); //设置纵坐标
this.jPanelMainField.add(cellBoard[i][j]);
}
}
this.jPanelMainField.setLayout(new GridLayout(landscapedCellNum,
portraitedCellNum, 2, 2)); //设置面板的布局,即格子均匀分布
this.add(this.jPanelMainField, BorderLayout.CENTER); //主面板放在界面中央
this.jPanelButtonField = new JPanel();
this.jButtonReset = new JButton("重置");
this.jButtonReset.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Game.this.dispose();
new Game(Game.this.landscapedSize, Game.this.portraitedSize,
Game.this.landscapedCellNum, Game.this.portraitedCellNum,
Game.this.mineNum);
}
});
this.jPanelButtonField.add(this.jButtonReset);
this.add(this.jPanelButtonField, BorderLayout.SOUTH); //按钮面板放在界面下方
this.setVisible(true);
}
public class Cell extends JPanel {
private int x,y;
private int num; //周围的地雷数目
private boolean isMine; //格子是地雷
public void setX(int x) { this.x = x; }
public void setY(int y) { this.y = y; }
public boolean getIsMine() { return this.isMine; }
public Cell(boolean isMine) {
this.num = 0;
this.isMine = isMine;
this.setBackground(Color.GRAY); //格子未翻开时是灰色
this.addMouseListener(new MouseAdapter() { //添加鼠标点击监听器
@Override
public void mousePressed(MouseEvent e) {
if(Cell.this.isMine) {
Cell.this.setBackground(Color.RED);
JOptionPane.showMessageDialog(null, "挖到雷了!");
}else {
Cell.this.setBackground(Color.WHITE); //格子翻开后是白色
Cell.this.num += Game.this.cellBoard[x-1][y-1].getIsMine()
==true ? 1 : 0;
Cell.this.num += Game.this.cellBoard[x-1][y+1].getIsMine()
==true ? 1 : 0;
Cell.this.num += Game.this.cellBoard[x+1][y-1].getIsMine()
==true ? 1 : 0;
Cell.this.num += Game.this.cellBoard[x+1][y+1].getIsMine()
==true ? 1 : 0;
Cell.this.num += Game.this.cellBoard[x-1][y].getIsMine()
==true ? 1 : 0;
Cell.this.num += Game.this.cellBoard[x][y+1].getIsMine()
==true ? 1 : 0;
Cell.this.num += Game.this.cellBoard[x+1][y].getIsMine()
==true ? 1 : 0;
Cell.this.num += Game.this.cellBoard[x][y-1].getIsMine()
==true ? 1 : 0;
Label label = new Label(); //用标签组件来显示周围地雷数目
label.setBackground(Color.WHITE);
int temp = Cell.this.num;
switch(temp) { //地雷数目不同颜色不同
case 1: label.setForeground(Color.GREEN); break;
case 2: label.setForeground(Color.BLUE); break;
case 3: label.setForeground(Color.RED); break;
case 4: label.setForeground(Color.ORANGE); break;
case 5: label.setForeground(Color.PINK); break;
case 6: label.setForeground(Color.MAGENTA); break;
case 7: label.setForeground(Color.YELLOW); break;
case 8: label.setForeground(Color.CYAN); break;
}
if (temp != 0) { //不显示0数字
label.setText(String.valueOf(temp));
}
label.setSize(Game.this.landscapedSize / Game.this.landscapedCellNum - 10,
Game.this.portraitedSize / Game.this.portraitedCellNum - 10);
label.setAlignment(1); //居中
Cell.this.add(label);
}
}
});
}
}
}
实际运行截图: