JProgressBar进度条通过手动点击按钮显示空白,但是通过程序点击却正常。请大佬指教问题是出在哪?
图片如下,后附源码。
package tmp;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
@SuppressWarnings("serial")
public class ProgressTest extends JFrame {
public static void main(String[] args) throws Exception {
ProgressTest rf = new ProgressTest();
rf.setVisible(true);
// rf.button.doClick();
}
private JButton button;
private JTextField textField;
private JDialog dialog;
private JProgressBar progressBar;
public ProgressTest() {
button = new JButton("开始");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText("");
dialog.setVisible(true);
for (int i = 0; i < 100; i++) {
progressBar.setValue(i);
try {
Thread.sleep(111);
} catch (InterruptedException e1) {
}
}
dialog.setVisible(false);
textField.setText("222222222222222222");
}
});
textField = new JTextField("111111111111111111");
getContentPane().add(BorderLayout.WEST, button);
getContentPane().add(BorderLayout.CENTER, textField);
progressBar = new JProgressBar();
progressBar.setStringPainted(true);
progressBar.setIndeterminate(false);
dialog = new JDialog(this, "进度条");
dialog.add(progressBar);
dialog.setSize(300, 75);
dialog.setLocationRelativeTo(this);
dialog.setDefaultCloseOperation(HIDE_ON_CLOSE);
setSize(600, 120);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}