如何在 Java 中更改光标图标?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4274606/
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 change cursor icon in Java?
提问by DYL
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.
我想在执行 Java 应用程序时将光标图标更改为我自定义的 32x32 图像。我查看并搜索,我发现的只是在 JComponent 上设置光标。但是我希望光标在移动、浏览和单击的任何地方都更改为我指定的图标,只要 Java 应用程序仍在运行,或者您可以说程序运行时。
Thanks alot.
非常感谢。
回答by stacker
Call Component.setCursor. The class Cursoras a few predefined cursors.
调用Component.setCursor。Cursor类作为几个预定义的游标。
A custom cursor image can be created:
可以创建自定义光标图像:
setCursor(Toolkit.getDefaultToolkit().createCustomCursor(
new ImageIcon("custom.png").getImage(),
new Point(0,0),"custom cursor"));
回答by Mohamed Saligh
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
您可以下载包含示例源的 zip:这里
回答by camickr
Try settin the cursor on the rootPane.
尝试在 rootPane 上设置光标。
frame.getRootPane().setCursor(...);
回答by Tony
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.
为什么没有扩展 JFrame 的 MyFrame 类。它所做的只是调用 JFrame 构造函数并将光标设置为所需的光标。在我的应用程序中,我们有一个没有光标的触摸屏,所以这就是我打算实现它的方式。
回答by Abhishek Singh
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);
}