2012-07-09 4 views
9

내 메인 클래스가 파일에서 구성을로드 한 다음 프레임을 표시합니다. 나는 이클립스와 같은 진행 막대가있는 스플래시 화면을 만들어서 파일이로드되는 동안 진행이 증가하고 파일이로드 된 후에 스플래시가 사라지도록하려고한다. 그러면 내 메인 프레임이로드됩니다.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 : 나는 다음과 같은 예를 발견,하지만 약간의 문제가 타이머 및 스플래시 화면 숨기기.

  • 파일을로드하는 동안 카운터를 바인딩하려는 경우 파일이로드 될 때까지 진행률이로드되고 진행률이 완료되고 스플래시 화면이 사라집니다.

    @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

자바는 바로 이러한 목적을 위해 SplashScreen 클래스에 내장하고있다. 그것을 사용하는 방법에 대한 자습서가 있습니다 here. 카운터가 지정된 수에 도달 할 때

+0

고마워요. 언급하기 전에 API에서 SplashScreen 클래스를 발견하지 못했습니다. – Bobulous

관련 문제