现在的问题是, 按 JButton 就会弹出一个 JFrame.
由于这个JFrame 有很多运算, 导致打开得很慢.
我希望按了JButton 先弹出 JProgressBar显示进度, 最后才弹出 JFrame..
如何知道JFrame 初始化进度, 然后将进度传回Main Frame 才可以更新进度.不知道如何实现?

解决方案 »

  1.   

    例子米有哇 基本的思路是 你Main里面先开一个线程 去启动你那个进度条
    大概这样
    public void propertyChange(PropertyChangeEvent evt) {
        if (!done) {
            int progress = task.getProgress();
            if (progress == 0) {
                progressBar.setIndeterminate(true);
                taskOutput.append("No progress yet\n");
            } else {
                progressBar.setIndeterminate(false); 
                progressBar.setString(null);
                progressBar.setValue(progress);
                taskOutput.append(String.format(
                        "Completed %d%% of task.\n", progress));
            }
        }
    }然后还有一个线程去执行你的load 你的GUI
    这个方法蛮简单  就是怕时间算不好  因为按时间来计算事件 是一个不好的方式
    比较好的方式是  你在jprogressbar的线程里面最后加一个flag 继续沿用上面的这种Indeterminate的方式
    然后在这个线程的最后抛出flag然后在GUI线程里面监听这个flag  jgrogressbar执行完毕(flag抛出)&&GUI loading完毕  然后你打开GUI
      

  2.   

    但是也不知道什么时候抛出flag?
    因为不知 newFrame 什么时候完成初始化.
      

  3.   

    那你就把GUI和LOGIC分离哇 
    GUI里面加LOGIC是很忌讳的  最好不要这样搞就像比如高级点的flash游戏  都是在c++或者java里面写逻辑  flash只管显示
    如果在flash里面加逻辑 不尽效率低慢卡 而且改起来不方便
      

  4.   

    还是想不通…高手指点.import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    public class Frame1 extends JFrame implements ActionListener {    JButton btn = new JButton("Open New");    public Frame1() {
            setLayout(new FlowLayout());        getContentPane().add(btn);
            btn.addActionListener(this);        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(200, 200);
        }    public void actionPerformed(ActionEvent e) {
            Object obj = e.getSource();
            Frame2 f = new Frame2();
            f.setSize(100, 100);
            f.setVisible(true);    }    public static void main(String[] args) {
            Frame1 f = new Frame1();
            f.setVisible(true);    }
    }import javax.swing.JFrame;
    public class Frame2 extends JFrame {    boolean flag;    public Frame2() {
            int i = 0;
            while (i < 10) {
                i++;
                System.out.println(i);
                try {
                    Thread.sleep(1000);
                } catch (Exception e) {
                }
            }
            flag = true;
        }
    }
      

  5.   

    /** 
    * @(#)ProgressTest.java 


    * @author 张栋芳QQ271536394 
    * @version 1.00 2008/3/13 
    */ import java.awt.*; 
    import java.awt.event.*; 
    import javax.swing.*; 
    import javax.swing.event.*; 
    import javax.swing.JProgressBar; 
    public class ProgressTest extends JDialog{ 
    public JProgressBar jpb; 
    public  int Max=100; 
    public  int Min=0; 
    public  int Count=1; 
    public JButton bStart; 
        public ProgressTest() { 
          
        
        Container con=(JPanel)this.getContentPane(); 
        con.setLayout(null); 
        bStart=new JButton("开始"); 
        bStart.setBounds(10,10,70,30); 
        bStart.addActionListener(new MyActionListener()); 
        
        jpb=new JProgressBar(); 
        jpb.setBounds(80,20,300,20); 
        jpb.setMaximum(Max); 
        jpb.setMinimum(Min); 
        jpb.setAutoscrolls(true); 
        jpb.setStringPainted(true);//进度字 
        //jpb.setValue(50); 
        con.add(bStart); 
        con.add(jpb); 
        this.setSize(400,300); 
        this.setTitle("进度条的小程序"); 
        this.setLocationRelativeTo(this); 
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        this.setVisible(true); 
        
        } 
        public static void main(String[] args){ 
        ProgressTest test=new ProgressTest(); 
        } 
        private class MyActionListener implements ActionListener{ 
        public void actionPerformed(ActionEvent e){ 
        if(e.getSource().equals(bStart)){ 
        System.out.println ("ok"); 
            UpThread thread=new UpThread(); 
        thread.start(); 
        } 
        } 
        } 
        
    class UpThread extends Thread{ 
    public UpThread(){ } 
        public void run(){ 
        Count=Min; 
        while(Count <=Max){ 
        Count++; 
            jpb.setValue(Count); 
        try{ 
        if(Count <40){ 
        this.sleep(50); 
        } 
        if(Count>=40&&Count <70){ 
        this.sleep(200); 
        } 
        if(Count>=70){ 
        this.sleep(50); 
        } 
        
        }catch(Exception ae){ 
        ae.printStackTrace(); 
        } 
        } 
        
        
        } 
    } } 
      

  6.   

    第一个有processbar的窗口加个public的方法,比如setValue(int value);来改变processbar的value,并setVisible(true),在另外一个真正要加载的窗口里,把第一个窗口的实例以参数传到第二个frame,每加载一步,调用一次setValue,加载完后第一个窗口setVisiblue(false)或者dispose,第二个setVisible(true);