线程--demo3

示例1:SwingAndThread 

package com.etc.jichu;
import java.awt.Container;
import java.net.URL;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
public class SwingAndThread extends JFrame
{
	private JLabel jl=new JLabel();
	private static Thread t;
	private int count;
	private Container container=new Container();
	public SwingAndThread()
	{
		setBounds(300,200,250,100);
		container.setLayout(null);
		URL url=SwingAndThread.class.getResource("/004.jpg");//获取图片资源路径
		Icon icon=new ImageIcon(url);//图标选择组件Icon
		jl.setIcon(icon);
		jl.setHorizontalAlignment(SwingConstants.LEFT);
		jl.setBounds(10,10,200,50);
		jl.setOpaque(true);
		t=new Thread(new Runnable() {
			public void run() {
				while(count<=200)
				{
					jl.setBounds(count, 10, 200, 5);
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					count+=4;//使横坐标每次增加4
					if(count==200)
					{
						count=10;
					}
				}
				
			}
		});
		t.start();
		container.add(jl);
		setVisible(true);
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
	}
	public static void main(String[] args)
	{
		new SwingAndThread();

	}

}

  

示例2:SleepMethodTest 

package com.etc.jichu;

import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;

import javax.swing.JFrame;

public class SleepMethodTest extends JFrame
{
	private Thread t;
	private static Color[] color={Color.BLACK,Color.BLUE,Color.CYAN,Color.GREEN,Color.ORANGE,Color.YELLOW,Color.RED,Color.PINK,Color.LIGHT_GRAY};
	private static final Random rand=new Random();
	private static Color getC()//获取随机颜色值的方法
	{
		return color[rand.nextInt(color.length)];
	}
	public SleepMethodTest()
	{
		t=new Thread(new Runnable() {			
			int x=30;
			int y=50;
			public void run() {
				while(true)
				{
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					Graphics graphics=getGraphics();
					graphics.setColor(getC());
					graphics.drawLine(x, y, 200, y++);
					if(y>=300)
					{
						y=2800;
					}
				}
				
				
			}
		});
		t.start();
	}
	public static void init(JFrame frame,int width,int height)
	{
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(width,height);
		frame.setVisible(true);
	}
	public static void main(String[] args) 
	{
		init(new SleepMethodTest(), 600, 600);
	}

}

  

示例3:jointest

package com.etc.jichu;

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JProgressBar;

public class JoinTest extends JFrame
{
	private Thread threadA;
	private Thread threadB;
	final JProgressBar progressBar=new JProgressBar();//进度条组件
	final JProgressBar progressBar2=new JProgressBar();
	int count =0;
	public JoinTest() {
		super();
		getContentPane().add(progressBar,BorderLayout.NORTH);
		getContentPane().add(progressBar2,BorderLayout.SOUTH);
		progressBar.setStringPainted(true);
		progressBar2.setStringPainted(true);
		//匿名内部类方式实例化线程
		threadA=new Thread(new Runnable() 
		{	
			int count=0;
			public void run()
			{
				while(true)
				{
					progressBar.setValue(++count);
					try {
						Thread.sleep(100);
						threadB.join();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				
			}
		});
		threadA.start();
		threadB=new Thread(new Runnable() {
			int count=0;
			public void run() {
				while(true)
				{
					progressBar2.setValue(++count);
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					if(count==100)
						break;
				}
				
			}
		});
		threadB.start();
	}
	public static void init(JFrame frame,int width,int height)
	{
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(width,height);
		frame.setVisible(true);
	}
	public static void main(String[] args) 
	{
		init(new JoinTest(), 200, 200);

	}

}

  

示例4:线程中断

package com.etc.jichu;

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JProgressBar;

public class InterruptedSwing extends JFrame
{
Thread thread;
public InterruptedSwing() {
	super();
	final JProgressBar progressBar=new JProgressBar();//进度条组件
	getContentPane().add(progressBar,BorderLayout.NORTH);
	progressBar.setStringPainted(true);
	thread=new Thread(new Runnable() {
		int count=0;
		public void run() {
			while(true)
			{
				progressBar.setValue(++count);
				try {
					Thread.sleep(10);
				} catch (InterruptedException e) {
					System.out.println("当前线程被中断");
					break;
				}
			}
			
		}
	});
	thread.start();
	thread.interrupt();//中断线程
}
public static void init(JFrame frame,int width,int height)
{
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	frame.setSize(width,height);
	frame.setVisible(true);
}
	public static void main(String[] args) 
	{
		init(new InterruptedSwing(), 100, 200);

	}

}

  

原文地址:https://www.cnblogs.com/ipetergo/p/6242623.html