从文件中我的主类负载配置则显示了一个框架。 我想打一个闪屏与Eclipse这样一个进度条,这样在加载该文件的进度将会增加,加载后飞溅消失。 然后,我的主框架被加载。
MainClass代码:
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 } } }