Java自定义光标在新计算机上不起作用 [英] Java custom cursor won't work on new computer

查看:146
本文介绍了Java自定义光标在新计算机上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近购买了一台新计算机,然后将项目从旧计算机移到了新计算机.我对所有项目都进行了编译,并且它们都运行良好,并且大多数仍在新计算机上运行,​​但是特别是一个项目不会显示我已移动的自定义光标.我确定我随项目一起移动图片只是为了排除这种情况.我重写了源代码以使其与新计算机上的新位置匹配,但仍不会显示.它给了我错误信息:

I recently bought a new computer and I moved my projects from my old one to my new one. I did a compilation on all of my projects and they all worked fine, and most of them still do on my new computer, but one project in particular wouldn't display the custom cursor that I had moved. I made sure that I moved the picture with the project just to rule that out. I rewrote the source to match the new location on my new computer, but it still won't display. It gives me the error message:

Exception in thread "main" java.lang.IndexOutOfBoundsException: invalid hotSpot
    at sun.awt.CustomCursor.<init>(Unknown Source)
    at sun.awt.windows.WCustomCursor.<init>(Unknown Source)
    at sun.awt.windows.WToolkit.createCustomCursor(Unknown Source)
    at wtalfvn.Window.<init>(Window.java:32)
    at wtalfvn.Main.main(Main.java:9)

我的旧计算机是32位,新计算机是64位,都运行在Windows 7上,我正在使用eclipse Kepler,但是使用Cursor和Toolkit时有关系吗?

My old computer is a 32 bit and my new one is a 64 bit, both run on Windows 7, I am using eclipse Kepler, but does it matter when using the Cursor and Toolkit?

这是我用来创建游标的代码

Here is my code I used to create my Cursor

Image cursor = Toolkit.getDefaultToolkit().getImage("graphx/PNG/cursor.png");
Cursor c = Toolkit.getDefaultToolkit().createCustomCursor(cursor,new Point(this.getX(),this.getY()), "cursor");
this.setCursor(c);

这是所有想要看的人的完整代码.

Here is the whole code for those who want to see it.

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

public class Window extends JFrame{

Image ico= Toolkit.getDefaultToolkit().getImage("graphx/ico/icon.PNG");

TextBox tb=new TextBox();

public Window(){
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(800,600);
    setVisible(true);
    setFocusable(true);
    getContentPane().setBackground(Color.BLACK);
    setIconImage(ico);
    setLocationRelativeTo(null);
    setResizable(false);
    setTitle("MYTITLE");
    addKeyListener(new KeyAdapter(){
        public void keyTyped(KeyEvent e) {
            if (e.getKeyChar()==KeyEvent.VK_ESCAPE){
                System.exit(0);
            }
        }
    });
    Image cursor = Toolkit.getDefaultToolkit().getImage( getClass().getResource("/graphx/PNG/cursor.png"));
    Cursor c = Toolkit.getDefaultToolkit().createCustomCursor(cursor,new Point(this.getX(),this.getY()), "cursor");
    setCursor(c);
}
}

推荐答案

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

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

这篇关于Java自定义光标在新计算机上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆