我有使用 ProgressBar 的主文件。在主文件中,我将 ProgressBar 称为
ProgressBar pbFrame = new ProgressBar();
pbFrame.setVisible(true);
我在调用可执行文件“calculate.exe”后调用 ProgressBar 以显示 calculate.exe 现在正在工作。但是当“calculate.exe”完成时,ProgressBar 会打开。如何并行执行“calculate.exe”和ProgressBar?我听说过 SwingWorker,但我绝对不明白如何在我的应用程序中使用它。
我的进度条文件:
public class ProgressBar extends JFrame {
static private int BOR = 10;
private String filename;
public ProgressBar() {
super("Calculating progress");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createEmptyBorder(BOR, BOR, BOR, BOR));
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(new JLabel("Calculating..."));
panel.add(Box.createVerticalGlue());
JProgressBar progressBar1 = new JProgressBar();
progressBar1.setIndeterminate(true);
panel.add(progressBar1);
panel.add(Box.createVerticalGlue());
JPanel buttonsPanel = new JPanel();
buttonsPanel.setLayout(new BoxLayout(buttonsPanel, BoxLayout.Y_AXIS));
buttonsPanel.add(Box.createVerticalGlue());
JButton quitButton = new JButton("OK!");
quitButton.setHorizontalAlignment(JButton.CENTER);
quitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
dispose();
}
});
panel.add(quitButton);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(panel, BorderLayout.CENTER);
setPreferredSize(new Dimension(200, 110));
setLocationRelativeTo(null);
pack();
}
}
提前致谢!