2016-10-20 2 views
0

실제 응용 프로그램 창 열기 전에로드하는 창을 만드는 중입니다. 적재 장면에 progressBar가 있고 그것은 미정입니다.javaFx에서 응용 프로그램 창 앞에 장면로드하는 방법?

문제는 있습니다. progressbar는 프로그램을 실행할 때 실제 창이 열릴 때까지 작동하지 않습니다.

나는 preloader 클래스를 시도했지만, 역시 작동하지 않습니다.

여기 내 코드입니다.

public class MainApp2 extends Application { 


    private Stage loadingStage = new Stage(); 

    public static void main(String[] args) { 
     launch(args); 
    } 


    public void start(final Stage mainStage) throws Exception { 

     //loading.. 
     loadingScreen(); 

     //start... 
     appScreen(); 

    } 


    private void loadingScreen() { 

     ProgressBar bar = new ProgressBar(ProgressIndicator.INDETERMINATE_PROGRESS); 
     bar.setPrefWidth(300); 
     bar.setPrefHeight(200); 

     loadingStage.initStyle(StageStyle.TRANSPARENT); 
     loadingStage.initStyle(StageStyle.UNDECORATED); 
     loadingStage.setScene(new Scene(bar)); 
     loadingStage.show(); 

    } 


    private void appScreen() { 

     new Task<Void>() { 
      @Override 
      protected Void call() throws Exception { 

       Stage mainStage = new Stage(); 

       //get real window 
       Scene root = new Scene(new MyAppWindow().getAppWindow()); 
       mainStage.setScene(root); 
       mainStage.centerOnScreen(); 
       mainStage.show(); 

//    loadingStage.close(); 

       return null; 
      } 
     }.run(); 

    } 

    public class MyAppWindow { 

     public BorderPane getAppWindow(){ 

      System.out.println("may be initialize take a long time..."); 
      for (int i = 0; i < 90000000; i++) { 
       System.out.print(""); 
      } 

      return new BorderPane(new Label("Here is real application Window!")); 
     } 


    } 


} 

답변

1

적어도 나는 그것을 발견했다. 어쨌든 JavaFx에서 시작 화면을 초기화 할 필요는 없습니다. JavaFx는 하나의 스레드에 의해 관리되기 때문에. 그래서이 단계를 시도했습니다. 그것은 나를 위해 작동합니다.

public void initAppScreen(final Stage mainStage) { 

     loadingWindow = new LoadingWindow().getInitWindow(); 

     applicationContext = new ClassPathXmlApplicationContext("hibernate-config.xml"); 

     initSample(); 

     Platform.runLater(new Runnable() { 
      public void run() { 

       Scene root = new Scene(new AppWindow().getAppWindow()); 
       mainStage.setScene(root); 

       mainStage.setTitle("My Application Title"); 
       mainStage.getIcons().add(new Image("/images/logo.png")); 
       mainStage.setOnCloseRequest(event -> System.exit(0)); 

       mainStage.setWidth(APP_WINDOW_WIDTH); 
       mainStage.setHeight(APP_WINDOW_HEIGHT); 
       mainStage.setMinWidth(APP_WINDOW_WIDTH); 
       mainStage.setMinHeight(APP_WINDOW_HEIGHT); 

       Rectangle2D screenBounds = Screen.getPrimary().getVisualBounds(); 
       mainStage.setX((screenBounds.getWidth() - APP_WINDOW_WIDTH)/2); 
       mainStage.setY((screenBounds.getHeight() - APP_WINDOW_HEIGHT)/2); 

       mainStage.show(); 
       loadingWindow.dispose(); 
      } 
     }); 

    } 

그리고 내 시작 화면

public class LoadingWindow { 


    public JFrame getInitWindow() { 

     JFrame loadingFrame = new JFrame(); 

     URL url = this.getClass().getResource("/images/loading.gif"); 
     Icon icon = new ImageIcon(url); 
     JLabel label = new JLabel(icon); 

     loadingFrame.setIconImage(new ImageIcon(getClass().getResource("/images/logo.png").getPath()).getImage()); 
     loadingFrame.setUndecorated(true); 
     loadingFrame.setBackground(new Color(0f, 0f, 0f, 0f)); 

     Rectangle2D screenBounds = Screen.getPrimary().getVisualBounds(); 
     loadingFrame.setLocation((int) ((screenBounds.getWidth() - 600)/2), (int) ((screenBounds.getHeight() - 0)/2)); 

     loadingFrame.getContentPane().add(label); 
     loadingFrame.pack(); 
     loadingFrame.setLocationRelativeTo(null); 
     loadingFrame.setVisible(true); 

     return loadingFrame; 
    } 

} 
1

프리 로더를 사용할 수 있습니다. 여기에 예제가 있습니다. from taken source

public class FirstPreloader extends Preloader { 
    ProgressBar bar; 
    Stage stage; 

    private Scene createPreloaderScene() { 
     bar = new ProgressBar(); 
     BorderPane p = new BorderPane(); 
     p.setCenter(bar); 
     return new Scene(p, 300, 150);   
    } 

    public void start(Stage stage) throws Exception { 
     this.stage = stage; 
     stage.setScene(createPreloaderScene());   
     stage.show(); 
    } 

    @Override 
    public void handleProgressNotification(ProgressNotification pn) { 
     bar.setProgress(pn.getProgress()); 
    } 

    @Override 
    public void handleStateChangeNotification(StateChangeNotification evt) { 
     if (evt.getType() == StateChangeNotification.Type.BEFORE_START) { 
      stage.hide(); 
     } 
    }  
} 
+0

감사합니다,하지만 나를 위해 작동하지 않으며, 당신의 프리 로더에 대한 제안에 대한 –

+2

덕분에 다시, 나는 기사를 발견 왜 몰라 [HTTPS ://blog.codecentric.de/ko/2015/09/javafx-how-to-easily-implement-application-preloader-2/] 내 문제가 해결 될 것이라고 생각합니다. –