java 如何在全屏独占模式下摆脱鼠标光标?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/191592/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 11:21:28  来源:igfitidea点击:

How do I get rid of the mouse cursor in full-screen exclusive mode?

javamousefullscreenmouse-cursor

提问by Adrian

I'm working on a simple 2D game engine in Java, and having no trouble with FSEM, buffer strategies, and so on; my issue is with the mouse cursor. In windowed mode, I can hide the mouse cursor, no problem, by using setCursor() from my JFrame to set a wholly-transparent cursor. However, after a call to device.setFullScreenWindow(this) to go into FSEM, the mouse cursor comes back, and subsequent calls to setCursor() to set it back to my blank cursor have no effect. Calling device.setFullScreenWindow(null) allows me to get rid of the cursor again - it's only while I'm in FSEM that I can't get rid of it.

我正在用 Java 开发一个简单的 2D 游戏引擎,并且在 FSEM、缓冲策略等方面没有问题;我的问题是鼠标光标。在窗口模式下,我可以隐藏鼠标光标,没问题,通过使用我的 JFrame 中的 setCursor() 来设置一个完全透明的光标。但是,在调用 device.setFullScreenWindow(this) 进入 FSEM 后,鼠标光标又回来了,随后调用 setCursor() 将其设置回我的空白光标没有任何效果。调用 device.setFullScreenWindow(null) 允许我再次摆脱光标 - 只有当我在 FSEM 中时我才能摆脱它。

I'm working under JDK 6, target platform is JDK 5+.

我在 JDK 6 下工作,目标平台是 JDK 5+。

UPDATE:I've done some more testing, and it looks like this issue occurs under MacOS X 10.5 w/Java 6u7, but not under Windows XP SP3 with Java 6u7. So, it could possibly be a bug in the Mac version of the JVM.

更新:我做了一些更多的测试,看起来这个问题在 MacOS X 10.5 w/Java 6u7 下出现,但在 Windows XP SP3 with Java 6u7 下没有。因此,它可能是 JVM 的 Mac 版本中的一个错误。

采纳答案by Adrian

I think I've finally found the solution:

我想我终于找到了解决方案:

System.setProperty("apple.awt.fullscreenhidecursor","true");

This is an Apple-proprietary system property that hides the mouse cursor when an application is in full-screen mode. It's the only way I've found to fix it.

这是 Apple 专有的系统属性,可在应用程序处于全屏模式时隐藏鼠标光标。这是我找到的唯一解决方法。

回答by Janthoe

Try Creating a custom invisible cursor:

尝试创建一个自定义的隐形光标:

    Toolkit toolkit = Toolkit.getDefaultToolkit();
    Point hotSpot = new Point(0,0);
    BufferedImage cursorImage = new BufferedImage(1, 1, BufferedImage.TRANSLUCENT); 
    Cursor invisibleCursor = toolkit.createCustomCursor(cursorImage, hotSpot, "InvisibleCursor");        
    setCursor(invisibleCursor);

回答by davenpcj

One developer found a way around it by creating a one pixel cursor out of a transparent GIF.

一位开发人员通过从透明 GIF 中创建一个像素光标找到了解决方法。

http://sevensoft.livejournal.com/23460.html

http://sevensoft.livejournal.com/23460.html

I know you tried that, but his is specifically addressing the issue of full-screen mode, exactly as you say, so perhaps there's something he's done that you haven't.

我知道你尝试过,但正如你所说,他专门解决了全屏模式的问题,所以也许他做了一些你没有做的事情。

回答by Ricket

Here's what has been working for me:

以下是对我有用的内容:

Toolkit toolkit = Toolkit.getDefaultToolkit();

// get the smallest valid cursor size
Dimension dim = toolkit.getBestCursorSize(1, 1);

// create a new image of that size with an alpha channel
BufferedImage cursorImg = new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_INT_ARGB);

// get a Graphics2D object to draw to the image
Graphics2D g2d = cursorImg.createGraphics();

// set the background 'color' with 0 alpha and clear the image
g2d.setBackground(new Color(0.0f, 0.0f, 0.0f, 0.0f));
g2d.clearRect(0, 0, dim.width, dim.height);

// dispose the Graphics2D object
g2d.dispose();

// now create your cursor using that transparent image
hiddenCursor = toolkit.createCustomCursor(cursorImg, new Point(0,0), "hiddenCursor");

Granted, I haven't tested it on Mac (yet), only Windows. But when I used the common methods I was getting the cursor as black box, so I use the code above the create a transparent box and set it as the cursor instead. Of course you have to use the setCursor method on an AWT object (such as your app's Frame) to set this hiddenCursor. Here is my hideMouse method ('fr' is my Frame):

当然,我还没有在 Mac(还)上测试过它,只有 Windows。但是当我使用常用​​方法时,我将光标设为黑框,所以我使用上面的代码创建一个透明框并将其设置为光标。当然,您必须在 AWT 对象(例如您的应用程序的框架)上使用 setCursor 方法来设置此 hiddenCursor。这是我的 hideMouse 方法('fr' 是我的框架):

public void hideMouse(boolean hide) {
    if(hide) {
        fr.setCursor(hiddenCursor);
    } else {
        fr.setCursor(Cursor.getDefaultCursor());
    }
}

回答by seisyll

If you're running only on Windows, it looks like you'll need to call ShowCursor(FALSE) through JNI. At least, to make the cursor hide complete.

如果您仅在 Windows 上运行,则您似乎需要通过 JNI 调用 ShowCursor(FALSE)。至少,使光标隐藏完成。

Here's some code which creates the 1x1 cursor. It works for me, though I still get a 1x1 cursor.

这是一些创建 1x1 游标的代码。它对我有用,但我仍然得到一个 1x1 光标。

 Toolkit toolkit = Toolkit.getDefaultToolkit();
 Dimension dim = toolkit.getBestCursorSize(1,1);
 transCursor = toolkit.createCustomCursor(gc.createCompatibleImage(dim.width, dim.height),
     new Point(0, 0), "transCursor");
 ((Component)mainFrame).setCursor(transCursor);

回答by slf

Specifically for your Mac problem, through JNI you could use the following:

专门针对您的 Mac 问题,通过 JNI,您可以使用以下内容:

Quartz Display Services Reference - CGDisplayHideCursor

Quartz 显示服务参考 - CGDisplayHideCursor

回答by Antonio Louro

I don't know if this knowledge applies but in a old VB6 app I had the same problem and I got rid of it moving the cursor out of the screen giving it some very large values.
Hope it helps.

我不知道这些知识是否适用,但在旧的 VB6 应用程序中,我遇到了同样的问题,我摆脱了将光标移出屏幕的方法,并为其提供了一些非常大的值。
希望能帮助到你。