To mój powitalny kod ekranem,Swing ekran powitalny z paska postępu
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 + "...");
}
});
}
}
Z głównego sposobu Ja powołuje tak,
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);
}
}
});
}
w konstruktorze AppFrame nazywam, ekran powitalny .setProgress (msg, val) metoda aktualizacji paska postępu. Ale plusk nie jest wyświetlany. Pokazuje się tylko na końcu, gdy ramka jest wyświetlana tylko przez ułamek sekundy, mimo że ładowanie zajmuje dużo czasu. Ale jeśli wstawię te trzy linie poza invokeLater(), pojawi się ekran powitalny, a pasek postępu ładnie się zaktualizuje. Wierzę, że aktualizacje GUI powinny być w invokeLater. Jaki może być problem?
Przy okazji, AppFrame ładuje różne panele mojej aplikacji.
Edytuj: Podsumowanie mojej aplikacji AppFrame pokazano poniżej.
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);
}
}
* "Wierzę, że aktualizacje GUI powinny być w invokeLater." * Lub 'invokeAndWait'. Ale zrób trochę rejestrowania, spodziewam się, że znajdziesz czas pomiędzy zakończeniem 'splashScreen.showScreen();' i 'AppFrame frame = new AppFrame (splashScreen);' jest znacznie mniej niż się spodziewasz. Zwróć też uwagę, że najlepsze plamy to stricte AWT (no Swing). –
Oznacza to, że powinienem używać okna lub ramki zamiast JWindow? Dzięki. – Praveen
Tak, użyj 'Window' lub' Frame'. Zajrzyj także do "MediaTracker' /' Canvas' & "EventQueue' - musi być ** no ** Import lub klasa Swing, lub cały pakiet Swing jest ładowany 1. –