gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 14:50:03 26 4
gpt4 key购买 nike

我最近买了一台新电脑,并将我的项目从旧电脑转移到新电脑。我对所有项目进行了编译,它们都运行良好,其中大多数在我的新计算机上仍然可以运行,但有一个项目不会显示我移动的自定义光标。我确保将图片与项目一起移动只是为了排除这种情况。我重写了源代码以匹配我的新计算机上的新位置,但它仍然无法显示。它给了我错误消息:

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 时有关系吗?

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

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);

编辑:这是完整的代码,供那些想查看的人使用。

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);
}
}

最佳答案

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

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

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

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

CursorExample

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

另一种可能是图片实际上没有加载...

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

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

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

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

或模拟加载图像。

您可以使用ImageIO.read来测试这一点因为这会抛出 IOException如果由于某种原因图片无法加载

关于Java 自定义光标在新计算机上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23902159/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com