背景颜色改变的程序

[复制链接]
查看11 | 回复5 | 2005-8-8 01:02:18 | 显示全部楼层 |阅读模式
我编了一个简单的背景颜色改变的程序,但在我的机子上运行不出来,我不知道为什么,请哪位高手帮我解决一下。
程序如下:
import java.awt.*;
public class BottonFrame extends Frame
{
public BottonFrame()
{
setTitle("BottonFrame&quot

;
setLayout(new FlowLayout());
add(new Button("Yellow&quot

);
add(new Button("Blue&quot

);
add(new Button("Red&quot

);
add(new Button("Orange&quot

);
add(new Button("Cyan&quot

);
add(new Button("Pink&quot

);
add(new Button("原背景颜色&quot

);
}
public boolean handleEvent(Event evt)
{
if(evt.id==Event.WINDOW_DESTROY)
System.exit(0);
return false;
}
public boolean action(Event evt,Object obj)
{
if(obj.equals("Yellow&quot

) setBackground(Color.yellow);
else if(obj.equals("Blue&quot

) setBackground(Color.blue);
else if(obj.equals("Red")) setBackground(Color.red);
else if(obj.equals("Orange")) setBackground(Color.orange);
else if(obj.equals("Cyan")) setBackground(Color.cyan);
else if(obj.equals("Pink")) setBackground(Color.pink);
else if(obj.equals("原背景颜色")) setBackground(Color.black);
else return super.action(evt,obj);
repaint();
return true;
}
public static void main(String arge[])
{
Frame f=new BottonFrame();
f.setBackground(Color.black);
f.resize(300,200);
f.show();
}
}
帮帮我,!!!
谢谢!!!!














回复

使用道具 举报

千问 | 2005-8-8 01:02:18 | 显示全部楼层
没有给Button加事件监听器
import java.awt.*;
import java.awt.event.*;

public class BottonFrame extends Frame {
public BottonFrame() {
setTitle("BottonFrame&quot

;
setLayout(new FlowLayout());
ButtonListener listener = new ButtonListener();
Button btn = new Button("Yellow&quot

;
btn.addActionListener(listener);
add(btn);
btn = new Button("Blue&quot

;
btn.addActionListener(listener);
add(btn);
btn = new Button("Red&quot

;
btn.addActionListener(listener);
add(btn);
btn = new Button("Orange&quot

;
btn.addActionListener(listener);
add(btn);
btn = new Button("Cyan&quot

;
btn.addActionListener(listener);
add(btn);
btn = new Button("原背景颜色&quot

;
btn.addActionListener(listener);
add(btn);
}
public boolean handleEvent(Event evt) {
if (evt.id == Event.WINDOW_DESTROY)

System.exit(0);
return true;
}
public boolean action(Event evt, Object obj) {
if (obj.equals("Yellow&quot

)

setBackground(Color.yellow);
else if (obj.equals("Blue&quot

)

setBackground(Color.blue);
else if (obj.equals("Red&quot

)

setBackground(Color.red);
else if (obj.equals("Orange"))

setBackground(Color.orange);
else if (obj.equals("Cyan"))

setBackground(Color.cyan);
else if (obj.equals("Pink"))

setBackground(Color.pink);
else if (obj.equals("原背景颜色"))

setBackground(Color.black);
else

return super.action(evt, obj);
repaint();
return true;
}
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {

String obj = e.getActionCommand();

if (obj.equals("Yellow"))

setBackground(Color.yellow);

else if (obj.equals("Blue"))

setBackground(Color.blue);

else if (obj.equals("Red"))

setBackground(Color.red);

else if (obj.equals("Orange"))

setBackground(Color.orange);

else if (obj.equals("Cyan"))

setBackground(Color.cyan);

else if (obj.equals("Pink"))

setBackground(Color.pink);

else if (obj.equals("原背景颜色"))

setBackground(Color.black);

repaint();
}
}

public static void main(String[] arge) {
Frame f = new BottonFrame();
f.setBackground(Color.black);
f.resize(300, 200);
f.show();
}
}
回复

使用道具 举报

千问 | 2005-8-8 01:02:18 | 显示全部楼层
谢谢你!
请问你学JAVA有多长时间了?
回复

使用道具 举报

千问 | 2005-8-8 01:02:18 | 显示全部楼层
看过你的程序后,我发现我编的action(Evevt evt,Object obj)方法是无用的,完整的程序应该是:
import java.awt.*;
import java.awt.event.*;

public class BottonFrame extends Frame {
public BottonFrame() {
setTitle("BottonFrame&quot

;
setLayout(new FlowLayout());
ButtonListener listener = new ButtonListener();
Button btn = new Button("Yellow&quot

;
btn.addActionListener(listener);
add(btn);
btn = new Button("Blue&quot

;
btn.addActionListener(listener);
add(btn);
btn = new Button("Red&quot

;
btn.addActionListener(listener);
add(btn);
btn = new Button("Orange&quot

;
btn.addActionListener(listener);
add(btn);
btn = new Button("Cyan&quot

;
btn.addActionListener(listener);
add(btn);
btn = new Button("原背景颜色&quot

;
btn.addActionListener(listener);
add(btn);
}
public boolean handleEvent(Event evt) {
if (evt.id == Event.WINDOW_DESTROY)
System.exit(0);
return true;
}
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String obj = e.getActionCommand();
if (obj.equals("Yellow&quot

)
setBackground(Color.yellow);
else if (obj.equals("Blue&quot

)
setBackground(Color.blue);
else if (obj.equals("Red&quot

)
setBackground(Color.red);
else if (obj.equals("Orange"))
setBackground(Color.orange);
else if (obj.equals("Cyan"))
setBackground(Color.cyan);
else if (obj.equals("Pink"))
setBackground(Color.pink);
else if (obj.equals("原背景颜色"))
setBackground(Color.black);
repaint();
}
}
public static void main(String[] arge) {
Frame f = new BottonFrame();
f.setBackground(Color.black);
f.resize(300, 200);
f.show();
}
}
回复

使用道具 举报

千问 | 2005-8-8 01:02:18 | 显示全部楼层
支持
回复

使用道具 举报

千问 | 2005-8-8 01:02:18 | 显示全部楼层
上面那个程序,还能最简单的方法实现么?
但程序的健壮性应该有
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行