import java.awt.event.*;
import javax.swing.*;
import java.awt.*; public class RunTest extends Thread{
public static void main(String[] args){
ProFrame f=new ProFrame();
f.show();
Thread t=new Thread(f);
t.start();
}
} class ProFrame extends JFrame implements Runnable{
public JProgressBar progressbar;
public ProFrame f1;//变量声名的位置
public Thread t1;
public ProFrame(){
this.setTitle("ProFrame");
this.setDefaultCloseOperation(3);
this.setSize(500,500); Container content=this.getContentPane();
content.setLayout(new FlowLayout()); progressbar=new JProgressBar();
JButton btn=new JButton("NewFrame");
btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
ProFrame f1=new ProFrame();
f1.show();
Thread t1=new Thread(f1);
t1.start();
}
}); content.add(progressbar);
content.add(btn);
}
public void run(){
for(int i=0;i<101;i++){
progressbar.setValue(i);
try {
t1.sleep(100);
}catch(InterruptedException e) {
System.err.println("Interrupted");
}
System.out.println(""+progressbar.getValue());
}
}
} 把你的程序稍微改动了一下,应该是你的要求吧
import javax.swing.*;
import java.awt.*; public class RunTest extends Thread{
public static void main(String[] args){
ProFrame f=new ProFrame();
f.show();
Thread t=new Thread(f);
t.start();
}
} class ProFrame extends JFrame implements Runnable{
public JProgressBar progressbar;
public ProFrame f1;//变量声名的位置
public Thread t1;
public ProFrame(){
this.setTitle("ProFrame");
this.setDefaultCloseOperation(3);
this.setSize(500,500); Container content=this.getContentPane();
content.setLayout(new FlowLayout()); progressbar=new JProgressBar();
JButton btn=new JButton("NewFrame");
btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
ProFrame f1=new ProFrame();
f1.show();
Thread t1=new Thread(f1);
t1.start();
}
}); content.add(progressbar);
content.add(btn);
}
public void run(){
for(int i=0;i<101;i++){
progressbar.setValue(i);
try {
t1.sleep(100);
}catch(InterruptedException e) {
System.err.println("Interrupted");
}
System.out.println(""+progressbar.getValue());
}
}
} 把你的程序稍微改动了一下,应该是你的要求吧
它比较复杂一点,上面的thread2需要被调用多次,如copy file等。
thread1监视文件的copy情况
注意sleep是thread类的静态方法。