~java完整代码如下~
package three; //包名随意
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel; //系统导入类
public class Playgame {
public static void main(String[] args) {
JFrame frame = new JFrame("开窗户小游戏");
frame.getContentPane().add(new MainPanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
class MainPanel extends JPanel {
SquarePanel pc = new SquarePanel();
ControlPanel ps = new ControlPanel(pc); //用类实例化的对象
public MainPanel() {
this.setLayout(new BorderLayout());
this.add(pc, "Center");
this.add(ps, "South");
}
}
class SquarePanel extends JPanel {
JButton[] winbutton = new JButton[25]; //包括25个窗户按钮
Color c;
public SquarePanel() {
this.setLayout(new GridLayout(5, 5)); //面板采用网格布局管理器
for (int i = 0; i < 25; i++) {
winbutton[i] = new JButton();
winbutton[i].setActionCommand(String.valueOf(i));
c = winbutton[i].getBackground(); //获得默认颜色
winbutton[i].addActionListener(new OpenWindowListener());
this.add(winbutton[i]);
}
this.setPreferredSize(new Dimension(400, 400)); //面板大小值为400*400
}
class OpenWindowListener implements ActionListener {
public void actionPerformed(ActionEvent a) {
int x = Integer.parseInt(a.getActionCommand()); //获取被点击窗户的ID
select(x); //设置被点击窗户被选。
isWin(); //判断是否胜利
}
private void select(int x) { //当一个窗户被选中时进行的操作。需要改变周围窗户的颜色。
if (x == 0) {
changeColor(winbutton[x]);
changeColor(winbutton[x + 1]);
changeColor(winbutton[x + 5]);
} else if (x > 0 && x < 4) {
changeColor(winbutton[x]);
changeColor(winbutton[x - 1]);
changeColor(winbutton[x + 1]);
changeColor(winbutton[x + 5]);
} else if (x == 4) {
changeColor(winbutton[x]);
changeColor(winbutton[x - 1]);
changeColor(winbutton[x + 5]);
} else if (x == 20) {
changeColor(winbutton[x]);
changeColor(winbutton[x - 5]);
changeColor(winbutton[x + 1]);
} else if (x == 24) {
changeColor(winbutton[x]);
changeColor(winbutton[x - 5]);
changeColor(winbutton[x - 1]);
} else if (x > 20 && x < 24) {
changeColor(winbutton[x]);
changeColor(winbutton[x - 5]);
changeColor(winbutton[x - 1]);
changeColor(winbutton[x + 1]);
} else if (x % 5 == 0) {
changeColor(winbutton[x]);
changeColor(winbutton[x - 5]);
changeColor(winbutton[x + 1]);
changeColor(winbutton[x + 5]);
} else if (x % 5 == 4) {
changeColor(winbutton[x]);
changeColor(winbutton[x - 5]);
changeColor(winbutton[x - 1]);
changeColor(winbutton[x + 5]);
} else {
changeColor(winbutton[x]);
changeColor(winbutton[x - 5]);
changeColor(winbutton[x - 1]);
changeColor(winbutton[x + 1]);
changeColor(winbutton[x + 5]);
}
}
private void changeColor(JButton winbutton) { // 改变周围颜色函数。
if (winbutton.getBackground() == c){ //如果窗户的颜色是初始颜色,则变成白色。
winbutton.setBackground(Color.white);
} else { //如果窗户的颜色不是初始颜色,则变成初始颜色
winbutton.setBackground(c);
}
}
private void isWin() { // 判断是否胜利
int a = 1;
for (int i = 0; i < 25; i++){ //当25个窗户都变成白色时,获胜
if (winbutton[i].getBackground() == Color.white){
a++;
}
}
if (a > 25){
JOptionPane.showMessageDialog(null, "恭喜过关");
}
}
}
}
class ControlPanel extends JPanel {
JLabel label = new JLabel("小提示:全变白色获胜");
JButton restart = new JButton("你也可以选择重新开始"); //游戏重新开始按钮
SquarePanel pc;
public ControlPanel(SquarePanel pc) {
this.pc = pc;
restart.addActionListener(new Reset());
this.add(label);
this.add(restart);
}
class Reset implements ActionListener {
public void actionPerformed(ActionEvent a) {
for (int i = 0; i < 25; i++) {
pc.winbutton[i].setBackground(pc.c);
}
}
}
}
运行结果:
初始运行界面
游戏进行时界面
蛮好玩的小游戏,要想赢也不容易哈。感兴趣的朋友,可以自己试一下下哈。