如何使java.awt.Label背景透明?

ban*_*cer 2 java transparency label awt transparent

我以前用javax.swing.JLabel做透明背景:

lbl.setBackground(new Color(0, 0, 0, 0));.

但它不适用于java.awt.Label.有没有简单的方法使标签透明?

更新:

public class SplashBackground extends Panel {

    private static final long serialVersionUID = 1L;

    private Image image = null;

    /**
     * This is the default constructor
     */
    public SplashBackground() {
        super();
        initialize();
    }

    /**
     * This method initializes this
     * 
     */
    private void initialize() {
        image = Toolkit.getDefaultToolkit().createImage(getClass().getResource("/splash/splash.jpg"));
        this.setLayout(null);
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        if(image != null) {
            g.drawImage(image, 0,0,this.getWidth(),this.getHeight(),this);
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

lbl= new Label();
lbl.setBackground(new Color(0, 0, 0, 0));
splashBackground = new SplashBackground();
splashBackground.add(appNameLabel, null);
Run Code Online (Sandbox Code Playgroud)

And*_*son 6

我可以看到为什么你不想加载Swing,因为它是为了引起轰动.Sun/Oracle自己实现的SplashScreen是AWT.

为什么不直接使用现有的类来实现启动功能?


正如camickr所述,请参阅如何创建启动画面以获取示例.

使用SplashScreen飞溅

现在就是我所说的.


至于标签,请将它们留下.使用FontMetrics或(更好)TextLayout确定文本的大小/位置,然后直接将其绘制到Image.

有关使用TextLayout该类的示例,请参阅trashgod对'Java2D Graphics anti-aliased' 的回答.