谁帮忙给看一下这个程序为什么答不到我要求的呢,这是个开窗户的游戏 就是点一个格周围的格子颜色也变 为什么我点完之后 就这个格子颜色变 而且鼠标离开后就又变回去了呢?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Openwin implements ActionListener
{
JFrame f;
JPanel panel1,panel2;
JButton winbutton[];
JButton reset;
public Openwin()
{
JFrame f=new JFrame("游戏");
f.setBackground(Color.white);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setSize(400,400);
f.setVisible(true);
JPanel panel1=new JPanel();
JPanel panel2=new JPanel();
f.getContentPane().add(panel1,BorderLayout.CENTER);
f.getContentPane().add(panel2,BorderLayout.SOUTH);
panel1.setLayout(new GridLayout(5,5));
JButton[] winbutton=new JButton[25];
panel1.setPreferredSize(new Dimension(300,300));
for(int i=0;i<=24;i++)
{
winbutton[i]=new JButton();
winbutton[i].setBackground(Color.red);
panel1.add(winbutton[i]);
winbutton[i].addActionListener(this);
}
JButton reset=new JButton("重置");
panel2.add(reset);
reset.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==reset)
{
for(int i=0;i<=24;i++)
{
winbutton[i].setBackground(Color.red);
}
}
else
{
String y=e.getActionCommand();//返回与此动作相关的命令字符串。
int x=Integer.parseInt(y);
select(x);
iswin();
}
}
public void iswin()
{
int a=1;
for(int i=0;i<=24;i++)
{
if(winbutton[i].getBackground()==Color.white)
a++;
if(a>25)
JOptionPane.showMessageDialog(null,"恭喜过关,奖励美女一个!");
}
}
public void ChangeColor(JButton winbutton)
{
if(winbutton.getBackground()==Color.red)
winbutton.setBackground(Color.white);
else winbutton.setBackground(Color.red);
}
public 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]);
}
}
public static void main(String args[])
{
Openwin mywin= new Openwin();
}
}