1.第一步,创建FlyFrame 窗体
public class FlyFrame extends JFrame {
public static final int WIDTH = 400;
public static final int HEIGHT = 600;
public FlyFrame(){
//设置窗口大小
super.setSize(WIDTH,HEIGHT);
//设置一直在最上面
super.setAlwaysOnTop(true);
//设置窗体居中显示
super.setLocationRelativeTo(null);
//设置窗体关闭,退出程序
super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
super.setVisible(true);
}
}
2.第二步,添加FlyPanel 画板,并添加背景图片
public class FlyPanel extends JPanel {
public static BufferedImage background; // 背景图
int x,y;
int bgWidth ;
int bgHeight ;
public FlyPanel(){
try {
background = ImageIO.read(new File(“C:\Users\Administrator\IdeaProjects\flywx\src\imgs\background.png”));
bgWidth = background.getWidth();
bgHeight = background.getHeight();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void paint(Graphics g) {
//画背景图片
g.drawImage(background, x, y, bgWidth, bgHeight, null);
}
}
3.将画板,添加到窗体上
FlyPanel flyPanel = new FlyPanel();
super.add(flyPanel);