相关代码段:
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
.
如何让程序达到我的预期?