我已经编写了一个无限循环[while(true)],如果我在无限循环之后直接放置和if语句,则if语句将不起作用。 但是,当我在无限循环和if语句之间放置一个sysout时。
此代码无效:
public class dialogPrgBar extends JDialog {
//GUI Elemente
private JPanel content;
private JLabel label_anzeige;
private JProgressBar prgBar;
globalComponents global = new globalComponents();
private static final long serialVersionUID = 1L;
public dialogPrgBar(String title, String message) {
setTitle(title);
setIconImage(Toolkit.getDefaultToolkit().getImage(icon));
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
setSize(width, 150);
setResizable(false);
initComponents(message);
addComponents();
setLocationRelativeTo(null);
checkPrgBar_start();
}
private void initComponents(String message) {
//Layouts
GridLayout layout_content = new GridLayout(5, 1);
EmptyBorder margin = new EmptyBorder(5, 5, 5, 5);
Color background = Color.WHITE;
//Content Panel
content = new JPanel();
content.setLayout(layout_content);
content.setBackground(background);
content.setBorder(margin);
//Label - Anzeige
label_anzeige = new JLabel(message);
label_anzeige.setHorizontalAlignment(SwingConstants.CENTER);
//ProgressBar
prgBar = new JProgressBar(0, 100);
prgBar.setValue(0);
prgBar.setStringPainted(true);
}
private void addComponents() {
setContentPane(content);
content.add(new Label());
content.add(label_anzeige);
content.add(prgBar);
}
private void checkPrgBar_start() {
new Thread(new Runnable() {
@Override
public void run() {
while(true) {
if(startPrgBar) {
startPrgBar = false;
startPrgBar();
}
}
}
}).start();
}
private void startPrgBar() {
for(int i = 0; i < prgBar.getMaximum(); i++) {
prgBar.setValue(i);
try {
Thread.sleep(50);
} catch (InterruptedException e) {}
}
gui.control.setVisible(true);
gui.settings.setVisible(false);
gui.setup.setVisible(false);
dispose();
}
}
现在,当我添加System.out.println(“”);在无穷循环之后,可以在功能checkPrgBar_start()中使用它。