gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 06:35:04 24 4
gpt4 key购买 nike

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

lbl.setBackground(新颜色(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);

最佳答案

我明白您为什么不想加载 Swing,因为它只是为了炫耀。 Sun/Oracle 自己的实现 SplashScreen一路都是AWT。

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


如 camickr 所述,请参阅 How to Create a Splash Screen举个例子。

Splash using SplashScreen

现在这就是我要说的。


至于标签,把它们放在外面。使用 FontMetrics 或(更好的)TextLayout 确定文本的大小/位置,然后直接将其绘制到 Image 上。

有关使用 TextLayout 类的示例,请参阅 trashgod's answer到“Java2D 图形抗锯齿”。

关于java - 如何使 java.awt.Label 背景透明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6399600/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com