import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
class Yanse1
{
JFrame f=new JFrame("两种颜色");
JPanel p1=new JPanel();
JPanel p2=new JPanel();
GridLayout g=new GridLayout(1,2);
Yanse1()
{
p1.setBackground(Color.BLACK);
p2.setBackground(Color.white);
f.add(p1);
f.add(p2);
f.setLayout(g);
Yanse yan=new Yanse();
Yanse2 yan2=new Yanse2();
p1.addMouseListener(yan);
p2.addMouseListener(yan2);
f.setSize(300,300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension d=Toolkit.getDefaultToolkit().getScreenSize();
f.setLocation((d.width-200)/2,(d.height-120)/2);
f.setVisible(true);
}
public class Yanse implements MouseListener
{
public void mousePressed(MouseEvent e)
{ }
public void mouseReleased(MouseEvent e)
{ }
public void mouseEntered(MouseEvent e)
{
p1.setBackground(Color.pink);
}
public void mouseExited(MouseEvent e)
{
p1.setBackground(Color.blue);
}
public void mouseClicked(MouseEvent e)
{ }
}
public class Yanse2 implements MouseListener
{
public void mousePressed(MouseEvent e)
{ }
public void mouseReleased(MouseEvent e)
{ }
public void mouseEntered(MouseEvent e)
{
p2.setBackground(Color.yellow);
}
public void mouseExited(MouseEvent e)
{
p2.setBackground(Color.red);
}
public void mouseClicked(MouseEvent e)
{ }
}
public static void main(String args[])
{
Yanse1 y=new Yanse1();
}
} |
|