我需要能够跟踪鼠标下的组件。在遗留项目中,我正在使用mouseEntered()和mouseExited()触发器使用MouseListener完成此操作。
然而,这不包括两个角落案例
这意味着即使在与其相关的组件消失之后,与用户当前具有鼠标指针的内容相关联的应用程序中的其他位置的按钮仍保持显示。通常,这将通过mouseExited()触发器清理。
到目前为止,我最好的尝试是将ComponentListener和HierarchyListeners添加到我想要跟踪的组件中,并使用它们来触发使用button.getMousePosition() != null
检查鼠标是否在它们上面。另外,我然后使用它来分派合成的MOUSE_ENTER和MOUSE_EXIT事件,这样就不会改变现有的遗留代码。
我正在寻找一个更简单的解决方案,如果存在的话。
public class MouseTracker {
private static int clock;
public static void main(String[] args) {
JFrame frame = new JFrame();
Container content = frame.getContentPane();
JButton button1 = new JButton("one");
button1.setName("one");
Timer timer = new Timer(2000, (e) -> {
content.removeAll();
if (clock % 2 == 0) {
content.add(button1);
}
frame.revalidate();
frame.repaint();
clock++;
});
timer.setRepeats(true);
timer.start();
frame.setSize(200, 100);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
track(button1);
}
private static void track(JButton component) {
component.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
System.out.println("Mouse enter ");
}
@Override
public void mouseExited(MouseEvent e) {
System.out.println("Mouse exit ");
}
});
component.addComponentListener(new ComponentAdapter() {
@Override
public void componentShown(ComponentEvent e) {
checkMouse(component);
}
@Override
public void componentHidden(ComponentEvent e) {
checkMouse(component);
}
@Override
public void componentResized(ComponentEvent e) {
checkMouse(component);
}
@Override
public void componentMoved(ComponentEvent e) {
checkMouse(component);
}
});
component.addHierarchyListener(new HierarchyListener() {
@Override
public void hierarchyChanged(HierarchyEvent e) {
checkMouse(component);
}
});
}
private static void checkMouse(JComponent component) {
Point mousePosition = component.getMousePosition();
if (component.isShowing() && mousePosition != null) {
System.out.println("Equivalent mouse enter " + component.getName());
component.dispatchEvent(new MouseEvent(component, MouseEvent.MOUSE_ENTERED, 0, 0, mousePosition.x,
mousePosition.y, 0, false));
} else {
System.out.println("Equivalent mouse exit " + component.getName());
component.dispatchEvent(new MouseEvent(component, MouseEvent.MOUSE_EXITED, 0, 0, 0, 0, 0, false));
}
}
}
答案 0 :(得分:0)
缺乏其他建议,坚持目前提出的解决方案。
private static void track(JButton component) {
component.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
System.out.println("Mouse enter ");
}
@Override
public void mouseExited(MouseEvent e) {
System.out.println("Mouse exit ");
}
});
component.addComponentListener(new ComponentAdapter() {
@Override
public void componentShown(ComponentEvent e) {
checkMouse(component);
}
@Override
public void componentHidden(ComponentEvent e) {
checkMouse(component);
}
@Override
public void componentResized(ComponentEvent e) {
checkMouse(component);
}
@Override
public void componentMoved(ComponentEvent e) {
checkMouse(component);
}
});
component.addHierarchyListener(new HierarchyListener() {
@Override
public void hierarchyChanged(HierarchyEvent e) {
checkMouse(component);
}
});
}
private static void checkMouse(JComponent component) {
Point mousePosition = component.getMousePosition();
if (component.isShowing() && mousePosition != null) {
System.out.println("Equivalent mouse enter " + component.getName());
component.dispatchEvent(new MouseEvent(component, MouseEvent.MOUSE_ENTERED, 0, 0, mousePosition.x,
mousePosition.y, 0, false));
} else {
System.out.println("Equivalent mouse exit " + component.getName());
component.dispatchEvent(new MouseEvent(component, MouseEvent.MOUSE_EXITED, 0, 0, 0, 0, 0, false));
}
}