这是一个带有两个标签的小例子。一个是完全不透明的,而另一个是半透明的。这可以与 JPanel 一起工作,但出于演示目的,它对 JLabel 更具示例性:
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import com.sun.awt.AWTUtilities;
public class Test3 {
protected static void initUI() {
JFrame frame = new JFrame("test");
frame.setUndecorated(true);
AWTUtilities.setWindowOpaque(frame, false);
JLabel label = new JLabel("Hello NOT transparent label");
label.setOpaque(true);
label.setBackground(new Color(255, 0, 0));
JLabel transLabel = new JLabel("Hello transparent label");
transLabel.setOpaque(true);
transLabel.setBackground(new Color(255, 0, 0, 50));
frame.setLocationByPlatform(true);
frame.getContentPane().add(label);
frame.getContentPane().add(transLabel, BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
initUI();
}
});
}
}