线程--demo3
2024-09-04 14:18:25
示例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); } }
最新文章
- 如何安全的将VMware vCenter Server使用的SQL Server Express数据库平滑升级到完整版
- 如何获取应用宝APP ID
- 学习iOS的网站
- git clone简介
- Django 1.6 的测试驱动开发
- HDU2056(rectangles)
- 【转】Thunderbird中配置签名
- [C#]使用dnSpy对目标程序(EXE或DLL)进行反编译修改并编译运行
- 如何用python和苹果Turicreate学习框架来识别图像?
- element-ui 点击获取table的行索引
- 跨域学习笔记1--跨域调用webapi
- SPP空间金字塔池化技术的直观理解
- Ado.net之连接数据库
- mongodb用mongoose查库的对象,不能增加属性
- exp、Exploit、Exploit Pack、exp-gui、Payload、MetaSploit都是啥?
- Spring @Trasactionl 失效, JDK,CGLIB动态代理
- (线段树 区间合并更新)Tunnel Warfare --hdu --1540
- Netty源码分析第8章(高性能工具类FastThreadLocal和Recycler)---->;第7节: 获取异线程释放的对象
- 值得推荐的开源C/C++框架和库
- MySQL学习【第五篇SQL语句上】
热门文章
- linux find命令使用(转)
- 在Windows Server 2012上安装SharePoint 2010 SP1
- IOS-将任意对象存进数据库
- 016对象——__set __get get_class_methods get_class_vars
- OC-协议与代理
- Xcode And iOS9新特性
- Android studio 导入ApiDemo
- Android Afinal框架学习(一) FinalDb 数据库操作
- Django 之母板
- jstl错误排除:According to TLD or attribute directive in tag file, attribute value does not accept any expressions