有时候数据太多了。想做一个小面板,上面一个进度条,数据加载中
加载完了关闭。不知道怎么实现啊~~

解决方案 »

  1.   

    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JDialog;
    import javax.swing.JFrame;
    import javax.swing.JProgressBar;public class TestMain extends JFrame {
        private JButton button = null;
        private ProgressDialog dialog = null;
        
        public TestMain(){
            super("Test");
            button = new JButton("Start");
            button.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    dialog = new ProgressDialog();
                }
            });
            this.getContentPane().setLayout(new FlowLayout());
            this.getContentPane().add(button);
            this.setSize(300, 200);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setVisible(true);
        }
        
        public static void main(String [] args){
            new TestMain();
        }
        
        class ProgressDialog extends JDialog implements Runnable {
            private JProgressBar pb = null;
            private Thread thread = null;
            public ProgressDialog(){
                super(TestMain.this, "Loading...", true);
                thread = new Thread(this);
                pb = new JProgressBar();
                pb.setMaximum(100);
                this.getContentPane().setLayout(new FlowLayout());
                this.getContentPane().add(pb);
                this.setSize(200, 65);
                this.setLocationRelativeTo(TestMain.this);
                thread.start();
                this.setVisible(true);
            }
            public void run(){
                pb.setValue(0);
                for(int i = 0; i < 100; i++){
                    pb.setValue(i + 1);
                    System.out.println(i);
                    try {
                        Thread.sleep(20);
                    } catch (InterruptedException ex) {
                        ex.printStackTrace();
                    }
                }
            }
        }
        
    }