我正在尝试实现一个全局加载对话框...我想调用一些静态函数来显示对话框和一些静态函数来关闭它。与此同时,我正在主线程或子线程中做一些工作......
我尝试关注,但对话框没有更新......最后一次,在再次隐藏之前,它更新......
private static Runnable getLoadingRunable()
{
if (loadingRunnable != null)
{
loadingFrame.setLocationRelativeTo(null);
loadingFrame.setVisible(true);
return loadingRunnable;
}
loadingFrame = new JFrame("Updating...");
final JProgressBar progressBar = new JProgressBar();
progressBar.setIndeterminate(true);
final JPanel contentPane = new JPanel();
contentPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
contentPane.setLayout(new BorderLayout());
contentPane.add(new JLabel("Updating..."), BorderLayout.NORTH);
contentPane.add(progressBar, BorderLayout.CENTER);
loadingFrame.setContentPane(contentPane);
loadingFrame.pack();
loadingFrame.setLocationRelativeTo(null);
loadingFrame.setVisible(true);
loadingRunnable = new Runnable() {
public void run() {
try {
while (running) {
Thread.sleep(100);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
SwingUtilities.invokeLater(new Runnable() {
public void run() {
loadingFrame.setVisible(false);
}
});
}
};
return loadingRunnable;
}
public static void showLoadingBar() {
System.out.println("showLoadingBar");
running = true;
threadLoadingBar = new Thread(getLoadingRunable());
threadLoadingBar.start();
}
public static void hideLoadingBar() {
System.out.println("hideLoadingBar");
running = false;
threadLoadingBar = null;
}