简体   繁体   English

在Swing中,是否可以从工具箱中提取预定义的鼠标光标图像?

[英]In Swing, is there a way to extract a predefined mouse cursor Image from the toolkit?

I'd like to make a custom help cursor by "badging" the built-in default mouse cursor with a question mark when the user is hovering over an object that can be clicked for context-sensitive help. 当用户将鼠标悬停在可以单击以获取上下文相关帮助的对象上时,我想通过用问号“标记”内置默认鼠标光标来创建自定义帮助光标。 I'd like this to work nicely across platforms/look-and-feels (to look consistent with the white Windows mouse and the black Mac mouse, for instance.) Is there a way to get the cursor Image from the current Toolkit so that I could generate a combined Image to set as the cursor? 我希望它可以在各种平台/外观上很好地工作(例如,看起来与白色的Windows鼠标和黑色的Mac鼠标一致。)是否有办法从当前工具包中获取光标图像,以便我可以生成一个组合图像设置为光标吗?

This question points out that the information can't be gotten from the Cursor object. 这个问题指出不能从Cursor对象获取信息。 There's also a comment there that suggested fishing around in the JRE, which I've also tried a bit: There and in google images, I didn't find any straightforwardly accessible graphics files to plunder 那里还有一条评论建议在JRE中钓鱼,我也尝试了一下:在那里和google图片中,我没有找到任何可直接访问的图形文件来掠夺

An alternative would be to add a mouseMoved listener and draw manually a little to the right of the cursor (on the parent, I suppose, to avoid clipping at the borders?) but I was a bit concerned about overhead, and in initial explorations, this was looking very complicated. 一种替代方法是添加mouseMoved侦听器,然后在光标右侧稍稍手动绘制(我想是在父级上,以避免在边界处出现剪切?),但是我有点担心开销,在最初的探索中,这看起来非常复杂。 I'd take other suggestions about finding or making a nice help cursor as well. 我也会就找到或制作一个很好的帮助光标提出其他建议。 (The hand is the best built-in, but it doesn't say "help" as clearly as a question-mark.) (指针是最好的内置指针,但没有像问号那样清楚地显示“帮助”。)

In general, no. 一般来说,没有。 Most cursors are owned by the platform's host operating system, but a few live in $JAVA_HOME/lib/images/cursors/ , for example: 大多数游标归平台的主机操作系统所有,而少数$JAVA_HOME/lib/images/cursors/位于$JAVA_HOME/lib/images/cursors/ ,例如:

$ ls -1 lib/images/cursors/
cursors.properties
invalid32x32.gif
motif_CopyDrop32x32.gif
motif_CopyNoDrop32x32.gif
motif_LinkDrop32x32.gif
motif_LinkNoDrop32x32.gif
motif_MoveDrop32x32.gif
motif_MoveNoDrop32x32.gif

I'm not sure this is the best solution in your case, because a good built-in mouse cursor should be the best. 我不确定这是否是您的最佳解决方案,因为良好的内置鼠标光标应该是最好的。 Anyway you can use mouse listeners and draw on a glasspane according to the mouse position. 无论如何,您都可以使用鼠标侦听器并根据鼠标位置在玻璃板上画图。 Here's a glasspane drawing example. 这是一个玻璃窗格绘图示例。

Java uses the default system cursor except for the Drag-and-Drop cursor, where it is using it's own cursors. Java使用默认的系统光标,但拖放光标除外,它使用的是自己的光标。

So for every cursors but DnD , refer to Extract cursor image in Java . 因此,对于除DnD之外的所有游标,请参阅用Java提取游标图像 JNA has to be used and can be easily added to any Maven project. 必须使用JNA,并且可以轻松地将其添加到任何Maven项目中。

For DnD cursors, the solution from trashgod is good. 对于DnD游标,trashgod的解决方案很好。 They can be loaded this way: 可以通过以下方式加载它们:

private static Path customSystemCursorPath = null;

public static Image loadDnDCursorImage(String cursorName) throws IOException {

    if (customSystemCursorPath == null) {
        String jhome = System.getProperty("java.home", "????");
        customSystemCursorPath = Paths.get(jhome, "lib", "images", "cursors");
    }

    // TODO change this to retrieve the cursor filename from  cursors.properties
    cursorName = "win32_" + cursorName + "32x32.gif";

    Path cursorPath = customSystemCursorPath.resolve(cursorName);

    Image image = ImageIO.read(cursorPath.toFile());
    return image;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM