package bar;import java.awt.FlowLayout;   
import java.awt.event.ActionEvent;   
import java.awt.event.ActionListener;   
import javax.swing.JButton;   
import javax.swing.JFrame;   
import javax.swing.JOptionPane;
import javax.swing.JProgressBar;   
import javax.swing.JTextField;   
import javax.swing.SwingUtilities;   
public class SwingThreadTest3 extends JFrame {   
    private static final long serialVersionUID = 1L;   
    private static final String STR = "Completed : ";   
    private JProgressBar progressBar = new JProgressBar();   
    private JTextField text = new JTextField(10);   
    private JButton start = new JButton("Start");   
    private JButton end = new JButton("End");   
    private boolean flag = false;   
    private int count = 0;   
       
    private GoThread t = null;   
       
    private Runnable run = null;//更新组件的线程   
    public SwingThreadTest3() {   
        this.setLayout(new FlowLayout());   
        add(progressBar);   
        text.setEditable(false);   
        add(text);   
        add(start);   
        add(end);   
        start.addActionListener(new Start());   
        end.addActionListener(new End());   
           
        run = new Runnable(){//实例化更新组件的线程   
            public void run() {   
                progressBar.setValue(count);   
                text.setText(STR + String.valueOf(count) + "%");   
            }   
        };   
    }   
    private void go() {   
        while (count < 100) {   
            try {   
                Thread.sleep(100);   
            } catch (InterruptedException e) {   
                e.printStackTrace();   
            }   
            if (flag) {   
                count++;   
                SwingUtilities.invokeLater(run);//将对象排到事件派发线程的队列中   
            }   
        }   
    }   
    private class Start implements ActionListener {   
        public void actionPerformed(ActionEvent e) {   
            flag = true;   
            if(t == null){   
                t = new GoThread();   
                t.start();   
            } 
            JOptionPane.showMessageDialog(null, "ok!"); 
        }   
    }   
       
    class GoThread extends Thread{   
        public void run() {   
            //do something...               go();   
        }   
    }   
    private class End implements ActionListener {   
        public void actionPerformed(ActionEvent e) {   
            flag = false;   
        }   
    }   
    public static void main(String[] args) {   
        SwingThreadTest3 fg = new SwingThreadTest3();   
        fg.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
        fg.setSize(300, 100);   
        fg.setVisible(true);   
    }   
}  请问我怎么才能让进度条执行完后才显示对话框JOptionPane.showMessageDialog(null, "ok!"); ?谢谢!

解决方案 »

  1.   

    将此句JOptionPane.showMessageDialog(null, "ok!");移到go方法中的末行
      

  2.   

    看代码:run = new Runnable(){//实例化更新组件的线程   
                public void run() {   
                    progressBar.setValue(count);   
                    text.setText(STR + String.valueOf(count) + "%");  
                    if(count == 100){
                     JOptionPane.showMessageDialog(null, "ok!"); 
                    }
                }   
            };  
     
    在那里加上一个if判断即可。
      

  3.   

    楼上正解~~package No1;import java.awt.FlowLayout;   
    import java.awt.event.ActionEvent;   
    import java.awt.event.ActionListener;   
    import javax.swing.JButton;   
    import javax.swing.JFrame;   
    import javax.swing.JOptionPane;
    import javax.swing.JProgressBar;   
    import javax.swing.JTextField;   
    import javax.swing.SwingUtilities;   
    public class Test extends JFrame {   
      private static final long serialVersionUID = 1L;   
      private static final String STR = "Completed : ";   
      private JProgressBar progressBar = new JProgressBar();   
      private JTextField text = new JTextField(10);   
      private JButton start = new JButton("Start");   
      private JButton end = new JButton("End");   
      private boolean flag = false;   
      private int count = 0;   
        
      private GoThread t = null;   
        
      private Runnable run = null;//更新组件的线程   
      public Test() {   
      this.setLayout(new FlowLayout());   
      add(progressBar);   
      text.setEditable(false);   
      add(text);   
      add(start);   
      add(end);   
      start.addActionListener(new Start());   
      end.addActionListener(new End());   
        
      run = new Runnable(){//实例化更新组件的线程   
      public void run() {   
      progressBar.setValue(count);   
      text.setText(STR + String.valueOf(count) + "%");  
      if(count == 100){
          JOptionPane.showMessageDialog(null, "ok!"); 
      }  }   
      };   
      }   
      private void go() {   
      while (count < 100) {   
      try {   
      Thread.sleep(100);   
      } catch (InterruptedException e) {   
      e.printStackTrace();   
      }   
      if (flag) {   
      count++;   
      SwingUtilities.invokeLater(run);//将对象排到事件派发线程的队列中   
      }   
      }   
      }   
      private class Start implements ActionListener {   
      public void actionPerformed(ActionEvent e) {   
      flag = true;   
      if(t == null){   
      t = new GoThread();   
      t.start();   
      }  
      //JOptionPane.showMessageDialog(null, "ok!");  
      }   
      }   
        
      class GoThread extends Thread{   
      public void run() {   
      //do something...     go();   
      }   
      }   
      private class End implements ActionListener {   
      public void actionPerformed(ActionEvent e) {   
      flag = false;   
      }   
      }   
      public static void main(String[] args) {   
      Test fg = new Test();   
      fg.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
      fg.setSize(300, 100);   
      fg.setVisible(true);   
      }   
    }