Java 如何在 Swing 应用程序中隐藏光标?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1984071/
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
How to hide cursor in a Swing application?
提问by Jessy
Is there a way to hide cursor (other than using transparent image as a cursor)?
有没有办法隐藏光标(除了使用透明图像作为光标)?
采纳答案by coobird
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.
看来,Cursor
类没有一个“空白”光标,首先,这样一个可以定义一个新的“空白”游标使用Toolkit.createCustomCursor
方法。
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);
Edit
编辑
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
没有游标结束的所有内容的评论,似乎Component
包含在 the中的sJFrame
最终将继承容器的游标(JFrame
),所以如果要求有一定Component
的游标出现,必须手动设置所需的光标。
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
包含JFrame
,则可以JPanel
使用以下Cursor.getDefaultCursor
方法将其光标设置为系统默认值:
JPanel p = ...
// Sets the JPanel's cursor to the system default.
p.setCursor(Cursor.getDefaultCursor());
回答by swap_i
I solve this problem much easier:
我更容易解决这个问题:
final Timer cursorTimer = new Timer(2000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
getContentPane().setCursor(null);
}
});
cursorTimer.start();
addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
getGlassPane().setCursor(Cursor.getDefaultCursor());
cursorTimer.restart();
}
});
回答by Lenar Hoyt
When using the LWJGL under Mac OS you need to do this:
在 Mac OS 下使用 LWJGL 时,您需要执行以下操作:
System.setProperty("apple.awt.fullscreenhidecursor","true");
回答by Dennis
frame.setCursor(frame.getToolkit().createCustomCursor(
new BufferedImage(3, 3, BufferedImage.TYPE_INT_ARGB), new Point(0, 0),
"null"));
回答by Dennis
tl;drAWT Toolkits are still bugged in 2017'; the proper solution is thus to call
tl;博士AWT 工具包在 2017 年仍然被窃听'; 因此,正确的解决方案是调用
w.setCursor( w.getToolkit().createCustomCursor(
new BufferedImage( 1, 1, BufferedImage.TYPE_INT_ARGB ),
new Point(),
null ) );
instead.
反而。
As per Javadoc page for createCustomCursor
,
根据Javadoc 页面createCustomCursor
,
Creates a new custom cursor object. If the image to display is invalid, the cursor will be hidden (made completely transparent), and the hotspot will be set to (0, 0).
创建一个新的自定义光标对象。如果要显示的图像无效,光标将被隐藏(完全透明),热点将设置为 (0, 0)。
It would follow from that that
由此可知
w.setCursor( w.getToolkit().createCustomCursor( null, null, null ) );
should do the trick. Sadly, there is a bug related to this case NOT handled by the code, see e.g. http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7150089(this particular is for MacOS, but by browsing the source you may easily find that there's no checking for 1st param Image
value validity in anyof the Toolkit
platform implementations; there's tracker.isErrorAny()
checking, which doesn't do it's job in this case), so passing null
or invalid Image
simply throws a NPEx.
应该做的伎俩。可悲的是,有一个与此案例相关的错误未由代码处理,请参见例如http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7150089(此特定适用于 MacOS,但通过浏览源代码,您可以很容易地发现,有一个为1 PARAM没有检查Image
中值有效性任何的的Toolkit
平台实现,有tracker.isErrorAny()
检查,不这样做在这种情况下工作),所以传递null
或无效Image
只是抛出一个NPEx。
回答by vandale
In the documentation it says that if the image is invalid, then it will be transparent by default so therefore passing an empty image will result in a transparent cursor. But Passing null
into the method for the image will result in an exception.
在文档中它说如果图像无效,那么默认情况下它将是透明的,因此传递空图像将导致透明光标。但是null
传入图像的方法会导致异常。
Toolkit tk= getToolkit();
Cursor transparent = tk.createCustomCursor(tk.getImage(""), new Point(), "trans");