gpt4 book ai didi

java - 带进度条的 Swing 启动画面

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:14:26 24 4
gpt4 key购买 nike

这是我的启动画面代码,

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(消息, 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);
}
}

最佳答案

嗯,看看我放在一起的这个工作示例,它使用 SplashScreen 类(只使用一个简单的 TimerActionListener 来增加ProgressBar 的值直到 100 并显示一个框架):

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionListener;
import javax.swing.*;

public class SplashScreen extends JWindow {

private static JProgressBar progressBar = new JProgressBar();
private static SplashScreen splashScreen;
private static int count = 1, TIMER_PAUSE = 25,PROGBAR_MAX=100;
private static Timer progressBarTimer;
ActionListener al = new ActionListener() {

@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
progressBar.setValue(count);
System.out.println(count);
if (PROGBAR_MAX == count) {
splashScreen.dispose();//dispose of splashscreen
progressBarTimer.stop();//stop the timer
createAndShowFrame();
}
count++;//increase counter

}
};

public SplashScreen() {
createSplash();
}

private void createSplash() {
Container container = getContentPane();

JPanel panel = new JPanel();
panel.setBorder(new javax.swing.border.EtchedBorder());
container.add(panel, BorderLayout.CENTER);

JLabel label = new JLabel("Hello World!");
label.setFont(new Font("Verdana", Font.BOLD, 14));
panel.add(label);

progressBar.setMaximum(PROGBAR_MAX);
container.add(progressBar, BorderLayout.SOUTH);


pack();
setLocationRelativeTo(null);
setVisible(true);

startProgressBar();
}

private void startProgressBar() {
progressBarTimer = new Timer(TIMER_PAUSE, al);
progressBarTimer.start();
}

private void createAndShowFrame() {
JFrame frame = new JFrame();
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
splashScreen = new SplashScreen();
}
});
}
}

关于java - 带进度条的 Swing 启动画面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11933089/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com