这是Java中简单的java.awt包的使用,并且使用了监听接口,只是作为一个程序思想的展示,没有什么高深之处。 import java.awt.*;import java.awt.event.*; class ChangeColor extends WindowAdapter implements WindowListener,ActionListener{Frame f;Button r,b,y; public static void main(String args[]) {ChangeColor c=new ChangeColor(); c.execute(); } public void execute() {f=new Frame("改变颜色"); f.setLayout(new FlowLayout()); f.addWindowListener(this); r=new Button("红色"); b=new Button("蓝色"); y=new Button("黄色"); f.add(r);f.add(b);f.add(y); r.addActionListener(this); b.addActionListener(this); y.addActionListener(this); f.setSize(400,300);f.setVisible(true); } public void windowClosing(WindowEvent e) {System.exit(0); } public void actionPerformed(ActionEvent e) {if(e.getSource()==r)f.setBackground(Color.red); else if(e.getSource()==b)f.setBackground(Color.blue); else if(e.getSource()==y)f.setBackground(Color.yellow); }}

评论