各位大哥大姐好,刚注册完,发个开窗户游戏源码来,高手们别笑喔!
无法粘附件上面,只好直接粘源码了
100% 纯 JAVA 编写, 可以在任何系统上运行,比如苹果电脑和LINUX等
以下是源代码
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class OpenWindows{
public static void main(String[] args){
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame=new JFrame("开窗户游戏");
frame.getContentPane().add(new Mainpanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
class Mainpanel extends JPanel{
PanelC pc=new PanelC();
PanelS ps=new PanelS(pc);
public Mainpanel(){
this.setLayout(new BorderLayout());
this.add(pc,"Center");
this.add(ps,"South");
}
}
//方块面板
class PanelC extends JPanel{
JButton[] jb=new JButton[25];
Color c;
public PanelC(){
this.setLayout(new GridLayout(5,5));
for(int i=0;i<25;i++){
jb[i]=new JButton();
jb[i].setActionCommand(String.valueOf(i));
c=jb[i].getBackground(); //获得默认颜色
jb[i].addActionListener(new OpenOther());
this.add(jb[i]);
}
this.setPreferredSize(new Dimension(300,300));
}
class OpenOther implements ActionListener{
public void actionPerformed(ActionEvent a){
String y=a.getActionCommand();
int x=Integer.parseInt(y);
Select(x);
IsWin();
}
//判断九种情况
private void Select(int x){
if(x==0){
ChangeColor(jb[x]);
ChangeColor(jb[x+1]);
ChangeColor(jb[x+5]);
}else if(x>0 && x<4){
ChangeColor(jb[x]);
ChangeColor(jb[x-1]);
ChangeColor(jb[x+1]);
ChangeColor(jb[x+5]);
}else if(x==4){
ChangeColor(jb[x]);
ChangeColor(jb[x-1]);
ChangeColor(jb[x+5]);
}else if(x==20){
ChangeColor(jb[x]);
ChangeColor(jb[x-5]);
ChangeColor(jb[x+1]);
}else if(x==24){
ChangeColor(jb[x]);
ChangeColor(jb[x-5]);
ChangeColor(jb[x-1]);
}else if(x>20 && x<24){
ChangeColor(jb[x]);
ChangeColor(jb[x-5]);
ChangeColor(jb[x-1]);
ChangeColor(jb[x+1]);
}else if(x%5==0){
ChangeColor(jb[x]);
ChangeColor(jb[x-5]);
ChangeColor(jb[x+1]);
ChangeColor(jb[x+5]);
}else if(x%5==4){
ChangeColor(jb[x]);
ChangeColor(jb[x-5]);
ChangeColor(jb[x-1]);
ChangeColor(jb[x+5]);
}else{
ChangeColor(jb[x]);
ChangeColor(jb[x-5]);
ChangeColor(jb[x-1]);
ChangeColor(jb[x+1]);
ChangeColor(jb[x+5]);
}
}
//改变周围颜色函数
private void ChangeColor(JButton jb){
if(jb.getBackground()==c)
jb.setBackground(Color.white);
else
jb.setBackground(c);
}
//判断是否胜出
private void IsWin(){
int a=1;
for(int i=0;i<25;i++)
if(jb[i].getBackground()==Color.white)
a++;
if(a>25)
JOptionPane.showMessageDialog(null,"恭喜过关");
}
}
}
class PanelS extends JPanel{
JLabel jl=new JLabel("开窗户游戏");
JButton jbS=new JButton("重置");
PanelC pc;
public PanelS(PanelC pc){
this.pc=pc;
jbS.addActionListener(new Reset());
this.add(jl);
this.add(jbS);
}
class Reset implements ActionListener{
public void actionPerformed(ActionEvent a){
for(int i=0;i<25;i++){
pc.jb[i].setBackground(pc.c);
}
}
}
}
本文地址:http://com.8s8s.com/it/it9774.htm