我曾经用 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);
最佳答案
我明白为什么你不想加载 Swing,因为它是为了启动。 Sun/Oracle 自己的 SplashScreen
实现一路都是AWT。
为什么不直接使用现有的类来实现启动功能?
正如camickr提到的,请参阅How to Create a Splash Screen举个例子。
现在这就是我所说的。
至于标签,请将其保留。使用 FontMetrics
或(更好)TextLayout
确定文本的大小/位置,然后将其直接绘制到 Image
上。
有关使用 TextLayout
类的示例,请参阅 trashgod's answer到“Java2D 图形抗锯齿”。
关于java - 如何使java.awt.Label背景透明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6399600/