简体   繁体   English

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

[英]Swing: How to set Custom Cursor as transparent?

I'm changing a cursor, using a PNG image (with transparency), but when I run the code below, the image doesn't look like it should. 我正在使用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);    
}

This method is called in the Jframe's constructor. 在Jframe的构造函数中调用此方法。

在此处输入图片说明

This is the cursor1.png image, sized 25x25px. 这是cursor1.png图片,大小为25x25px。

After running the code: 运行代码后:

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

If I use cursor1.png as a JLabel , it looks OK: 如果我将cursor1.png用作JLabel ,则看起来cursor1.png

在此处输入图片说明

MCVE 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("http://i.stack.imgur.com/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);
    }
}

Can anyone tell why that happens? 谁能说出为什么发生?

The issue is Windows; 问题是Windows。 transparent pixels just aren't taken into account. 透明像素只是不考虑在内。

There is a really good answer on how to fix this on this post . 对于如何解决这一问题的一个很好的回答这个职位 Another good answer here . 这里的另一个很好的答案。

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

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