我的主类从文件加载配置,然后显示一个框架。我想制作一个像 Eclipse 这样的带有进度条的启动画面,以便在加载文件时进度会增加,并且在加载文件后启动画面会消失。然后我的主框架被加载。
主类代码:
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"classpath:/META-INF/spring/applicationContext.xml");
// splash with progress load till this file is loaded
UserDao userDao = context.getBean(UserDao.class);
isRegistered = userDao.isRegistered();
System.out.println("registered: " + isRegistered);
if (isRegistered) {
// progress finish and hide splash
log.debug("user is registered"); // show frame1
} else {
// progress finish and hide splash
log.debug("user is not registered"); // show frame2
}
}
我对 Swing 没有太多经验,所以请告知如何实现。
更新:我找到了以下示例,但问题不大:
当计数器达到指定的数字时,它应该在 (300) 处停止,它会一直计数,而不会停止计时器并隐藏启动屏幕。
我想将计数器绑定到文件加载,因此在加载文件时会加载进度,直到加载文件然后进度完成并且启动屏幕消失。
@SuppressWarnings("serial") @Component public class SplashScreen extends JWindow { static boolean isRegistered; static Log log = LogFactory.getLog(SplashScreen.class); private static JProgressBar progressBar = new JProgressBar(); private static SplashScreen execute; private static int count; private static Timer timer1; public SplashScreen() { Container container = getContentPane(); container.setLayout(null); JPanel panel = new JPanel(); panel.setBorder(new javax.swing.border.EtchedBorder()); panel.setBackground(new Color(255, 255, 255)); panel.setBounds(10, 10, 348, 150); panel.setLayout(null); container.add(panel); JLabel label = new JLabel("Hello World!"); label.setFont(new Font("Verdana", Font.BOLD, 14)); label.setBounds(85, 25, 280, 30); panel.add(label); progressBar.setMaximum(50); progressBar.setBounds(55, 180, 250, 15); container.add(progressBar); loadProgressBar(); setSize(370, 215); setLocationRelativeTo(null); setVisible(true); } public void loadProgressBar() { ActionListener al = new ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { count++; progressBar.setValue(count); if (count == 300) { timer1.stop(); execute.setVisible(false); return; } } }; timer1 = new Timer(50, al); timer1.start(); } public static void main(String[] args) { execute = new SplashScreen(); ApplicationContext context = new ClassPathXmlApplicationContext( "classpath:/META-INF/spring/applicationContext.xml"); UserDao userDao = context.getBean(UserDao.class); isRegistered = userDao.isRegistered(); if (isRegistered) { // show frame 1 } else { // show frame 2 } } }