如何使java.awt.Label中的背景透明? [英] How to make java.awt.Label background transparent?

查看:1413
本文介绍了如何使java.awt.Label中的背景透明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前做的透明背景javax.swing.JLabel中是这样的:

lbl.setBackground(新颜色(0,0,0,0));

但是它不与java.awt.Label中工作。有没有简单的方法,使标签透明?

更新:

 公共类SplashBackground扩展面板{    私有静态最后的serialVersionUID长1L =;    私人形象画像= NULL;    / **
     *这是默认的构造函数
     * /
    公共SplashBackground(){
        超();
        初始化();
    }    / **
     *这个方法初始化此
     *
     * /
    私人无效初始化(){
        。图像= Toolkit.getDefaultToolkit()的createImage(的getClass()的getResource(/飞溅/ splash.jpg));
        this.setLayout(NULL);
    }    @覆盖
    公共无效漆(图形G){
        super.paint(G);
        如果(形象!= NULL){
            g.drawImage(图像,0,0,this.getWidth(),this.getHeight(),这一点);
        }
    }}

  LBL =新的Label();
lbl.setBackground(新的色彩(0,0,0,0));
splashBackground =新SplashBackground();
splashBackground.add(appNameLabel,NULL);


解决方案

我明白为什么你不希望加载秋千,因为它是一个轰动。太阳/ Oracle自己的执行 闪屏的 是AWT的所有道路。

为什么不直接使用现有类的防溅功能?


正如camickr提到的,请参见如何创建一个闪屏为例。

现在的这就是的我在说什么。


至于标签,离开他们。使用的FontMetrics 或(更好的)的TextLayout 来确定文本的大小/位置,然后就直接画它的图片

有关使用的TextLayout 类的一个示例,请参阅trashgod's回答来的Java2D图形消除锯齿。

I used to do the transparent background with javax.swing.JLabel this way:

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

But it doesn't work with java.awt.Label. Is there any simple way to make the label transparent?

Update:

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);
        }
    }

}

and

lbl= new Label();
lbl.setBackground(new Color(0, 0, 0, 0));
splashBackground = new SplashBackground();
splashBackground.add(appNameLabel, null);

解决方案

I can see why you do not want to load Swing, since it is for a splash. Sun/Oracle's own implementation of SplashScreen is AWT all the way.

Why not just use that existing class for your splash functionality?


As mentioned by camickr, see How to Create a Splash Screen for an example.

Now that's what I'm talking about.


As to the labels, leave them out. Use FontMetrics or (better) TextLayout to determine the size/position of the text, then just paint it directly to the Image.

For an example of using the TextLayout class, see trashgod's answer to 'Java2D Graphics anti-aliased'.

这篇关于如何使java.awt.Label中的背景透明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆