**************************************************************
新建一个类ChangeColor.java,代码如下:
**************************************************************
import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
public class ChangeColor extends JFrame implements MouseMotionListener {
public ChangeColor() {
this.setTitle("Change Color");
this.setBounds(300, 200, 400, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.getContentPane().setBackground(Color.GREEN);
this.addMouseMotionListener(this);
}
public void mouseMoved(MouseEvent e) {
if (e.getX() < >
this.getContentPane().setBackground(Color.RED);
} else {
this.getContentPane().setBackground(Color.BLUE);
}
}
public void mouseDragged(MouseEvent e) {
}
public static void main(String[] args) {
new ChangeColor();
}
}
**************************************************************
运行结果如下:
**************************************************************