通过鼠标移动到按钮上更改背景色案例

通过鼠标移动到按钮上更改背景色案例

import java.awt.Button;

import java.awt.Color;

import java.awt.FlowLayout;

import java.awt.Frame;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

 

public classFrameDemo {

   public static void main(String[] args) {

      // 创建窗体对象

      final Frame f = new Frame("更改背景色");

      // 设置窗体属性和布局

      f.setBounds(400,200, 400, 300);

      f.setLayout(new FlowLayout());

 

      // 创建四个按钮

      ButtonredButton = newButton("红色");

      ButtongreenButton = newButton("绿色");

      ButtonbuleButton = newButton("蓝色");

 

      // 添加按钮

      f.add(redButton);

      f.add(greenButton);

      f.add(buleButton);

 

      // 设置窗体关闭

      f.addWindowListener(new WindowAdapter() {

         @Override

         public voidwindowClosing(WindowEvent e) {

            System.exit(0);

         }

      });

 

      // 对按钮添加动作事件

      // redButton.addActionListener(new ActionListener() {

      // @Override

      // public void actionPerformed(ActionEvent e) {

      // f.setBackground(Color.RED);

      // }

      // });

 

      // 对按钮添加鼠标点击事件

      // redButton.addMouseListener(new MouseAdapter() {

      // @Override

      // public void mouseClicked(MouseEvent e) {

      // f.setBackground(Color.RED);

      // }

      // });

 

      // 对按钮添加鼠标的进入事件

      redButton.addMouseListener(new MouseAdapter() {

         @Override

         public void mouseEntered(MouseEvent e) {

            f.setBackground(Color.RED);

         }

      });

 

      redButton.addMouseListener(new MouseAdapter() {

         @Override

         public void mouseExited(MouseEvent e) {

            f.setBackground(Color.WHITE);

         }

      });

 

      greenButton.addMouseListener(new MouseAdapter() {

         @Override

         public void mouseEntered(MouseEvente) {

            f.setBackground(Color.GREEN);

         }

      });

 

      greenButton.addMouseListener(new MouseAdapter() {

         @Override

         public void mouseExited(MouseEvente) {

            f.setBackground(Color.WHITE);

         }

      });

 

      buleButton.addMouseListener(new MouseAdapter() {

         @Override

         public void mouseEntered(MouseEvente) {

            f.setBackground(Color.BLUE);

         }

      });

 

      buleButton.addMouseListener(new MouseAdapter() {

         @Override

         public void mouseExited(MouseEvente) {

            f.setBackground(Color.WHITE);

         }

      });

 

      // 设置窗体显示

      f.setVisible(true);

   }

}

 

 

你可能感兴趣的:(鼠标)