鼠标退出按钮时未触发 MouseExited
Posted
技术标签:
【中文标题】鼠标退出按钮时未触发 MouseExited【英文标题】:MouseExited not fired when mouse exits button 【发布时间】:2015-05-25 05:08:16 【问题描述】:我在向其中添加按钮的 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
【问题讨论】:
“某种效果”什么效果? 我认为问题是您需要使用MouseMotionListener
而不是MouseListener
。
【参考方案1】:
不要使用 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();
);
【讨论】:
但是为什么没有触发 mouseExited 事件? @user2864740:对我来说效果很好(见上面的代码)。如果您仍然遇到问题,那么您需要创建并发布一个minimal example program。 @user2864740:您的问题的问题在于您没有向我们展示为什么您的代码不起作用。如果您可以创建并发布mcve,那么我们可以告诉您。否则没有人会知道。 当您在按钮上移动鼠标时,此代码会打印出“Rollover:true”后跟“Rollover:false”以上是关于鼠标退出按钮时未触发 MouseExited的主要内容,如果未能解决你的问题,请参考以下文章