2012-07-09 9 views
9

私のメインクラスはファイルから設定を読み込み、フレームを表示します。私は、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 
    } 
} 

私はそうそれを達成する方法をアドバイスしてください、スイングで多くの経験を持っていません。それは停止することなく永遠にカウントし続けるカウンタが指定された数に到達したときに

  • はそれが(300)で停止する必要があります

    UPDATE: iは、次の例を発見したが、それは少し問題がありますタイマーとスプラッシュ画面を非表示にします。

  • ファイルロード中にカウンターをバインドしたいので、ファイルがロードされている間に、ファイルがロードされるまで進行状況がロードされ、進行状況が完了し、スプラッシュ画面が消えます。

    @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 
    
         } 
    
        } 
    
    } 
    
+1

[この質問](http://stackoverflow.com/questions/2089359/java-6-splash-screen?rq=1)をチェックしましたか?それはいくつかの良いリンクを持っています。 –

答えて

7

それはタイマーを停止し、 スプラッシュ画面を隠すことなく、それは永遠にカウントし続ける(300) で停止する必要があります。

次のコードは、(致命的な欠陥とカウンタはその後、長いファイルの読み込みとその逆がかかる場合があります)偉大な動作するようです:

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

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

私は、ファイルにカウンターをバインドしたいです読み込み中ですので、ファイルが に読み込まれている間、ファイルが読み込まれるまで進捗状況が読み込まれ、 の進行が完了し、スプラッシュ画面が消えます。

あなたはTaskは、あなたがして、ファイルが完全に読み込まれたときにチェックして、SplashScreenを終了して使用してProgressMonitorProgressMonitorInputStreamを見てみる必要があります。いくつかの偉大なチュートリアルと説明のためにhereを参照してください

+0

非常に高速にロードするので、どのようにプログレスバーをロードするのが遅いのですか?私は数を増やそうとしましたが、それでもまだ非常に高速です。 –

+0

@Msalehあなたがコードを使っているなら、私は単純に 'timer1 = new Timer(400、al);の値をもっと大きなものに変更しました。この例では、タイマは0.5秒ごとに呼び出されます –

+0

'if(count == 300)'と 'timer1 = new Timer(50、al);の違いを説明できますか? 。 –

10

Javaは、まさにこの目的のためにSplashScreenクラスが組み込まれています。それを使用する方法に関するチュートリアルがありますhere。カウンタが指定された数に到達したときに

+0

ありがとう。あなたがそれを言及する前に、APIのSplashScreenクラスを発見していなかった。 – Bobulous

関連する問題