| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 697 人关注过本帖
标题:点一个格周围的格子颜色也要变
只看楼主 加入收藏
wuzhong
Rank: 1
等 级:新手上路
帖 子:233
专家分:0
注 册:2006-10-23
收藏
 问题点数:0 回复次数:5 
点一个格周围的格子颜色也要变

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

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

}
}

搜索更多相关主题的帖子: 格子 颜色 
2007-01-17 14:52
开心一科
Rank: 1
等 级:新手上路
帖 子:56
专家分:0
注 册:2006-10-5
收藏
得分:0 

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

}
}


2007-01-17 15:21
wuzhong
Rank: 1
等 级:新手上路
帖 子:233
专家分:0
注 册:2006-10-23
收藏
得分:0 
女士,哪儿错了.这是别人问我的,我没找出来.

2007-01-17 15:25
开心一科
Rank: 1
等 级:新手上路
帖 子:56
专家分:0
注 册:2006-10-5
收藏
得分:0 

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

}
}


2007-01-17 15:32
wuzhong
Rank: 1
等 级:新手上路
帖 子:233
专家分:0
注 册:2006-10-23
收藏
得分:0 
第一个错误知道了,可这第二个还是没发现,你标记的红色和我以前的不是一样的吗?呵呵.

2007-01-17 15:51
开心一科
Rank: 1
等 级:新手上路
帖 子:56
专家分:0
注 册:2006-10-5
收藏
得分:0 
就是由于 winbutton[i]=new JButton(""+i+"");这里原先有错误,
所以 String y=e.getActionCommand();//返回与此动作相关的命令字符串。
int x=Integer.parseInt(y);
抛出空指针异常拉!
现在可以运行变色了!


2007-01-17 18:15
快速回复:点一个格周围的格子颜色也要变
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.024386 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved