如何在Java中更改光标图标?

发布于 2024-10-04 12:15:13 字数 149 浏览 4 评论 0 原文

我想在执行 Java 应用程序时将光标图标更改为自定义的 32x32 图像。我查看并搜索,我发现那些只是将光标设置在 JComponent 上。但我希望只要 Java 应用程序仍在运行,或者可以说程序运行时,无论它移动、浏览和单击,光标都会更改为我指定的图标。

多谢。

I would like to change the cursor icon to my customized 32x32 image when a Java application is executing. I looked and searched, those I found are just setting cursor on a JComponent. But I want the cursor changed to my specified icon wherever it goes moving, browsing, and click, as long as the Java application is still running, or you can say program runtime.

Thanks alot.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

丢了幸福的猪 2024-10-11 12:15:14

尝试将光标设置在 rootPane 上。

frame.getRootPane().setCursor(...);

Try settin the cursor on the rootPane.

frame.getRootPane().setCursor(...);
南冥有猫 2024-10-11 12:15:14
public void mouseEntered(MouseEvent e)
{
// set cursor for frame and its component
//  this is the current frame you are using .
//  You can change the this keyword with your frame name .

java.awt.Toolkit toolkit = java.awt.Toolkit.getDefaultToolkit();
Image image = toolkit.getImage("/images/mousepoint.jpg");
Cursor a = toolkit.createCustomCursor(image , new Point(this.getX(),this.getY()), "");
this.setCursor (a);
}
public void mouseEntered(MouseEvent e)
{
// set cursor for frame and its component
//  this is the current frame you are using .
//  You can change the this keyword with your frame name .

java.awt.Toolkit toolkit = java.awt.Toolkit.getDefaultToolkit();
Image image = toolkit.getImage("/images/mousepoint.jpg");
Cursor a = toolkit.createCustomCursor(image , new Point(this.getX(),this.getY()), "");
this.setCursor (a);
}
染柒℉ 2024-10-11 12:15:14

对于所问问题的直接(尽我所能)回答,不,您不能使用 Java 设置全局光标。这很大程度上依赖于操作系统,但我想,出于安全原因,在大多数(如果不是全部)安全操作系统上设置全局光标是被阻止的。

然而,同样重要的是要谈一谈我认为可行的方法,但似乎不适用于我的系统,即 64 位 Windows 10 Pro。

这个答案的启发,您可以创建一个完全透明的窗口,将事件传递到其后面的窗口。 (请参阅下面的代码示例)

现在,定义 Window.setOpacity 指出这是平台相关的行为,但它具体涉及如何处理 MouseEvents,而不是如何处理鼠标光标。但是,根据 Windows 文档,设置光标是基于特定事件的,所以我们需要控制哪些事件被传递。因此,这更像是一个较低级别(很可能是 C/C++)的问题,而不是 Java 问题。

这是我用来测试它的代码示例:

    JFrame frame = new JFrame();
    
    frame.setSize(1920, 1080);          // set the size
    frame.setLocationRelativeTo(null);  // center the window
    frame.setUndecorated(true);         // make it so the frame is a basic rectangle, no topbar or outline.
    frame.setAlwaysOnTop(true);         // make it so the frame is on top
    
    frame.setOpacity(0.0f);             // make the frame transparent.
    
    frame.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
    
    frame.setVisible(true);

如果将 frame.setOpacity(0.0f); 中的参数从 0.0f 设置为 0 到 1 之间的值,那么您'您会看到光标实际上发生了变化。

如果你想冒险进入当地人的土地并使用钩子(也许你不必走得那么深),也许可以开始 此处。每当我使用 C/C++(或任何其他较低级别的语言)进行编程时,我往往会使用自己的代码,从不使用 WinAPI 并依赖于 SDL 等库,因此我不太熟悉这些系统是如何工作的级功能有效。


TL;DR,在标准 Java 中,你不能。较低级别的选项可以起作用。

我想您也可以制作自己的操作系统,甚至将其包含在 Java 中,这样您就不必在硬盘驱动器上安装一个全新的分区。

For a direct (as best as I can) answer to the question asked, no, you can not set the global cursor using Java. This is largely operating system dependent, but I would like to think that, for security reasons, setting the global cursor is blocked on most, if not all, secure operating systems.

However, it's also important to touch on a method I thought would work, but doesn't appear to work on my system, being a 64-bit Windows 10 Pro.

Inspired from this answer, you can make a fully transparent window that passes events through to the windows behind them. (see code sample below)

Now, the definition of the Window.setOpacity states this is platform-dependent behavior, but it's specifically about how MouseEvents are handled, not how the mouse cursor is handled. However, according to the Windows Documentation, setting the cursor is based on a specific event, so we'd need control which events get passed through. So, this becomes more of a lower-level (most likely C/C++) question rather than a Java question.

Here's the code sample I made to test it:

    JFrame frame = new JFrame();
    
    frame.setSize(1920, 1080);          // set the size
    frame.setLocationRelativeTo(null);  // center the window
    frame.setUndecorated(true);         // make it so the frame is a basic rectangle, no topbar or outline.
    frame.setAlwaysOnTop(true);         // make it so the frame is on top
    
    frame.setOpacity(0.0f);             // make the frame transparent.
    
    frame.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
    
    frame.setVisible(true);

If you set the parameter in frame.setOpacity(0.0f); from 0.0f to something between 0 and 1, you'll see that the cursor actually changes.

If you want to venture into the land of natives and using hooks (maybe you don't have to go that deeply), maybe start here. Whenever I program in C/C++ (or any other lower level language), I tend to be self-contained with my code, never using the WinAPI and relying on libraries like SDL, so I'm not well-versed on how these system-level functions work.


TL;DR, in standard Java, you cannot. Lower-level options can work.

I guess you could also just make your own operating system, even contain it within Java so you don't have to install a whole new partition onto your hard drive.

千笙结 2024-10-11 12:15:14

为什么你没有一个扩展 JFrame 的 MyFrame 类。它所做的只是调用 JFrame 构造函数并将光标设置为您想要的光标。在我的应用程序中,我们有一个没有光标的触摸屏,所以这就是我打算实现它的方式。

Why don't you have a class MyFrame which exteds JFrame. All it does is call the JFrame constructor and sets the cursor to your desired cursor. In my application we have a touch screen with no cursor so this is how I intend to implement it.

极度宠爱 2024-10-11 12:15:13

标准光标图像:

setCursor(Cursor.getDefaultCursor());

用户定义的图像:

Toolkit toolkit = Toolkit.getDefaultToolkit();
Image image = toolkit.getImage("icons/handwriting.gif");
Cursor c = toolkit.createCustomCursor(image , new Point(mainPane.getX(), 
           mainPane.getY()), "img");
mainPane.setCursor (c);

您可以下载包含示例源的 zip:这里

Standard cursor image:

setCursor(Cursor.getDefaultCursor());

User defined Image:

Toolkit toolkit = Toolkit.getDefaultToolkit();
Image image = toolkit.getImage("icons/handwriting.gif");
Cursor c = toolkit.createCustomCursor(image , new Point(mainPane.getX(), 
           mainPane.getY()), "img");
mainPane.setCursor (c);

You can download a zip containing sample source: HERE

挽手叙旧 2024-10-11 12:15:13

调用 Component.setCursor
Cursor 作为一些预定义的游标。

可以创建自定义光标图像:

setCursor(Toolkit.getDefaultToolkit().createCustomCursor(
new ImageIcon("custom.png").getImage(),
new Point(0,0),"custom cursor"));

Call Component.setCursor.
The class Cursor as a few predefined cursors.

A custom cursor image can be created:

setCursor(Toolkit.getDefaultToolkit().createCustomCursor(
new ImageIcon("custom.png").getImage(),
new Point(0,0),"custom cursor"));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文