且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Java自定义光标在新计算机上不起作用

更新时间:2022-12-23 22:24:53

光标热点应相对于光标图像...

The cursor hot spot should relative to the cursor image...

可能的原因是给定的x/y坐标超出了图像的可见范围...

The likely cause is the fact that the given x/y coordinates are outside the visible range of the image...

 Cursor c = Toolkit.getDefaultToolkit().createCustomCursor(cursor,new Point(this.getX(),this.getY()), "cursor");

例如,假设以下光标为32x32像素...

For example, assuming the following cursor is 32x32 pixels...

光标热点将在26x0左右,这表示将触发鼠标事件并且将PointMouseEvent注册为已发生的点

The cursor hot spot would be around 26x0, this represents the point at which mouse events would be triggered and the Point the MouseEvent would be registered as having occured

另一种可能性是图像实际上没有被加载...

The other possibility is that the image is actually not been loaded...

Image cursor = Toolkit.getDefaultToolkit().getImage("graphx/PNG/cursor.png");

getImage期望该值表示文件位置,在此示例中,这意味着该文件应相对于程序执行的位置

getImage expects that the value represent a file location, which in this example, means the file should be relative to the location that the program is been executed

如果图像实际上是嵌入式资源,则应该使用

If the image is actually an embedded resource, you should be using

Image cursor = Toolkit.getDefaultToolkit().getImage(
    getClass().getResource("/graphx/PNG/cursor.png"));

或类似图片以加载图像.

or simular to load the image.

您可以使用ImageIO.read进行测试,因为如果由于某种原因无法加载图像,则会抛出IOException

You can test this by using ImageIO.read as this will throw an IOException if the image can't be loaded for some reason