“`
package com.js;
import java.awt.*;
import javax.swing.*;
public class Java_4_Thread_InterruptedSwing extends JFrame {
Thread thread;
public static void main(String[] args) {
// TODO 自动生成的方法存根
init(new Java_4_Thread_InterruptedSwing(),400,100);
}
public Java_4_Thread_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(10000);// 使线程休眠 1000毫秒
// 捕捉 InterruptedException 异常
}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);
}
}
“`