实现接口
package button2;
import java.awt.Container;
import java.net.URL;
import javax.swing.*;
public class getmain extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
private JLabel jl=new JLabel();//声明JLabel对象
private static Thread t;//声明线程对象
private int count=0;//计数变量
private Container container=getContentPane();//容器
public getmain() { //构造方法
setBounds(0,0,2000,2000); // 绝对定位窗体的大小位置
container.setLayout(null);//不用任何布局管理器
URL url=getmain.class.getResource("Daijunyi.jpg");//获取图片地址
Icon icon=new ImageIcon(url); //实例化Icon
jl.setIcon(icon); //将图片放在标签中
jl.setHorizontalAlignment(SwingConstants.CENTER); //
jl.setBounds(0,0,1000,1000); //设置标签大小和位置
jl.setOpaque(true);
t=new Thread(new Runnable() {//定义匿名内部类,实现Runnable接口
public void run() {
while(count<=1000) {
jl.setBounds(count,200,500,500);
try {
Thread.sleep(400);
}catch(Exception e) {
e.printStackTrace();
}
count+=50;
if(count==1000) {
count=0;
}
}
}
});
t.start();
container.add(jl);
setVisible(true);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
public static void main(String args[]) {
new getmain();
}
}
///继承Thread类
直接继承重写run()方法即可