Java - 在 JPanel 中设置不透明度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3562225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 01:52:45  来源:igfitidea点击:

Java - set opacity in JPanel

javajpanelopacity

提问by test

Let's say I want to make the opacity of a JPanel %20 viewable? I don't mean setOpaque (draw or not draw) or setVisible (show or hide)... I mean make it see-through JPanel.. you know?

假设我想让 JPanel %20 的不透明度可见?我的意思不是 setOpaque(绘制或不绘制)或 setVisible(显示或隐藏)...我的意思是让它透明 JPanel .. 你知道吗?

Is this possible?

这可能吗?

采纳答案by camickr

panel.setBackground( new Color(r, g, b, a) );

You should also look at Backgrounds With Transparencyto understand any painting problems you might have when you use this.

您还应该查看具有透明度的背景以了解您在使用它时可能遇到的任何绘画问题。

回答by Mike

AWTUtilities.setWindowOpacity(aWindow, aFloat);

Where aWindow is the Swing component, and aFloat is the opacity.

其中 aWindow 是 Swing 组件,aFloat 是不透明度。

回答by djakapm

How about override the paintComponent method of the JPanel(in order to do this you have to sub class the JPanel itself and implement your own paintComponent method) inside the paintComponent you can retrieve a buffered image of the component, from there you can manipulate the alpha of the buffered image and paint it back to the JPanel. I have red this long time ago. Still looking for the code.

如何在paintComponent中覆盖JPanel的paintComponent方法(为了做到这一点,您必须对JPanel本身进行子类化并实现您自己的paintComponent方法),您可以在paintComponent中检索组件的缓冲图像,从那里您可以操纵alpha缓冲图像并将其绘制回 JPanel。我很久以前就红了。还在找代码。

回答by OscarRyz

Use the alpha attribute for the color.

使用颜色的 alpha 属性。

For instance:

例如:

panel.setBackground(new Color(0,0,0,64));

Will create a black color, with 64 of alpha ( transparency )

将创建一个黑色,具有 64 的 alpha(透明度)

Resulting in this:

结果是这样:

sample

样本

Here's the code

这是代码

package test;

import javax.swing.*;
import java.awt.Color;
import java.awt.BorderLayout;

public class See {
    public static void main( String [] args ){
        JFrame frame = new JFrame();
        frame.setBackground( Color.orange );


        frame.add( new JPanel(){{
                        add( new JLabel("Center"));
                        setBackground(new Color(0,0,0,64));
                    }} , BorderLayout.CENTER );
        frame.add( new JLabel("North"), BorderLayout.NORTH);
        frame.add( new JLabel("South"), BorderLayout.SOUTH);

        frame.pack();
        frame.setVisible( true );
    }
}

With out it it looks like this:

没有它它看起来像这样:

setBackground( new Color( 0,0,0 )  ); // or setBackground( Color.black );

alt text

替代文字

回答by Jonny Santy

It doesn't work so well on windows 7.

它在 Windows 7 上效果不佳。

panel.setBackground( new Color(r, g, b, a) );

the alpha channel just lightens the color.

alpha 通道只是使颜色变亮。

when an element is updated on a color with an alpha channel, the computer gets confused and resets the background of the updated element without an alpha. I'm going to try

当使用 alpha 通道在颜色上更新元素时​​,计算机会感到困惑并在没有 alpha 的情况下重置更新元素的背景。我要试试

AWTUtilities.setWindowOpacity(aWindow, aFloat);

next.

下一个。

回答by Hyman'

If you have a custom panel and want the whole thing to be semi-transparent, I advise you to do override its method paintComponent like this :

如果您有一个自定义面板并希望整个面板是半透明的,我建议您像这样覆盖它的方法paintComponent:

@Override
    protected void paintComponent(Graphics graphics) {
        super.paintComponent(graphics);
        Graphics2D g2d = (Graphics2D) graphics;
        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
    }