package Thread_Test; import java.awt.Container; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.SwingConstants; public class Thread_Swing extends JFrame { /** * */ private static final long serialVersionUID = 1L; private JLabel jl = new JLabel("hi"); private static Thread t; private int count = 0; private Container c = getContentPane(); public Thread_Swing() { // TODO Auto-generated constructor stub setBounds(300, 200, 250, 100); c.setLayout(null); jl.setHorizontalAlignment(SwingConstants.LEFT); jl.setBounds(10, 10, 200, 50); t=new Thread(new Runnable() { public void run() { // TODO Auto-generated method stub while (count<=200){ jl.setBounds(count, 10, 200, 20); try { Thread.sleep(500); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } count+=4; if(count==200){ count=10; } } } }); t.start(); c.add(jl); setVisible(true); setDefaultCloseOperation(EXIT_ON_CLOSE); } public static void main(String[] args) { // TODO Auto-generated method stub new Thread_Swing(); } }
笔记:利用线程来实现JLabel的循环滚动。
实用runnable接口创建线程步骤:创建Runnable的类——》实例化对象——》写实现方法,即:thread t—— t = new thread(new runnable)——run里面写方法
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-26 05:11:08