【发布时间】:2011-09-17 23:32:48
【问题描述】:
我以前用 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);
}
}
}
和
lbl= new Label();
lbl.setBackground(new Color(0, 0, 0, 0));
splashBackground = new SplashBackground();
splashBackground.add(appNameLabel, null);
【问题讨论】:
-
不知道为什么你的 JLabel 需要设置它的背景,因为它默认不是不透明的,所以你可以将它的背景设置为任何东西,它会是透明的。回到你的问题:为什么 AWT 而不是 Swing 呢?
-
@hovercraft,不知道我是怎么错过你的评论的,它是在我发表评论前 3 小时发表的,+1。
-
@Hovercraft Full Of Eels:我认为这是因为我将标签放在自定义面板中(请参阅问题中的更新)。 AWT,因为它加载速度更快。那是用于启动窗口。那是一个“学习”项目。
标签: java label transparency awt transparent