这几天在看<core java>这个书 可是看到awt的时候有个书上的程序,程序要求是,在一个窗体中有几个按钮,然后单击一个改变背景颜色,我遇到的问题是单击完全没有反应请各位高手赐教 小弟不胜感激代码如下:import java.awt.*;public class changeColor extends Frame{
public boolean handleEvent(Event e){
if(e.id==Event.WINDOW_DESTROY)
System.exit(0);
return false;
}
public changeColor(){
setTitle("changeColor");
setLayout(new FlowLayout());
add(new Button("red"));
add(new Button("yellow"));
add(new Button("blue"));
add(new Button("orange"));
add(new Button("pink"));
}
public boolean action(Event e,Object arg){
if(arg.equals("yellow")) setBackground(Color.yellow);
else if(arg.equals("blue")) setBackground(Color.blue);
else if(arg.equals("orange")) setBackground(Color.orange);
else if(arg.equals("pink")) setBackground(Color.pink);
else if(arg.equals("red")) setBackground(Color.red);
else return super.action(e, arg);
repaint();
return true;
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
changeColor cb=new changeColor();
cb.resize(500, 400);
cb.show();
}}
public boolean handleEvent(Event e){
if(e.id==Event.WINDOW_DESTROY)
System.exit(0);
return false;
}
public changeColor(){
setTitle("changeColor");
setLayout(new FlowLayout());
add(new Button("red"));
add(new Button("yellow"));
add(new Button("blue"));
add(new Button("orange"));
add(new Button("pink"));
}
public boolean action(Event e,Object arg){
if(arg.equals("yellow")) setBackground(Color.yellow);
else if(arg.equals("blue")) setBackground(Color.blue);
else if(arg.equals("orange")) setBackground(Color.orange);
else if(arg.equals("pink")) setBackground(Color.pink);
else if(arg.equals("red")) setBackground(Color.red);
else return super.action(e, arg);
repaint();
return true;
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
changeColor cb=new changeColor();
cb.resize(500, 400);
cb.show();
}}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货