package view;
import javax.swing.*;
import java.awt.*;
public class TestBody extends JFrame{
public TestBody(){
this.setSize(200, 200);
this.getContentPane().setLayout(null);
this.setTitle("测试动画");
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
// 动画的承载体
JLabel jLabel = new JLabel();
// 初始化第一张图
this.cgJLabelImg(jLabel,"a1.png");
this.add(jLabel);
// 框体透明
this.setUndecorated(true); // 取消窗口标题栏
this.setBackground(new Color(0,0,0,0));// 背景透明
this.setVisible(true);
// 动画线程
new Thread(() -> {
int i=1;
try{
while (true){
Thread.sleep(250);
cgJLabelImg(jLabel,"a"+ i++ +".png");
if(i>3)i=1;
}
}catch (Exception e){
e.printStackTrace();
}
}).start();
}
private void cgJLabelImg(JLabel jLabel,String imgUrl){
ImageIcon icon = new ImageIcon(imgUrl);
int picWidth = icon.getIconWidth(),pinHeight = icon.getIconHeight();
icon.setImage(icon.getImage().getScaledInstance(picWidth,pinHeight, Image.SCALE_DEFAULT));
jLabel.setBounds(0,0,picWidth,pinHeight);
jLabel.setIcon(icon);
}
public static void main(String[] args){
new TestBody();
}
}