java - Swing:如何将自定义光标设置为透明?

标签 java swing user-interface mouse-cursor

我正在使用 PNG 图像(具有透明度)更改光标,但是当我运行下面的代码时,图像看起来并不像它应该的那样。

public void CustomCursor()
{
    Toolkit t1 = Toolkit.getDefaultToolkit();
    Image img = t1.getImage("src/AppImages/Cursor1.png");
    Point point = new Point(0,0);
    Cursor cursor = t1.createCustomCursor(img, point, "Cursor");
    setCursor(cursor);    
}

此方法在 Jframe 的构造函数中调用。

enter image description here

这是 cursor1.png 图像,大小为 25x25px。

运行代码后:

enter image description here

enter image description here

enter image description here

如果我使用 cursor1.png 作为 JLabel,它看起来没问题:

enter image description here

MCVE

import java.awt.*;
import java.net.*;
import java.util.logging.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class CustomCursor {

    private JComponent ui = null;

    CustomCursor() {
        initUI();
    }

    public void initUI() {
        if (ui != null) {
            return;
        }

        ui = new JPanel(new BorderLayout(4, 4));
        ui.setBorder(new EmptyBorder(20, 200, 20, 200));
        Toolkit t1 = Toolkit.getDefaultToolkit();
        Image img;
        try {
            URL url = new URL("/image/sJKuE.png");
            img = t1.getImage(url);
            Point point = new Point(0, 0);
            Cursor cursor = t1.createCustomCursor(img, point, "Cursor");
            ui.setCursor(cursor);
            ui.add(new JLabel(new ImageIcon(url)));
        } catch (MalformedURLException ex) {
            Logger.getLogger(CustomCursor.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                CustomCursor o = new CustomCursor();

                JFrame f = new JFrame(o.getClass().getSimpleName());
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

谁能告诉我为什么会发生这种情况吗?

最佳答案

问题是 Windows;只是不考虑透明像素。

关于如何解决这个问题,有一个非常好的答案 this post 。 另一个很好的答案here .

关于java - Swing:如何将自定义光标设置为透明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32202747/

相关文章:

java - 有没有办法找到哪个区域 BorderLayout 停靠了用户可拖动的 JToolBar

java - 是否有在监听器的句柄方法中调用 Controller 类的方法的最佳实践方法?

user-interface - 愚蠢且令人沮丧的跨浏览器 UI 问题

java - 如何使用 GridBagLayout 将组件放在右下角?

java - 为什么接口(interface)方法没有主体

java - 将两个 BufferedImages 并排复制到一个图像中

java - 如何手动将 Spring MVC View 呈现为 html?

java - Radix 在 Java 中使用队列对字符串数组进行排序

java - 如何添加 JButton 和 JLabels 而不会发生冲突?

java - JPasswordField长度跳过检查