簡體   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