java 鼠标感应问题
要求写一个程式, 当鼠标在左边程式窗口时背景色变成红色, 当鼠标指在右边程式窗口时背景色变成绿色。希望高手可以写出来给我这个新手看看, 我刚接触java一个礼拜, 基本什么也不懂, 谢谢大家帮忙
import java.awt.Color; import java.awt.Container; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Toolkit; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.JFrame; import javax.swing.JLabel; public class MouseEventTest extends JFrame{ private Container container; private JLabel label1,label2; public MouseEventTest(){ super("MouseEventTest"); container = getContentPane(); label1 = new JLabel("绿色"); label1.addMouseListener( new MouseAdapter(){ public void mouseEntered(MouseEvent event){ container.setBackground(Color.GREEN); } public void mouseExited(MouseEvent event){ container.setBackground(Color.WHITE); } } ); label2 = new JLabel("红色"); label2.addMouseListener( new MouseAdapter(){ public void mouseEntered(MouseEvent event){ container.setBackground(Color.RED); } public void mouseExited(MouseEvent event){ container.setBackground(Color.WHITE); } } ); container.setLayout(new FlowLayout()); container.add(label1); container.add(label2); setSize(400,400); setResizable(false); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Dimension frameSize = getSize(); setLocation((screenSize.width - frameSize.width)/2,(screenSize.height - frameSize.height)/2); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { new MouseEventTest(); } }
import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.Dimension; import java.awt.Toolkit; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.JFrame; import javax.swing.JLabel; public class MouseEventTest extends JFrame{ private Container container; private JLabel label1,label2; public MouseEventTest(){ super("MouseEventTest"); container = getContentPane(); label1 = new JLabel("绿色",javax.swing.SwingConstants.CENTER); label1.addMouseListener( new MouseAdapter(){ public void mouseEntered(MouseEvent event){ container.setBackground(Color.GREEN); } public void mouseExited(MouseEvent event){ container.setBackground(Color.WHITE); } } ); label2 = new JLabel("红色",javax.swing.SwingConstants.CENTER); label2.addMouseListener( new MouseAdapter(){ public void mouseEntered(MouseEvent event){ container.setBackground(Color.RED); } public void mouseExited(MouseEvent event){ container.setBackground(Color.WHITE); } } ); container.add(label1,BorderLayout.WEST); container.add(label2,BorderLayout.EAST); setSize(400,400); setResizable(false); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Dimension frameSize = getSize(); setLocation((screenSize.width - frameSize.width)/2,(screenSize.height - frameSize.height)/2); label1.setPreferredSize(new Dimension(200,400)); label2.setPreferredSize(new Dimension(200,400)); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { new MouseEventTest(); } }