当JProgressBar达到100%时,如何使JDialog不可见? [英] How to make the JDialog invisible when the JProgressBar reaches 100%?
问题描述
相关代码段:
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
方法:
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
方法下面的嵌套类:
Task
is a nested class just below the 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不会关闭.其实,我想在JProgressBar达到100%之后执行actionPerformed
中的注释代码.
I want the JDialog not to be visible when the JProgressBar in it reaches 100% . Currently, the JDialog does not close when the JProgressBar reaches 100%. Actually, I want to the commented piece of code in actionPerformed
to be executed after the JProgressBar reaches 100%.
我刚在task.start();
之后尝试了task.join();
,但这给出了负面的结果.当我这样做时,将显示JDialog的边框,然后,对话框关闭.我从没在JDialog中看到任何东西.
I've tried task.join();
just after task.start();
, but this gave a negative result. When I did this, the JDialog's border was displayed and then, after a moment, the dialog closes. I never see anything in the JDialog.
请注意,我是SwingUtilities
的新手.
我如何使程序达到预期的效果?
How do I make the program do what I am expecting?
推荐答案
怎么样:
public void run() {
if(j == 100)
dialog.dispose();
else
progress.setValue(j);
}
这篇关于当JProgressBar达到100%时,如何使JDialog不可见?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!