繁体   English   中英

JProgressBar在阅读期间不可见

[英]JProgressBar not visible during reading

我有一个完美加载的java进度条,但我看不到进程,只看到了结果。 (当栏完成装载时)我想看到进度的每个百分比。 当我运行代码时,框架会出现,但进度条不会出现,只有当它处于100%时。 哪里有问题?

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         

       JFrame f = new JFrame("JProgressBar Sample");
       f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       Container content = f.getContentPane();
       progressBar = new JProgressBar();
       progressBar.setStringPainted(true);
       Border border = BorderFactory.createTitledBorder("Reading...");
       progressBar.setBorder(border);
       content.add(progressBar, BorderLayout.CENTER);
       f.setSize(300, 100);
       f.setVisible(true);

       progressBar.setValue(0);
       inc(); //fill the bar
       //It fills, but I can't se the whole loading...

    }                                        
        //Here's the filling path
    public static void inc(){

        int i=0;
       try{
            while (i<=100){    
            progressBar.setValue(i+10);

            Thread.sleep(1000);
            i+=20;
            }
        }catch(Exception ex){
            //nothing
        }
    }

当您在GUI的线程中运行长进程时,您的GUI不会更新,例如填充进度条并暂停几秒钟。

刚出现一个线程,它将处理长时间的操作。 在此线程中,将进度条设置为SwingUtilities.invokeLater(new Runnable() {...}的所需值。

Runnable r = new Runnable() {
    public void run() {
        int i=0;
        try{
            while (i<=100){    
                final int tmpI = i+10;
                SwingUtilities.invokeLater(new Runnable(){ 
                    public void run() {
                        progressBar.setValue(tmpI);
                    }
                });
                Thread.sleep(1000);
                i += 20;
            }
        } catch(Exception ex){
             //nothing
        }
    }
};
Thread t = new Thread(r);
t.start();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM