java – 用Eclipse进度条制作闪屏
我的主要类从文件加载配置,然后显示一个框架.我想使用像
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 } } } 解决方法
下面的代码似乎工作很好(有致命的缺陷,计数器可能需要更长的时间,文件加载,反之亦然): import java.awt.Color; import java.awt.Container; import java.awt.Font; import java.awt.HeadlessException; import java.awt.event.ActionListener; import javax.swing.*; public class SplashScreen extends JWindow { static boolean isRegistered; 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,215); setLocationRelativeTo(null); setVisible(true); } private void loadProgressBar() { ActionListener al = new ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { count++; progressBar.setValue(count); System.out.println(count); if (count == 300) { createFrame(); execute.setVisible(false);//swapped this around with timer1.stop() timer1.stop(); } } private void createFrame() throws HeadlessException { JFrame frame = new JFrame(); frame.setSize(500,500); frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }; timer1 = new Timer(50,al); timer1.start(); } public static void main(String[] args) { execute = new SplashScreen(); } };
您应该使用任务来查看ProgressMonitor和ProgressMonitorInputStream,然后您可以检查文件何时完全读取并结束SplashScreen.看到here有一些很棒的教程和解释 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |