如何在 Swing 应用程序中隐藏光标? [英] How to hide cursor in a Swing application?
问题描述
有没有办法隐藏光标(除了使用透明图像作为光标)?
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 Component
s 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屋!