第一篇博客都不知道有没有人会看。。。。。。。
首先上一张效果图吧:
原理很简单,就是新建JLabel的时候重写一下JLabel的paint方法,然后对窗体设置一下就行,上代码:
import com.sun.awt.AWTUtilities;
import javax.swing.*;
import java.awt.*;
/**
* Created by null on 15-11-15.
*/
public class ImageFrame extends JFrame {
int Frame_Width = 500;
int Frame_Height = 500;
ImageIcon icon = new ImageIcon("drawable/loading.png");
public ImageFrame() {
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(Frame_Width, Frame_Height); // 设置窗口大小
AWTUtilities.setWindowOpaque(this, false);
setLocationRelativeTo(null); //设置窗口居中
setVisible(true);
}
public static void main(String[] args) {
ImageFrame frame = new ImageFrame();
frame.setSize(500, 500);
frame.setVisible(true);
}
}
包含测试方法,复制可直接运行。。。