Java 按下鼠标左键时鼠标悬停事件
Java mouse hover event while left mouse key is pressed
在一个面板中,我有 10*10 个切换按钮。我的目标是做这个活动:
如果鼠标左键按下并且鼠标位于特定按钮调用 "click on the button" 行上。因此用户可以轻松 select(点击)多个按钮。
这不起作用(移动鼠标时更改同一个按钮而不是更改另一个按钮):
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JToggleButton;
public class ButtonClickOnHover {
public void clickOnButtonOnHover(JToggleButton button) {
button.addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseMoved(MouseEvent e) {
}
@Override
public void mouseDragged(MouseEvent e) {
button.doClick(0);
}
});
}
}
试过了,没用(他的实现有什么问题?):
import java.awt.Color;
import java.awt.Component;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.SwingUtilities;
public class ButtonClickOnHover {
public void clickOnButtonOnHover(JPanel panel) {
panel.addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseMoved(MouseEvent e) {
}
@Override
public void mouseDragged(MouseEvent e) {
Point location = MouseInfo.getPointerInfo().getLocation();
JPanel parent = (JPanel) e.getComponent();
SwingUtilities.convertPointFromScreen(location, parent);
Component mouseOver = parent.findComponentAt( location );
if (mouseOver instanceof JToggleButton)
{
JToggleButton button = (JToggleButton)mouseOver;
button.setBackground( Color.YELLOW );
}
}
});
}
}
mouseDragged 事件的源始终是您第一次生成 mousePressed 事件的组件,即使您将鼠标移到另一个组件上也是如此。
如果您想知道鼠标当前位于哪个组件上,那么您需要做一些额外的工作。
类似于:
Point location = MouseInfo.getPointerInfo().getLocation();
Component button = e.getComponent();
JPanel parent = (JPanel)button.getParent();
SwingUtilities.convertPointFromScreen(location, parent);
Component mouseOver = parent.findComponentAt( location );
if (mouseOver instanceof JToggleButton)
{
JToggleButton button = (JToggleButton)mouseOver;
button.setBackground( Color.YELLOW );
}
以上代码尝试转换鼠标在屏幕上的位置以找到它当前所在的组件。
在一个面板中,我有 10*10 个切换按钮。我的目标是做这个活动: 如果鼠标左键按下并且鼠标位于特定按钮调用 "click on the button" 行上。因此用户可以轻松 select(点击)多个按钮。
这不起作用(移动鼠标时更改同一个按钮而不是更改另一个按钮):
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JToggleButton;
public class ButtonClickOnHover {
public void clickOnButtonOnHover(JToggleButton button) {
button.addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseMoved(MouseEvent e) {
}
@Override
public void mouseDragged(MouseEvent e) {
button.doClick(0);
}
});
}
}
试过了,没用(他的实现有什么问题?):
import java.awt.Color;
import java.awt.Component;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.SwingUtilities;
public class ButtonClickOnHover {
public void clickOnButtonOnHover(JPanel panel) {
panel.addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseMoved(MouseEvent e) {
}
@Override
public void mouseDragged(MouseEvent e) {
Point location = MouseInfo.getPointerInfo().getLocation();
JPanel parent = (JPanel) e.getComponent();
SwingUtilities.convertPointFromScreen(location, parent);
Component mouseOver = parent.findComponentAt( location );
if (mouseOver instanceof JToggleButton)
{
JToggleButton button = (JToggleButton)mouseOver;
button.setBackground( Color.YELLOW );
}
}
});
}
}
mouseDragged 事件的源始终是您第一次生成 mousePressed 事件的组件,即使您将鼠标移到另一个组件上也是如此。
如果您想知道鼠标当前位于哪个组件上,那么您需要做一些额外的工作。
类似于:
Point location = MouseInfo.getPointerInfo().getLocation();
Component button = e.getComponent();
JPanel parent = (JPanel)button.getParent();
SwingUtilities.convertPointFromScreen(location, parent);
Component mouseOver = parent.findComponentAt( location );
if (mouseOver instanceof JToggleButton)
{
JToggleButton button = (JToggleButton)mouseOver;
button.setBackground( Color.YELLOW );
}
以上代码尝试转换鼠标在屏幕上的位置以找到它当前所在的组件。