当JProgressBar达到100%时如何使JDialog不可见?

2023-12-26

相关代码段:

JProgressBar progress;
JButton button;     
JDialog dialog;      //Fields of my GUI class

progress=new JProgressBar(JProgressBar.HORIZONTAL,0,100);
button=new JButton("Done");
dialog=new JDialog();             //Done from methods

progress.setValue(0);
progress.setStringPainted(true);
progress.setBorderPainted(true);  //Also done from methods

button.addActionListener(this);   //Also done from methods

dialog.setLayout(new FlowLayout(FlowLayout.CENTER));
dialog.setTitle("Please wait...");
dialog.setBounds(475,150,250,100);
dialog.setModal(true);            //Also done from methods

dialog.add(new JLabel("Loading..."));
dialog.add(progress);             //Also done from methods

这是actionPerformed method:

public void actionPerformed(ActionEvent e)
{
    dialog.setVisible(true);
    Task task=new Task();
    task.start();


    //After the JProgressBar reaches 100%, do the following things:
    /*progress.setValue(progress.getMinimum());
    dialog.setVisible(false);*/
}

Task是一个嵌套类,就在actionPerformed method:

private class Task extends Thread {    
  public void run(){
     for(int i =0; i<= 100; i++){
        final int j = i;
        SwingUtilities.invokeLater(new Runnable() {
           public void run() {
              progress.setValue(j);
           }
        });
        try {
           Thread.sleep(10);
        } catch (InterruptedException e) {}
     }
  }
} 

我希望当 JProgressBar 达到 100% 时 JDialog 不可见。目前,当 JProgressBar 达到 100% 时,JDialog 不会关闭。实际上,我想要注释的代码段actionPerformed在 JProgressBar 达到 100% 后执行。

我试过了task.join();刚过task.start();,但这给出了负面结果。当我这样做时,JDialog 的边框被显示,然后,过了一会儿,对话框关闭。我在 JDialog 中从未看到任何内容。

请注意,我是新手SwingUtilities.

如何让程序达到我的预期?


怎么样:

public void run() {
          if(j == 100)
              dialog.dispose();
          else
              progress.setValue(j);
       }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

当JProgressBar达到100%时如何使JDialog不可见? 的相关文章

随机推荐