如何在 Swing 应用程序中隐藏光标? [英] How to hide cursor in a Swing application?

查看:39
本文介绍了如何在 Swing 应用程序中隐藏光标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法隐藏光标(除了使用透明图像作为光标)?

Is there a way to hide cursor (other than using transparent image as a cursor)?

推荐答案

看来 Cursor 类开始时没有空白"光标,因此可以使用 Toolkit.createCustomCursor 方法.

It appears that the Cursor class does not have a "blank" cursor to begin with, so one could define a new "blank" cursor using the Toolkit.createCustomCursor method.

这是我尝试过的一种似乎有效的方法:

Here's one way I've tried which seems to work:

// Transparent 16 x 16 pixel cursor image.
BufferedImage cursorImg = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);

// Create a new blank cursor.
Cursor blankCursor = Toolkit.getDefaultToolkit().createCustomCursor(
    cursorImg, new Point(0, 0), "blank cursor");

// Set the blank cursor to the JFrame.
mainJFrame.getContentPane().setCursor(blankCursor);

<小时>

编辑

关于 JFrame 中所有内容的注释都没有光标,似乎 JFrame 中包含的 Component最终会继承容器的光标(JFrame),所以如果要求某个Component有光标出现,就必须手动设置所需的光标.

Regarding the comment about everything inside the JFrame ending up without a cursor, it seems that the Components which are contained in the JFrame will end up inheriting the cursor of the container (the JFrame), so if it is a requirement to have a certain Component have the cursor appear, one would have to manually set the desired cursor.

例如,如果JFrame中包含一个JPanel,则可以将该JPanel的光标设置为系统默认值使用 Cursor.getDefaultCursor 方法:

For example, if there is a JPanel contained in the JFrame, then one could set the cursor of that JPanel to the system's default using the Cursor.getDefaultCursor method:

JPanel p = ...
// Sets the JPanel's cursor to the system default.
p.setCursor(Cursor.getDefaultCursor());

这篇关于如何在 Swing 应用程序中隐藏光标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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