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

我以前用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。

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


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

使用SplashScreen飞溅

现在就是我所说的。


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

有关使用TextLayout类的示例,请参阅trashgod对“Java2D Graphics anti-aliased” 的回答 。

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

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

那什么都没做。 JLabel默认是透明的(即setOpaque(false))。 您可能需要阅读透明背景,以了解透明度如何与Swing一起使用。

我从来没有使用过Label,但是如果它像JLabel那样工作,那么我猜你会覆盖isOpaque()方法返回false。