package 测试JProgressBar;import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
public class TestProgressBar extends JApplet implements Runnable,ActionListener{
/**
 * 
 */
private static final long serialVersionUID = 1L;
Thread prothread;
JProgressBar progress;
public void init()
{   
setLayout(new BorderLayout());
final ImageIcon image=new ImageIcon("测试JProgressBar\\1.GIF");
    final JButton bt=new JButton("开始");
    final JLabel lb=new JLabel(image);
    progress=new  JProgressBar(0,100);
    progress.setStringPainted(true); 
add(bt, "North");
add(lb, "Center");
add(progress, "South");
bt.addActionListener(this);

}
public void actionPerformed( ActionEvent e) 

if(prothread==null||!prothread.isAlive())
{
prothread=new Thread("进度");

    prothread.start();
    System.out.print(prothread.isAlive());
    System.out.print(prothread.toString());
 
    }
    

}
@SuppressWarnings("static-access")
public void run() 
{           for(int i=0;i<=100;i++)
{
          progress.setForeground(Color.BLUE);
          System.out.print("反对公司的" );
 progress.setValue(progress.getValue()+1);
 progress.setString(progress.getValue() + "% ");

try {
Thread.sleep(100);
} catch (final InterruptedException e) {
// TODO 自动生成 catch 块
System.out.print(false);
}
}

}
}

解决方案 »

  1.   

    prothread=new Thread("进度"); new了一个线程
    prothread.start();执行上面那个线程而run()方法是TestProgressBar 的方法
    所以没有执行。
    建议将
    public void actionPerformed( ActionEvent e) 

    if(prothread==null||!prothread.isAlive()) 

    prothread=new Thread("进度");     prothread.start(); 
        System.out.print(prothread.isAlive()); 
        System.out.print(prothread.toString());     } 
        } 
    修改为public void actionPerformed( ActionEvent e) 

    if(prothread==null||!prothread.isAlive()) 

    prothread=new Thread("进度"){public void run() 
    {         for(int i=0;i <=100;i++) 

            progress.setForeground(Color.BLUE); 
            System.out.print("反对公司的" ); 
    progress.setValue(progress.getValue()+1); 
    progress.setString(progress.getValue() + "% "); try { 
    Thread.sleep(100); 
    } catch (final InterruptedException e) { 
    // TODO 自动生成 catch 块 
    System.out.print(false); 

    } } };     prothread.start(); 
        System.out.print(prothread.isAlive()); 
        System.out.print(prothread.toString());     } 
        } 将原有run()删除