当 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
方法下方的嵌套 class:
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) {}
}
}
}
我希望 JDialog 在其中的 JProgressBar 达到 100% 时不可见。目前,当 JProgressBar 达到 100% 时,JDialog 不会关闭。实际上,我想在 JProgressBar 达到 100% 后执行 actionPerformed
中的注释代码段。
我在 task.start();
之后尝试了 task.join();
,但结果是否定的。当我这样做时,显示了 JDialog 的边框,片刻之后,对话框关闭。我从未在 JDialog 中看到任何内容。
请注意,我是 SwingUtilities
的新手。
如何让程序按照我的预期进行?
怎么样:
public void run() {
if(j == 100)
dialog.dispose();
else
progress.setValue(j);
}
相关代码:
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
方法下方的嵌套 class:
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) {}
}
}
}
我希望 JDialog 在其中的 JProgressBar 达到 100% 时不可见。目前,当 JProgressBar 达到 100% 时,JDialog 不会关闭。实际上,我想在 JProgressBar 达到 100% 后执行 actionPerformed
中的注释代码段。
我在 task.start();
之后尝试了 task.join();
,但结果是否定的。当我这样做时,显示了 JDialog 的边框,片刻之后,对话框关闭。我从未在 JDialog 中看到任何内容。
请注意,我是 SwingUtilities
的新手。
如何让程序按照我的预期进行?
怎么样:
public void run() {
if(j == 100)
dialog.dispose();
else
progress.setValue(j);
}