简体   繁体   English

Java Swing自定义光标不可见

[英]Java Swing Custom Cursor is invisible

I made a custom cursor using this tutorial . 我使用本教程制作了一个自定义光标。 Problem is, as soon as it changes, i just get nothing. 问题是,一旦改变,我什么也得不到。 The cursor is invisible. 光标不可见。 I tried the pencil image given there, a custom image i quickly have drawn in paint, but they all don't work. 我尝试了那里提供的铅笔图像,很快就用油漆画了一个自定义图像,但是它们都不起作用。

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

This is inside a JFrame ofcourse. 这在课程的JFrame内部。

". If the image to display is invalid, the cursor will be hidden (made completely transparent), and the hotspot will be set to (0, 0)." “。如果要显示的图像无效,则光标将被隐藏(使其完全透明),并且热点将设置为(0,0)。” This is written in the javadoc of createCustomCursor(), but it should work with the pencil.gif? 这是用createCustomCursor()的javadoc编写的,但是应该可以使用Pencil.gif吗?

Thanks for the answers in advance! 预先感谢您的答复! :) :)

Your code works for me. 您的代码对我有用。 I am betting that the toolkit can't find your image and therefore is unable to display it. 我敢打赌,该工具包找不到您的图像,因此无法显示它。 Here is a complete working example that uses the same code as yours (except I used a public Image from a URL): 这是一个完整的工作示例,它使用与您相同的代码(除了我使用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();
                }
            }
        });
    }
}

If you are using Java 1.5, this SO question might be interesting to you. 如果您使用的是Java 1.5,那么您可能会很感兴趣这个SO问题 If you use something newer, this code works perfectly for me as well (just tried it). 如果您使用更新的代码,那么此代码对我也非常适用(只需尝试一下)。 If the image is not there or can not be accessed correctly, then I experience the same effect as you. 如果图像不存在或无法正确访问,那么我会遇到与您相同的效果。

Make sure that the image is loaded correctly. 确保正确加载图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM