谁帮忙给看一下这个程序为什么答不到我要求的呢,这是个开窗户的游戏 就是点一个格周围的格子颜色也变 为什么我点完之后 就这个格子颜色变 而且鼠标离开后就又变回去了呢?import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public 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.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 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 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 static void main(String args[])
{
new Openwin();
}
}
import java.awt.event.*;
import javax.swing.*;
public 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.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 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 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 static void main(String args[])
{
new Openwin();
}
}
解决方案 »
- 一个关于多线程的问题
- 关于重新封装XML问题
- java如何实现填充算法,谢谢啦~
- io 相对路径
- JTable中加入JCheckBox需要重写AbstractTableModel中的getColumnClass()
- 求测试方法
- 请问大虾们什么是静态方法啊
- 新手问题,一下午了。。。。
- 请各位怎样在JTextField中屏蔽掉英文字母,而只输入数字?
- driver = (Driver)Class.forName(driverName).newInstance()和Class.forName(driverName);有什么不同呀?
- 有关递归的一个费解的问题
- jar中getResource加载gif总是返回null?
我也等着答案呢