import java.awt.*;
import java.awt.event.*;
import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class Changecolor extends MouseAdapter {
Frame f = new Frame();
Button b1 = new Button("红色");
Button b2 = new Button("蓝色");
Button b3 = new Button("退出");
Button b4 = new Button("黄色");
public static void main(String args[]) {
Changecolor ct = new Changecolor();
ct.init();
}
public void init() {
Frame f = new Frame();
f.setLayout(new FlowLayout()));
Button b1 = new Button("红色");
Button b2 = new Button("蓝色");
Button b3 = new Button("退出");
Button b4 = new Button("黄色");
f.add(b1);
f.add(b2);
f.add(b3);
f.add(b4);
b1.addMouseListener(this);
b2.addMouseListener(this);
b3.addMouseListener(this);
b4.addMouseListener(this);
f.setSize(600, 600);
f.setVisible(true);
}
public void mousePressed(MouseEvent e2) {
if (e2.getSource() == b4) {
f.setBackground(Color.red);
}
if (e2.getSource() == b2) {
f.setBackground(Color.blue);
}
if (e2.getSource() == b1) {
f.setBackground(Color.yellow);
}
if (e2.getSource() == b3) {
System.exit(0);
}
}
}
就是这段代码,b4不管用,请问是怎么回事.
急,我在线等