在Mac OS X中最大化或完全筛选窗口后,Java停止捕获鼠标移动事件 [英] Java stops capturing mouse movement events after maximizing or full screening a window in Mac OS X

查看:120
本文介绍了在Mac OS X中最大化或完全筛选窗口后,Java停止捕获鼠标移动事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我通过按下OS X上的最大化按钮或全屏按钮放大窗口时,不再捕获鼠标移动事件(虽然拖动)。

When I enlarge a window by either pressing the maximize button or the full screen button on OS X, mouse movement events are no longer captured (though dragging is).

我在下面添加了一个演示程序,突出了该问题。可以使用 MouseEventDemo Web启动示例在 Java Tutorials网站上。

I have included a demo program below that highlights the issue. The maximization problem can be replicated using the MouseEventDemo web start example on the Java Tutorials website.

经过一些故障排除后,我注意到如果鼠标离开窗口(例如,移动到窗口顶部以访问菜单栏),则会重新捕获鼠标移动,然后返回。看起来问题可能与调整动画大小期间鼠标位置和窗口之间的关系有关,因为鼠标在调整大小之前不在帧中,但是后来即使它不一定在过程中移动也是如此。

After some troubleshooting, I noted that mouse movements are recaptured if the mouse leaves the window (e.g., moves to the top of the window to access the menu bar) and then returns. It seems the issue may have something to do with the relation between the mouse position and the window during resizing animations, since the mouse is not in the frame before the resizing, but is afterward even though it didn’t necessarily move in the process.

import java.awt.Window;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.lang.reflect.Method;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Main implements MouseMotionListener {

    JLabel label = new JLabel("label");

    public static void main(String[] args) {
        Main main = new Main();
        main.init();
    }

    public void init() {
        JFrame frame = new JFrame();
        frame.setSize(640, 480);
        frame.setLocationRelativeTo(null);
        frame.getContentPane().add(label);
        frame.addMouseMotionListener(this);
        frame.setVisible(true);

        if (isMacOSX()) {
            enableFullScreenMode(frame);
        }
    }

    public void mouseDragged(MouseEvent e) {
        label.setText(e.toString());
    }

    public void mouseMoved(MouseEvent e) {
        label.setText(e.toString());
    }

    private static boolean isMacOSX() {
        return System.getProperty("os.name").indexOf("Mac OS X") >= 0;
    }

    public static void enableFullScreenMode(Window window) {
        try {
            Class<?> clazz = Class.forName("com.apple.eawt.FullScreenUtilities");
            Method method = clazz.getMethod("setWindowCanFullScreen", new Class<?>[] { Window.class, boolean.class });
            method.invoke(null, window, true);
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }
}

运行上面的代码应该显示当标签执行和不更新时。

Running the code above should reveal when the label does and does not update.

我正在使用Java SE 7 [1.7.0_45]运行OS X版本10.9 Build 13A3017。

I am running OS X Version 10.9 Build 13A3017 with Java SE 7 [1.7.0_45].

推荐答案

Oracle回复了错误报告我提交了,并在评论中指出应该在Java 8中解决该问题。上面的代码在1.8.0 JRE(build 1.8.0-b132)上使用OS X 10.9.2按预期工作。

Oracle responded to the bug report I submitted, noting in their comments that the issue should be resolved in Java 8. The code above works as expected on the 1.8.0 JRE (build 1.8.0-b132) using OS X 10.9.2.

这篇关于在Mac OS X中最大化或完全筛选窗口后,Java停止捕获鼠标移动事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆