当鼠标退出按钮时,MouseExited不会被触发

时间:2015-03-21 23:58:00

标签: java swing jbutton mouselistener

我在GridLayout中有一个JPanel,我添加了按钮。我想在输入鼠标时和鼠标退出按钮时对按钮实现某种效果。鼠标悬停在按钮上时会正确触发mouseEntered事件,但是,当鼠标离开按钮时,不会触发mouseExited事件。我该如何解决这个问题?

自定义JButton

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;


public class CustomButton extends JButton implements MouseListener{


    Dimension scaled;

    public CustomButton(String text){
        super(text);

        setContentAreaFilled(false);
        setBorderPainted(false);
        addMouseListener(this);

    }
    @Override
    public void invalidate() {
        super.invalidate();
        scaled = getSize();

    }

    @Override 
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        g.setColor(Color.BLUE);
        g.drawRoundRect(0, 0, getWidth()-1, getHeight()-1, 20, 20);
        g.fillRoundRect(0, 0, getWidth()-1, getHeight()-1, 20, 20);             

    }

    @Override
    public void mouseClicked(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub
        System.out.println("entered the view");

    }

    @Override
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub

        System.out.println("exited the view");
    }

    @Override
    public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub

    }


}

1 个答案:

答案 0 :(得分:2)

不要使用MouseListener,而是将ChangeListener添加到JButton的模型中并对其翻转状态的变化作出反应,...... isRollover()

import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class TestRollover extends JPanel {
   private static final int PREF_W = 500;
   private static final int PREF_H = PREF_W;
   JButton button = new JButton("Button");

   public TestRollover() {
      add(button);
      button.getModel().addChangeListener(new ChangeListener() {
         private boolean rollover = false;

         @Override
         public void stateChanged(ChangeEvent e) {
            ButtonModel model = (ButtonModel) e.getSource();
            if (model.isRollover() != rollover) {
               System.out.println("Rollover: " + model.isRollover());
               rollover = model.isRollover();               
            }
         }
      });

      button.addMouseListener(new MouseAdapter() {
         @Override
         public void mouseEntered(MouseEvent e) {
            System.out.println("mouse entered");
         }

         @Override
         public void mouseExited(MouseEvent e) {
            System.out.println("mouse exited");
         }
      });
   }

   @Override
   public Dimension getPreferredSize() {
      if (isPreferredSizeSet()) {
         return super.getPreferredSize();
      }
      return new Dimension(PREF_W, PREF_H);
   }

   private static void createAndShowGui() {
      TestRollover mainPanel = new TestRollover();

      JFrame frame = new JFrame("TestRollover");
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}