Java Swing自定义光标是不可见的

wlf*_*bck 1 java swing custom-cursor

我使用本教程制作了一个自定义光标.问题是,一旦改变,我就什么也得不到.光标不可见.我尝试了那里给出的铅笔图像,我快速绘制的自定义图像,但它们都不起作用.

    public Cursor stoneCursor;
    Toolkit toolkit = Toolkit.getDefaultToolkit();
    Image image = toolkit.getImage("pencil.gif");
    Point hotspot = new Point(0,0);
    stoneCursor = toolkit.createCustomCursor(image, hotspot, "Stone");
    getContentPane().setCursor(stoneCursor);
Run Code Online (Sandbox Code Playgroud)

这是一个JFrame的课程.

".如果要显示的图像无效,光标将被隐藏(完全透明),热点将被设置为(0,0)." 这是在createCustomCursor()的javadoc中编写的,但它应该与pencil.gif一起使用?

谢谢你的答案提前!:)

Gui*_*let 5

你的代码适合我.我打赌工具包找不到您的图像,因此无法显示它.这是一个完整的工作示例,使用与您相同的代码(除了我使用了URL中的公共图像):

import java.awt.Cursor;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class TestCursor {

    protected void initUI() throws MalformedURLException {
        JFrame frame = new JFrame("Test text pane");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Image image = toolkit.getImage(new URL("http://fc03.deviantart.net/fs71/f/2010/094/7/9/Kalyan_hand_cursor_1_by_Zanten.png"));
        Point hotspot = new Point(0, 0);
        Cursor cursor = toolkit.createCustomCursor(image, hotspot, "Stone");
        frame.setCursor(cursor);

        frame.setSize(600, 400);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                try {
                    new TestCursor().initUI();
                } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)