博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
线程--demo3
阅读量:5282 次
发布时间:2019-06-14

本文共 4757 字,大约阅读时间需要 15 分钟。

示例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:jointestpackage 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

你可能感兴趣的文章
yaf(3) 正则路由
查看>>
Spring 4 官方文档学习(十三)集成其他web框架
查看>>
BZOJ3517 翻硬币
查看>>
Redis数据结构之intset
查看>>
自动化测试
查看>>
扩展欧几里德
查看>>
eclipse修改vm页面JavaScript代码颜色
查看>>
call()和apply()函数
查看>>
x86 保护方式 简介 一
查看>>
three.js鼠标交互
查看>>
sublimetext
查看>>
在linux下如何将文件夹打包
查看>>
MultipartFile 文件上传
查看>>
js 控制 easyui datagrid 隐藏之后显示不来的问题
查看>>
laravel配置文件(自定义配置文件)
查看>>
017_set
查看>>
排序算法:归并排序
查看>>
mysql 常用函数
查看>>
正则表达式
查看>>
Android开发视频学习(1)
查看>>