当前位置: 代码迷 >> Java相关 >> 点一个格周围的格子颜色也要变
  详细解决方案

点一个格周围的格子颜色也要变

热度:91   发布时间:2007-01-17 14:52:07.0
点一个格周围的格子颜色也要变

谁帮忙给看一下这个程序为什么答不到我要求的呢,这是个开窗户的游戏 就是点一个格周围的格子颜色也变 为什么我点完之后 就这个格子颜色变 而且鼠标离开后就又变回去了呢?

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();

}
}

搜索更多相关的解决方案: 格子  颜色  

----------------解决方案--------------------------------------------------------

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()
{
f=new JFrame("游戏");
f.setBackground(Color.white);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setSize(500,500);
f.setVisible(true);
panel1=new JPanel();
panel2=new JPanel();
f.getContentPane().add(panel1,BorderLayout.CENTER);
f.getContentPane().add(panel2,BorderLayout.SOUTH);
panel1.setLayout(new GridLayout(5,5));
winbutton=new JButton[25];
panel1.setPreferredSize(new Dimension(300,300));
for(int i=0;i<=24;i++)
{
winbutton[i]=new JButton(""+i+"");
winbutton[i].setBackground(Color.red);
panel1.add(winbutton[i]);
winbutton[i].addActionListener(this);
}
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[])
{
new Openwin();

}
}


----------------解决方案--------------------------------------------------------
女士,哪儿错了.这是别人问我的,我没找出来.
----------------解决方案--------------------------------------------------------

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()
{
f=new JFrame("游戏");
f.setBackground(Color.white);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setSize(500,500);
f.setVisible(true);
panel1=new JPanel();
panel2=new JPanel();
f.getContentPane().add(panel1,BorderLayout.CENTER);
f.getContentPane().add(panel2,BorderLayout.SOUTH);
panel1.setLayout(new GridLayout(5,5));
winbutton=new JButton[25];
panel1.setPreferredSize(new Dimension(300,300));
for(int i=0;i<=24;i++)
{
winbutton[i]=new JButton(""+i+"");
winbutton[i].setBackground(Color.red);
panel1.add(winbutton[i]);
winbutton[i].addActionListener(this);
}
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[])
{
new Openwin();

}
}


----------------解决方案--------------------------------------------------------
第一个错误知道了,可这第二个还是没发现,你标记的红色和我以前的不是一样的吗?呵呵.

----------------解决方案--------------------------------------------------------
就是由于 winbutton[i]=new JButton(""+i+"");这里原先有错误,
所以 String y=e.getActionCommand();//返回与此动作相关的命令字符串。
int x=Integer.parseInt(y);
抛出空指针异常拉!
现在可以运行变色了!


----------------解决方案--------------------------------------------------------
  相关解决方案