如何创建一个半透明的 JButton?

How to create a semi-transparent JButton?

我想创建一个不完全透明但背景模糊的按钮,就像有人用砂纸打磨一块透明塑料一样。 我如何创建这样的东西?因为目前我只找到了如何让它完全透明。

JButton.setOpaque(boolean)

正如评论中所述,它是通过使用 setOpaque 方法完成的。

https://docs.oracle.com/javase/7/docs/api/javax/swing/JComponent.html#setOpaque(boolean)

我先看看 How to Decorate Components with the JLayer Class

模糊...

JLayer<JComponent> layeredButton = new JLayer<>(bluredButton, new BlurLayerUI());

public class BlurLayerUI extends LayerUI<JComponent> {
    private BufferedImage mOffscreenImage;
    private BufferedImageOp mOperation;
    
    private int radius = 3;

    public BlurLayerUI() {            
        int size = radius * 2 + 1;
        float weight = 1.0f / (float)(size * size);
        float[] blurKernel = new float[size * size];
        for (int index = 0; index < blurKernel.length; index++) {
            blurKernel[index] = weight;
        };
        mOperation = new ConvolveOp(
                new Kernel(size, size, blurKernel),
                ConvolveOp.EDGE_NO_OP, null);
    }

    @Override
    public void paint(Graphics g, JComponent c) {
        int w = c.getWidth();
        int h = c.getHeight();

        if (w == 0 || h == 0) {
            return;
        }

        // Only create the off-screen image if the one we have
        // is the wrong size.
        if (mOffscreenImage == null
                || mOffscreenImage.getWidth() != w
                || mOffscreenImage.getHeight() != h) {
            mOffscreenImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
        }

        Graphics2D ig2 = mOffscreenImage.createGraphics();
        ig2.setClip(g.getClip());
        super.paint(ig2, c);
        ig2.dispose();

        Graphics2D g2 = (Graphics2D) g;
        g2.drawImage(mOffscreenImage, mOperation, 0, 0);
    }
}

nb:我更喜欢使用JH Lab's filters for blurring, it generally produces better results. GitHub,显然它在Maven中也可用

阿尔法...

JLayer<JComponent> layeredButton = new JLayer<>(bluredButton, new AlphaLayerUI(0.1f));

public class AlphaLayerUI extends LayerUI<JComponent> {
    private BufferedImage mOffscreenImage;
    private BufferedImageOp mOperation;
    
    private float alpha;

    public AlphaLayerUI(float alpha) {            
        this.alpha = alpha;
    }

    @Override
    public void paint(Graphics g, JComponent c) {
        int w = c.getWidth();
        int h = c.getHeight();

        if (w == 0 || h == 0) {
            return;
        }

        // Only create the off-screen image if the one we have
        // is the wrong size.
        if (mOffscreenImage == null
                || mOffscreenImage.getWidth() != w
                || mOffscreenImage.getHeight() != h) {
            mOffscreenImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
        }

        Graphics2D ig2 = mOffscreenImage.createGraphics();
        ig2.setComposite(AlphaComposite.SrcOver.derive(alpha));
        ig2.setClip(g.getClip());
        super.paint(ig2, c);
        ig2.dispose();

        Graphics2D g2 = (Graphics2D) g;
        g2.drawImage(mOffscreenImage, mOperation, 0, 0);
    }
}

合并...

JLayer<JComponent> layeredButton = new JLayer<>(bluredButton, new SandPaperLayerUI(0.25f));

public class SandPaperLayerUI extends LayerUI<JComponent> {
    private BufferedImage mOffscreenImage;
    private BufferedImageOp mOperation;
    
    private int radius = 3;
    private float alpha = 1.0f;

    public SandPaperLayerUI(float alpha) {            
        this.alpha = alpha;
        int size = radius * 2 + 1;
        float weight = 1.0f / (float)(size * size);
        float[] blurKernel = new float[size * size];
        for (int index = 0; index < blurKernel.length; index++) {
            blurKernel[index] = weight;
        };
        mOperation = new ConvolveOp(
                new Kernel(size, size, blurKernel),
                ConvolveOp.EDGE_NO_OP, null);
    }

    @Override
    public void paint(Graphics g, JComponent c) {
        int w = c.getWidth();
        int h = c.getHeight();

        if (w == 0 || h == 0) {
            return;
        }

        // Only create the off-screen image if the one we have
        // is the wrong size.
        if (mOffscreenImage == null
                || mOffscreenImage.getWidth() != w
                || mOffscreenImage.getHeight() != h) {
            mOffscreenImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
        }

        Graphics2D ig2 = mOffscreenImage.createGraphics();
        ig2.setComposite(AlphaComposite.SrcOver.derive(alpha));
        ig2.setClip(g.getClip());
        super.paint(ig2, c);
        ig2.dispose();

        Graphics2D g2 = (Graphics2D) g;
        g2.drawImage(mOffscreenImage, mOperation, 0, 0);
    }
}