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.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();
}
}
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();
}
}
----------------解决方案--------------------------------------------------------
动作事件 选项事件 鼠标移动事件 鼠标事件 键盘事件 组件焦点事件 调整事件 组件改变事件 窗口事件 都记住就好了
----------------解决方案--------------------------------------------------------