因为文件读取属于IO操作,线程必须等待其完成后才能执行!所以,你的进度条启动不了!
解决方法:SwingUtilities.invokeLater()。

解决方案 »

  1.   

    线程中不能操作任何Swing组件,因此需要调用EventQueue的invokeLater()方法,事件一旦进入事件队列,invokeLater()方法立即返回,run()方法异步执行!
      

  2.   

    我和程序如下:请指点: class UpdateThread extends Thread{
       Runnable update,finish;
       int value,min,max,increment;
       public UpdateThread(){
         max=pb.getMaximum();
         min=pb.getMinimum();
         update=new Runnable(){
           public void run(){
            value=pb.getValue()+increment;
            updateprogressBar(value);       }
         };
         finish=new Runnable(){
           public void run(){
             updateprogressBar(min);
           }
         };
       }
       public void run(){
         map.readMfyFile("beijing.mfo");//自己的文件格式
     //  jButton1.setEnabled(false);
        while(value+increment<=max)
       {     simnulateTimeConsumingActivity();
         SwingUtilities.invokeLater(update);
       }
       SwingUtilities.invokeLater(finish);
     //  jButton1.setEnabled(true);
       //pb.setVisible(false);
     }
     public void updateprogressBar(int value){
       pb.setValue(value);
     }
     private void simnulateTimeConsumingActivity(){
       try{
         Thread.currentThread().sleep(1000);
         increment=(max-min)/10;
       }catch(InterruptedException e){
         e.printStackTrace();
       }
     }
      }
      

  3.   

    参考以下文章:标题:用JAVA实现线程等待提示框
    本文将用Java的多线程特性来实现线程等待提示框。 
    1、问题的提出 
    在Java应用程序编程中,有时需要在GUI(图形化用户界面)中处理一些占用系统资源较多,耗费时间较长的事务,例如:与数据库进行大批量数据交换、大数据量的复杂运算、远程连接服务器等等。系统在处理这些事务时,如果还是使用GUI所在的线程,会导致界面冻结,无法刷新,看起来好象系统已经崩溃,这是一个良好的软件系统不允许出现的局面。 
    2、解决问题的途径 
    解决上述问题的方法就是采用Java的多线程特性,为这些耗时又耗资源的事务再开一个线程单独运行,并在GUI处出现提示框“正在执行,请等待”,在线程结束时自动关闭该提示框。这样即避免了上面出现的界面冻结情况,又保证了线程的安全性,是软件开发者上佳的选择。 
    3、具体实现 
    (1)例子 
    这里举一个简单的例子来介绍如何用JAVA实现线程等待提示框。 
    此例实现一个很简单的GUI,根窗体testFrame是一个JFrame(框架)类,在testFrame中放置一个JPanel(面板):testPanel ,最后将一个JButton(按钮):testButton添加到testPanel中。 
    按下testButton,系统开始运行一个模拟的耗时又耗资源的事务:在标准输出设备上显示从1到100000,同时出现“线程正在运行”提示框,一旦事务完成(即线程结束),系统自动关闭该提示框。 
    (2)实现方法 
    为了达到上述功能,可以这样来实现: 
    当按下按钮后,启动一个新的线程来完成事务,即在标准输出设备上显示从1到100000(在代码中通过TestThread类来实现),紧接着再启动一个线程来显示“线程正在运行”提示框(在代码中通过ThreadDiag类来实现)。 
    为了使提示框在TestThread结束后,自行关闭,在TestThread启动后,还启动了一个DisposeDiag线程,这个线程专门用来等待TestThread线程结束后,关闭“线程正在运行”提示框。 
    (3)程序代码及注释 
    ① TestFrame类 
    TestFrame是Java运行主程序,用来显示用户界面。 import javax.swing.*; 
      import java.awt.*; 
      import java.awt.event.*; 
      public class TestFrame extends JFrame 
      { 
       //GUI所需组件 
       public JPanel testPanel = null; 
       public JButton testButton = null; 
       public JFrame testFrame = null; 
       public TestFrame() 
       { 
        //设置GUI为windows风格 
        try 
        { 
         UIManager.setLookAndFeel( 
         "com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); 
        } 
        catch (Exception ex)  
        { 
         System.out.println(“Exception: ” + ex); 
        } 
        testFrame = this; 
        // 初始化GUI 
        Dimension dimensions = Toolkit.getDefaultToolkit().getScreenSize(); 
        setSize(dimensions.width /2, dimensions.height /2); 
          setLocation(dimensions.width/2-dimensions.width/4,  
          dimensions.height/2-dimensions.height/4); 
        testPanel = new JPanel(); 
        testButton = new JButton("开始线程"); 
        testPanel.add(testButton); 
        getContentPane().add(testPanel); 
        //增加按钮testButton事件监听器 
        testButton.addActionListener(new java.awt.event.ActionListener() { 
        public void actionPerformed(ActionEvent e) { 
        TestThread testThread = new TestThread();//新生成一个处理事务线程 
        testThread.start();//启动事务线程 
        (new ThreadDiag(testFrame, testThread ,  
         "正在执行,请等待......")).start();//启动等待提示框线程 
        } 
       }); 
         //增加testFrame事件监听器 
       addWindowListener(new WindowAdapter() 
       { 
        public void windowClosing(WindowEvent e) { 
        System.exit(0); 
         } 
       }); 
      } 
       public static void main(String[] args)  
       { 
        //主程序 
        TestFrame testFrame2 = new TestFrame(); 
        testFrame2.setTitle("线程等待测试"); 
        testFrame2.show(); 
       } 
      } 
    ② TestThread类 
    TestThread类是处理事务线程,即在标准输出设备上显示从1到100000。 
    public class TestThread extends Thread 
     { 
      public void run() 
      { 
       for (int i = 1; i < 100000 ; i++ ) 
       { 
        System.out.println(i); 
       }   
      } 
     } 
    ③ ThreadDiag类 
    ThreadDiag类用来显示“线程正在运行”提示框。 
    import java.awt.*; 
      import javax.swing.*; 
      public class ThreadDiag extends Thread 
      { 
       private Thread currentThread = null;//实际调用时就是TestThread事务处理线程 
       private String messages = "";//提示框的提示信息 
       private JFrame parentFrame = null;//提示框的父窗体 
       private JDialog clueDiag = null;// “线程正在运行”提示框 
       private Dimension dimensions = Toolkit.getDefaultToolkit().getScreenSize(); 
       private int width = dimensions.width / 4, height = 60; 
       private int left = 0, top = 0; 
       public ThreadDiag(JFrame parentFrame, Thread currentThread, String messages) 
       { 
        this.parentFrame = parentFrame; 
        this.currentThread = currentThread; 
        this.messages = messages; 
        initDiag();//初始化提示框 
       } 
       protected void initDiag() 
        { 
        clueDiag = new JDialog(parentFrame,"正在执行,请等待...",true); 
        clueDiag.setCursor(new Cursor(Cursor.WAIT_CURSOR)); 
        JPanel testPanel = new JPanel(); 
        JLabel testLabel = new JLabel(messages); 
        clueDiag.getContentPane().add(testPanel); 
        testPanel.add(testLabel); 
        (new DisposeDiag()).start();//启动关闭提示框线程 
       } 
      public void run() 
       { 
        //显示提示框 
        int left = (dimensions.width - width)/2; 
        int top = (dimensions.height - height)/2; 
        clueDiag.setSize(new Dimension(width,height)); 
        clueDiag.setLocation(left, top); 
        clueDiag.show(); 
       } 
      } 
    ④ DisposeDiag类 
    DisposeDiag类用来关闭提示框 
    class DisposeDiag extends Thread 

     public void run() 
     { 
     try 
     { 
      currentThread.join();//等待事务处理线程结束 
     } 
     catch(InterruptedException e) 
     { 
      System.out.println("Exception:" + e); 
     } 
     clueDiag.dispose();//关闭提示框 


    注:为了共用变量clueDiag,上述ThreadDiag类和DisposeDiag类放在同一个Java文件内,如果分开存放,只需传递一下参数即可。 
    上述程序在jdk1.3下运行通过。 
    (4)程序运行结果 
    运行结果如下图所示: 
    当事务执行完后,“正在执行”提示框自动关闭。
      

  4.   

    按照上面大侠们的解释,那么就不可能做到像OICQ那样,一边发送文件,一边做其他事情了??????????????????????
    难道必须等待文件发送完了才行吗???????????:(
      

  5.   

    针对你的问题,我做了个实验,希望能够对你有帮助
    import java.io.*;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    public class ProgressBarTest
    {
            public static void main(String[] args)
            {
                    ProgressFrame frame = new ProgressFrame();
                    frame.show();
            }
    }class ProgressFrame extends JFrame
    {
            JProgressBar pb;
            JTextArea ta;
            public ProgressFrame(){
                    setTitle("ProgressBar test");
                    setSize(500,300);
                    addWindowListener(new WindowAdapter(){
                            public void windowClosing(WindowEvent e){
                                    System.exit(0);
                            }
                    });
                    JPanel contentPane = (JPanel)getContentPane();
                    JPanel p = new JPanel();
                    ta = new JTextArea(10,20);
                    p.add(ta);
                    contentPane.add(p,"South");
                    p = new JPanel();
                    JButton b = new JButton("Read file");
                    b.addActionListener(new ActionListener(){
                            public void actionPerformed(ActionEvent e){
                                    new FileThread(pb,ta).start();
                            }
                    });
                    p.add(b);
                    contentPane.add(p,"North");
                    pb = new JProgressBar(0,100);
                    pb.setValue(0);
                    p.add(pb);
                    contentPane.add(p,"Center");
            }
    };
    class FileThread extends Thread
    {
            private JProgressBar pb;
            private JTextArea ta;
            private int value = 0;
            BufferedReader in;
            String ss="";
            String aa="";
            public FileThread(JProgressBar aPb,JTextArea aTa){
                    try
                    {
                            in = new BufferedReader(new FileReader("d:\\aa.txt"));
                    }
                    catch (Exception e)
                    {
                            System.out.println(e.getMessage());
                    }
                    pb=aPb;
                    ta=aTa;
                    pb.setValue(0);
            }
            public void run(){
                    try
                    {
                      while (true){
                            while ((aa=in.readLine())!=null)
                            {
                                    ss+=aa;
                                    value++;
                                    pb.setValue(value);
                            }
                            in.close();
                            ta.setText(ss);
                      }
                    }
                    catch (Exception e)
                    {
                            System.out.println(e.getMessage());
                    }        }
    };
      

  6.   

    http://expert.csdn.net/Expert/topic/1619/1619544.xml?temp=.5748712
    一个类似的问题
    最奇怪的是:
        发送端的applet界面像死了似的,直到发送完才有恢复到正常。
        但是接受文件的客户端却一点不受影响,applet界面一直正常。谁能随便帮解决一下????