JLabel默认是透明的,所以JLabel设置背景色setBackground之前要setOpaque(true);
将不透明设为true,再setBackground
jlabel.setOpaque(true); jlabel.setBackground(new Color(0, 0, 0, 100));
//将不透明设为true,再setBackground
jlabel.setOpaque(true); jlabel.setBackground(new Color(0, 0, 0, 100));
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.*;
import javax.swing.*;
public class JLabel默认是透明的_所以JLabel设置背景色setBackground之前要setOpaque为true {
public static void main(String...arguments)throws Exception{
JFrame frame = new JFrame(Thread.currentThread().getStackTrace()[1].getClassName());
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent windowEvent) {
System.out.println(windowEvent);
System.exit(0);
}
});
frame.setBounds(100, 100, 1024, 768);
JLabel jlabel = new JLabel("""
<html><head></head><body>
<div style='background:rgba(100,100,255,0.5); text-align:center;'>
JLabel默认是透明的_所以JLabel设置背景色setBackground之前要setOpaque为true
<br/>
//将不透明设为true,再setBackground<br/>
label.setOpaque(true); jlabel.setBackground(new Color(0, 0, 0, 200));
JLabel中的html的背景色不会覆盖整个JLable, html的background在JLabel的background之上
</div>
</body></html>
""");
frame.add(jlabel);
//将不透明设为true,再setBackground
jlabel.setOpaque(true); jlabel.setBackground(new Color(0, 0, 0, 100));
frame.setVisible(true);
}
}