gpt4 book ai didi

java - 如何在 Swing 应用程序中隐藏光标?

转载 作者:IT老高 更新时间:2023-10-28 20:39:55 26 4
gpt4 key购买 nike

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

当用户将鼠标指向 JFrame 中的 JPanel 外部时,我想隐藏光标。

最佳答案

似乎 Cursor类开始时没有“空白”光标,因此可以使用 Toolkit.createCustomCursor 定义一个新的“空白”光标。方法。

这是我尝试过的一种似乎可行的方法:

// 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出现光标,则必须手动设置所需的光标。

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

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

关于java - 如何在 Swing 应用程序中隐藏光标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1984071/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com