效果图
package test;
import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import com.sun.awt.AWTUtilities;
public class ImageOpacity extends JFrame{
int fw = 500;
int fh = 500;
ImageIcon icon = new ImageIcon("h.png");
public ImageOpacity() {
JLabel backLabel = new JLabel() {
@Override
public void paint(Graphics g) {
super.paint(g);
icon.paintIcon(this, g, 0, 0);
}
};
this.add(backLabel);
setUndecorated(true); // 不装饰
setSize(fw, fh); // 设置窗口大小
AWTUtilities.setWindowOpaque(this, false); //
setLocationRelativeTo(null); //设置窗口居中
setVisible(true);
}
public static void main(String[] args) {
ImageOpacity frame = new ImageOpacity();
frame.setSize(500, 500);
frame.setVisible(true);
}
}
PS:需要导包rt.jar,否则AWTUtilities.setWindowOpaque(this, false)报错。声明一下,这不是jdk啥问题,是缺包(
学习建议)