这是我的启动画面代码,
public class SplashScreen extends JWindow {
private static final long serialVersionUID = 1L;
private BorderLayout borderLayout = new BorderLayout();
private JLabel imageLabel = new JLabel();
private JProgressBar progressBar = new JProgressBar(0, 100);
public SplashScreen(ImageIcon imageIcon) {
imageLabel.setIcon(imageIcon);
setLayout(borderLayout);
add(imageLabel, BorderLayout.CENTER);
add(progressBar, BorderLayout.SOUTH);
pack();
setLocationRelativeTo(null);
}
public void showScreen() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
setVisible(true);
}
});
}
public void close() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
setVisible(false);
dispose();
}
});
}
public void setProgress(final String message, final int progress) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
progressBar.setValue(progress);
if (message == null) {
progressBar.setStringPainted(false);
} else {
progressBar.setStringPainted(true);
}
progressBar.setString("Loading " + message + "...");
}
});
}
}
从我这样调用的主要方法中,
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel(UIManager
.getSystemLookAndFeelClassName());
SplashScreen splashScreen = new SplashScreen(new ImageIcon("images/splash.jpg"));
splashScreen.showScreen();
AppFrame frame = new AppFrame(splashScreen);
} catch (Exception e) {
appLogger.error(e.getMessage(), e);
}
}
});
}
在我调用的 AppFrame 的构造函数中, splashScreen.setProgress(msg, val) 方法来更新进度条。但飞溅没有显示。它仅在最后显示帧仅显示几分之一秒,即使加载需要很长时间。但是如果我把这三行
SplashScreen splashScreen = new SplashScreen(new ImageIcon("images/splash.jpg"));
splashScreen.showScreen();
AppFrame frame = new AppFrame(splashScreen);
在 invokeLater() 之外,会显示初始屏幕,并且进度条会很好地更新。我相信 GUI 更新应该在 invokeLater 中。可能是什么问题呢?
顺便说一句,AppFrame 加载了我的应用程序的各种面板。
编辑: 我的 AppFrame 的模拟如下所示。
public class AppFrame extends JFrame {
public AppFrame(SplashScreen splashScreen) {
JPanel test = new JPanel();
test.setLayout(new GridLayout(0, 10));
splashScreen.setProgress("jlabel", 10);
for(int i = 0; i < 10000; i++) {
test.add(new JButton("Hi..." + i));
splashScreen.setProgress("jbutton", (int)(i * 0.1));
}
add(new JScrollPane(test));
setPreferredSize(new Dimension(800, 600));
pack();
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLocationRelativeTo(null);
splashScreen.setProgress("complete", 100);
setVisible(true);
}
}