Java Swing自定义光标不可见 [英] Java Swing Custom Cursor is invisible

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

问题描述

我使用此教程创建了自定义光标.问题是,一旦改变,我什么也得不到.光标不可见.我尝试了那里提供的铅笔图像,很快就用油漆画了一个自定义图像,但是它们都不起作用.

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

这是在课程的JFrame内部.

This is inside a JFrame ofcourse.

.如果要显示的图像无效,则光标将被隐藏(使其完全透明),并且热点将设置为(0,0). 这是用createCustomCursor()的javadoc编写的,但是它应该可以使用Pencil.gif吗?

". If the image to display is invalid, the cursor will be hidden (made completely transparent), and the hotspot will be set to (0, 0)." This is written in the javadoc of createCustomCursor(), but it should work with the pencil.gif?

感谢您提前回答! :)

Thanks for the answers in advance! :)

推荐答案

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

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

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

这篇关于Java Swing自定义光标不可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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