- import javax.swing.*;
- import java.awt.event.*;
- public class ThreadDemo extends JFrame implements ActionListener {
- private JProgressBar progress;// 声明进度条
- private JButton btnStart, btnPause, btnStop;// 声明按钮
- private JToolBar toolBar;// 声明状态栏
- private MyThread myThread;
- // 创建构造函数
- public ThreadDemo() {
- this.setSize(400, 150);// 设置窗体的大小
- this.setLocationRelativeTo(null);// 设置窗体的初始位置,此为绝对据中
- this.setResizable(false);// 设置窗体是否能被更改大小
- this.setLayout(null);// 窗体不使用任何的布局方式
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设置窗体的关闭方式
- this.setVisible(true);// 显示窗体
- this.init();// 加载其他组建
- this.initListener();// 加载监听事件
- }
- // 初始化其他组建
- public void init() {
- progress = new JProgressBar();
- toolBar = new JToolBar();
- toolBar.add(new JLabel("state"));
- toolBar.add(progress);
- toolBar.setFloatable(false);
- toolBar.setBounds(15, 80, 350, 21);
- btnStart = new JButton("start");
- btnStart.setBounds(12, 10, 91, 21);
- btnPause = new JButton("Pause");
- btnPause.setBounds(131, 10, 91, 21);
- btnStop = new JButton("stop");
- btnStop.setBounds(241, 10, 91, 21);
- this.getContentPane().add(btnStart);
- this.getContentPane().add(btnPause);
- this.getContentPane().add(btnStop);
- this.getContentPane().add(toolBar);
- }
- // 初始化监听事件
- public void initListener() {
- this.btnStart.addActionListener(this);
- this.btnPause.addActionListener(this);
- this.btnStop.addActionListener(this);
- }
- public static void main(String[] args) {
- new ThreadDemo();
- }
- class MyThread extends Thread {
- private boolean flag = false;// 设置线程的标志位
- private boolean pause = false;// 设置线程的暂停位
- // 重写线程的启动方法
- public void setStart() {
- this.flag = true;
- super.start();
- }
- // 重写线程的运行方法
- public void run() {
- int value = 0;
- while (flag) {
- try {
- if (pause) {
- Thread.sleep(50);
- continue;
- }
- progress.setValue(value++);
- if (progress.getValue() == progress.getMaximum()) {
- break;
- }
- Thread.sleep(50);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- // 暂停线程
- public void setPause() {
- this.pause = !pause;
- }
- // 关闭线程
- public void setStop() {
- progress.setValue(0);
- this.flag = false;
- }
- }
- // 重写监听事件
- public void actionPerformed(ActionEvent e) {
- // 开始事件
- if (e.getSource() == this.btnStart) {
- System.out.println("启动线程");
- myThread = new MyThread();
- myThread.setStart();
- }
- // 暂停事件
- if (e.getSource() == this.btnPause) {
- System.out.println("暂停线程");
- myThread.setPause();
- }
- // 停止事件
- if (e.getSource() == this.btnStop) {
- System.out.println("终止线程");
- myThread.setStop();
- }
- }
- }
- // 创建构造函数
public ThreadDemo() {
this.setSize(400, 150);// 设置窗体的大小
this.setLocationRelativeTo(null);// 设置窗体的初始位置,此为绝对据中
this.setResizable(false);// 设置窗体是否能被更改大小
this.setLayout(null);// 窗体不使用任何的布局方式
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设置窗体的关闭方式
this.setVisible(true);// 显示窗体
this.init();// 加载其他组建
this.initListener();// 加载监听事件
}
// 初始化其他组建
public void init() {
progress = new JProgressBar();
toolBar = new JToolBar();
toolBar.add(new JLabel("state"));
toolBar.add(progress);
toolBar.setFloatable(false);
toolBar.setBounds(15, 80, 350, 21);
btnStart = new JButton("start");
btnStart.setBounds(12, 10, 91, 21);
btnPause = new JButton("Pause");
btnPause.setBounds(131, 10, 91, 21);
btnStop = new JButton("stop");
btnStop.setBounds(241, 10, 91, 21);
this.getContentPane().add(btnStart);
this.getContentPane().add(btnPause);
this.getContentPane().add(btnStop);
this.getContentPane().add(toolBar);
}
// 初始化监听事件
public void initListener() {
this.btnStart.addActionListener(this);
this.btnPause.addActionListener(this);
this.btnStop.addActionListener(this);
}
public static void main(String[] args) {
new ThreadDemo();
}
class MyThread extends Thread {
private boolean flag = false;// 设置线程的标志位
private boolean pause = false;// 设置线程的暂停位
// 重写线程的启动方法
public void setStart() {
this.flag = true;
super.start();
}
// 重写线程的运行方法
public void run() {
int value = 0;
while (flag) {
try {
if (pause) {
Thread.sleep(50);
continue;
}
progress.setValue(value++);
if (progress.getValue() == progress.getMaximum()) {
break;
}
Thread.sleep(50);
} catch (Exception e) {
e.printStackTrace();
}
}
}
// 暂停线程
public void setPause() {
this.pause = !pause;
}
// 关闭线程
public void setStop() {
progress.setValue(0);
this.flag = false;
}
}
// 重写监听事件
public void actionPerformed(ActionEvent e) {
// 开始事件
if (e.getSource() == this.btnStart) {
System.out.println("启动线程");
myThread = new MyThread();
myThread.setStart();
}
// 暂停事件
if (e.getSource() == this.btnPause) {
System.out.println("暂停线程");
myThread.setPause();
}
// 停止事件
if (e.getSource() == this.btnStop) {
System.out.println("终止线程");
myThread.setStop();
}
}
}
// 初始化其他组建
public void init() {
progress = new JProgressBar();
toolBar = new JToolBar();
toolBar.add(new JLabel("state"));
toolBar.add(progress);
toolBar.setFloatable(false);
toolBar.setBounds(15, 80, 350, 21);
btnStart = new JButton("start");
btnStart.setBounds(12, 10, 91, 21);
btnPause = new JButton("Pause");
btnPause.setBounds(131, 10, 91, 21);
btnStop = new JButton("stop");
btnStop.setBounds(241, 10, 91, 21);
this.getContentPane().add(btnStart);
this.getContentPane().add(btnPause);
this.getContentPane().add(btnStop);
this.getContentPane().add(toolBar);
}
// 初始化监听事件
public void initListener() {
this.btnStart.addActionListener(this);
this.btnPause.addActionListener(this);
this.btnStop.addActionListener(this);
}
public static void main(String[] args) {
new ThreadDemo();
}
class MyThread extends Thread {
private boolean flag = false;// 设置线程的标志位
private boolean pause = false;// 设置线程的暂停位
// 重写线程的启动方法
public void setStart() {
this.flag = true;
super.start();
}
// 重写线程的运行方法
public void run() {
int value = 0;
while (flag) {
try {
if (pause) {
Thread.sleep(50);
continue;
}
progress.setValue(value++);
if (progress.getValue() == progress.getMaximum()) {
break;
}
Thread.sleep(50);
} catch (Exception e) {
e.printStackTrace();
}
}
}
// 暂停线程
public void setPause() {
this.pause = !pause;
}
// 关闭线程
public void setStop() {
progress.setValue(0);
this.flag = false;
}
}
// 重写监听事件
public void actionPerformed(ActionEvent e) {
// 开始事件
if (e.getSource() == this.btnStart) {
System.out.println("启动线程");
myThread = new MyThread();
myThread.setStart();
}
// 暂停事件
if (e.getSource() == this.btnPause) {
System.out.println("暂停线程");
myThread.setPause();
}
// 停止事件
if (e.getSource() == this.btnStop) {
System.out.println("终止线程");
myThread.setStop();
}
}
}
// 初始化监听事件
public void initListener() {
this.btnStart.addActionListener(this);
this.btnPause.addActionListener(this);
this.btnStop.addActionListener(this);
}
public static void main(String[] args) {
new ThreadDemo();
}
class MyThread extends Thread {
private boolean flag = false;// 设置线程的标志位
private boolean pause = false;// 设置线程的暂停位
// 重写线程的启动方法
public void setStart() {
this.flag = true;
super.start();
}
// 重写线程的运行方法
public void run() {
int value = 0;
while (flag) {
try {
if (pause) {
Thread.sleep(50);
continue;
}
progress.setValue(value++);
if (progress.getValue() == progress.getMaximum()) {
break;
}
Thread.sleep(50);
} catch (Exception e) {
e.printStackTrace();
}
}
}
// 暂停线程
public void setPause() {
this.pause = !pause;
}
// 关闭线程
public void setStop() {
progress.setValue(0);
this.flag = false;
}
}
// 重写监听事件
public void actionPerformed(ActionEvent e) {
// 开始事件
if (e.getSource() == this.btnStart) {
System.out.println("启动线程");
myThread = new MyThread();
myThread.setStart();
}
// 暂停事件
if (e.getSource() == this.btnPause) {
System.out.println("暂停线程");
myThread.setPause();
}
// 停止事件
if (e.getSource() == this.btnStop) {
System.out.println("终止线程");
myThread.setStop();
}
}
}
// 初始化监听事件
public void initListener() {
this.btnStart.addActionListener(this);
this.btnPause.addActionListener(this);
this.btnStop.addActionListener(this);
}
public static void main(String[] args) {
new ThreadDemo();
}
class MyThread extends Thread {
private boolean flag = false;// 设置线程的标志位
private boolean pause = false;// 设置线程的暂停位
// 重写线程的启动方法
public void setStart() {
this.flag = true;
super.start();
}
// 重写线程的运行方法
public void run() {
int value = 0;
while (flag) {
try {
if (pause) {
Thread.sleep(50);
continue;
}
progress.setValue(value++);
if (progress.getValue() == progress.getMaximum()) {
break;
}
Thread.sleep(50);
} catch (Exception e) {
e.printStackTrace();
}
}
}
// 暂停线程
public void setPause() {
this.pause = !pause;
}
// 关闭线程
public void setStop() {
progress.setValue(0);
this.flag = false;
}
}
// 重写监听事件
public void actionPerformed(ActionEvent e) {
// 开始事件
if (e.getSource() == this.btnStart) {
System.out.println("启动线程");
myThread = new MyThread();
myThread.setStart();
}
// 暂停事件
if (e.getSource() == this.btnPause) {
System.out.println("暂停线程");
myThread.setPause();
}
// 停止事件
if (e.getSource() == this.btnStop) {
System.out.println("终止线程");
myThread.setStop();
}
}
}
public static void main(String[] args) {
new ThreadDemo();
}
class MyThread extends Thread {
private boolean flag = false;// 设置线程的标志位
private boolean pause = false;// 设置线程的暂停位
// 重写线程的启动方法
public void setStart() {
this.flag = true;
super.start();
}
// 重写线程的运行方法
public void run() {
int value = 0;
while (flag) {
try {
if (pause) {
Thread.sleep(50);
continue;
}
progress.setValue(value++);
if (progress.getValue() == progress.getMaximum()) {
break;
}
Thread.sleep(50);
} catch (Exception e) {
e.printStackTrace();
}
}
}
// 暂停线程
public void setPause() {
this.pause = !pause;
}
// 关闭线程
public void setStop() {
progress.setValue(0);
this.flag = false;
}
}
// 重写监听事件
public void actionPerformed(ActionEvent e) {
// 开始事件
if (e.getSource() == this.btnStart) {
System.out.println("启动线程");
myThread = new MyThread();
myThread.setStart();
}
// 暂停事件
if (e.getSource() == this.btnPause) {
System.out.println("暂停线程");
myThread.setPause();
}
// 停止事件
if (e.getSource() == this.btnStop) {
System.out.println("终止线程");
myThread.setStop();
}
}
}
class MyThread extends Thread {
private boolean flag = false;// 设置线程的标志位
private boolean pause = false;// 设置线程的暂停位
// 重写线程的启动方法
public void setStart() {
this.flag = true;
super.start();
}
// 重写线程的运行方法
public void run() {
int value = 0;
while (flag) {
try {
if (pause) {
Thread.sleep(50);
continue;
}
progress.setValue(value++);
if (progress.getValue() == progress.getMaximum()) {
break;
}
Thread.sleep(50);
} catch (Exception e) {
e.printStackTrace();
}
}
}
// 暂停线程
public void setPause() {
this.pause = !pause;
}
// 关闭线程
public void setStop() {
progress.setValue(0);
this.flag = false;
}
}
// 重写监听事件
public void actionPerformed(ActionEvent e) {
// 开始事件
if (e.getSource() == this.btnStart) {
System.out.println("启动线程");
myThread = new MyThread();
myThread.setStart();
}
// 暂停事件
if (e.getSource() == this.btnPause) {
System.out.println("暂停线程");
myThread.setPause();
}
// 停止事件
if (e.getSource() == this.btnStop) {
System.out.println("终止线程");
myThread.setStop();
}
}
}
// 重写线程的启动方法
public void setStart() {
this.flag = true;
super.start();
}
// 重写线程的运行方法
public void run() {
int value = 0;
while (flag) {
try {
if (pause) {
Thread.sleep(50);
continue;
}
progress.setValue(value++);
if (progress.getValue() == progress.getMaximum()) {
break;
}
Thread.sleep(50);
} catch (Exception e) {
e.printStackTrace();
}
}
}
// 暂停线程
public void setPause() {
this.pause = !pause;
}
// 关闭线程
public void setStop() {
progress.setValue(0);
this.flag = false;
}
}
// 重写监听事件
public void actionPerformed(ActionEvent e) {
// 开始事件
if (e.getSource() == this.btnStart) {
System.out.println("启动线程");
myThread = new MyThread();
myThread.setStart();
}
// 暂停事件
if (e.getSource() == this.btnPause) {
System.out.println("暂停线程");
myThread.setPause();
}
// 停止事件
if (e.getSource() == this.btnStop) {
System.out.println("终止线程");
myThread.setStop();
}
}
}
// 重写线程的运行方法
public void run() {
int value = 0;
while (flag) {
try {
if (pause) {
Thread.sleep(50);
continue;
}
progress.setValue(value++);
if (progress.getValue() == progress.getMaximum()) {
break;
}
Thread.sleep(50);
} catch (Exception e) {
e.printStackTrace();
}
}
}
// 暂停线程
public void setPause() {
this.pause = !pause;
}
// 关闭线程
public void setStop() {
progress.setValue(0);
this.flag = false;
}
}
// 重写监听事件
public void actionPerformed(ActionEvent e) {
// 开始事件
if (e.getSource() == this.btnStart) {
System.out.println("启动线程");
myThread = new MyThread();
myThread.setStart();
}
// 暂停事件
if (e.getSource() == this.btnPause) {
System.out.println("暂停线程");
myThread.setPause();
}
// 停止事件
if (e.getSource() == this.btnStop) {
System.out.println("终止线程");
myThread.setStop();
}
}
}
// 暂停线程
public void setPause() {
this.pause = !pause;
}
// 关闭线程
public void setStop() {
progress.setValue(0);
this.flag = false;
}
}
// 重写监听事件
public void actionPerformed(ActionEvent e) {
// 开始事件
if (e.getSource() == this.btnStart) {
System.out.println("启动线程");
myThread = new MyThread();
myThread.setStart();
}
// 暂停事件
if (e.getSource() == this.btnPause) {
System.out.println("暂停线程");
myThread.setPause();
}
// 停止事件
if (e.getSource() == this.btnStop) {
System.out.println("终止线程");
myThread.setStop();
}
}
}
// 暂停线程
public void setPause() {
this.pause = !pause;
}
// 关闭线程
public void setStop() {
progress.setValue(0);
this.flag = false;
}
}
// 重写监听事件
public void actionPerformed(ActionEvent e) {
// 开始事件
if (e.getSource() == this.btnStart) {
System.out.println("启动线程");
myThread = new MyThread();
myThread.setStart();
}
// 暂停事件
if (e.getSource() == this.btnPause) {
System.out.println("暂停线程");
myThread.setPause();
}
// 停止事件
if (e.getSource() == this.btnStop) {
System.out.println("终止线程");
myThread.setStop();
}
}
}
// 关闭线程
public void setStop() {
progress.setValue(0);
this.flag = false;
}
}
// 重写监听事件
public void actionPerformed(ActionEvent e) {
// 开始事件
if (e.getSource() == this.btnStart) {
System.out.println("启动线程");
myThread = new MyThread();
myThread.setStart();
}
// 暂停事件
if (e.getSource() == this.btnPause) {
System.out.println("暂停线程");
myThread.setPause();
}
// 停止事件
if (e.getSource() == this.btnStop) {
System.out.println("终止线程");
myThread.setStop();
}
}
}