2017-03-31 153 views
0

我有A.fxml和B.fxml。使用Java Application覆蓋啓動方法運行。我想每循環40分鐘(5次){打開新階段B.fxml並等待stage.close,如果階段關閉繼續循環打開新階段B fxml。循環五次。我嘗試計時器timertask我不能。我嘗試JavaFX服務,我不能。我創建Mythread擴展線程對象。這次我無法控制下一階段的循環。當陳述開始開放5階段。但我想循環等待currentstage接近然後去下一個循環。這是我的失敗代碼;JavaFX Auto打開新窗口

public class Driver extends Application { 

public static Stage stage; 

@Override 
public void start(Stage primaryStage) throws Exception { 
    FXMLLoader loader = new FXMLLoader(getClass().getResource(View.SETTINGS)); 
    Parent root = loader.load(); 
    Scene scene = new Scene(root); 
    stage = primaryStage; 
    stage.setScene(scene); 
    stage.setTitle("Info Library"); 
    stage.setResizable(false); 
    stage.show(); 
    RandomQuestionThread thread = new RandomQuestionThread(); 
    if (DBContext.settings.isAbbreviation() || DBContext.settings.isTranslation()) { 
     thread.start(); 
    } 
} 

public static void main(String[] args) throws InterruptedException { 
    DBContext.settings = DBContext.getInstance().settings().getSettings(); 

    launch(args); 
    HibernateUtil.getSessionFactory().close(); 
} 

}

public class RandomQuestionThread extends Thread { 
Thread randomThread = new Thread(this); 
private String fxml; 
private static String TITLE; 


@Override 
public void run() { 
    while (true) { 
     try { 
      Thread.sleep(DBContext.settings.getAutoQuestionTime() * 6000); 
     } catch (InterruptedException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 
     for (int i = 0; i<DBContext.settings.getAutoQuestionCount(); i++) { 
      randomFxml(); 
      Platform.runLater(()->{ 
       Parent root = null; 
       try { 
        root = new FXMLLoader(getClass().getResource(fxml)).load(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       Stage stage = new Stage(); 
       stage.setScene(new Scene(root)); 
       stage.setTitle(TITLE); 
       stage.show(); 
       System.out.println(currentThread().getName()); 
      }); 
     } 
    } 
} 

private void randomFxml() { 
    int start = 0; 
    if (DBContext.settings.isTranslation() && DBContext.settings.isAbbreviation()) { 
     start = new Random().nextInt(2); 
    } else if (DBContext.settings.isTranslation()) { 
     start = 1; 
    } 

    switch (start) { 
    case 0: 
     fxml = View.ABBREVIATION; 
     break; 
    case 1: 
     fxml = View.TRANSLATION; 
     break; 

    default: 
     break; 
    } 
    if (start == 0) { 
     TITLE = "KISALTMA SORUSU"; 
    } else TITLE = "ÇEVİRİ SORUSU"; 
} 

}

我需要工作更多的Java線程多。但解決這個問題後。請解釋我做錯了什麼。在循環寫入控制檯currentThread名稱控制檯結果「Java Apllication Thread」中。但我設置了我的線程名稱「MyThread」。我很困惑。我的大腦給了藍屏錯誤。

回答

0

我解決這個問題。我在我的主控制器init方法中使用了Timer和TimeTask。和它的工作。但是,應用程序啓動方法或面向方法階段的相同代碼沒有等待。我用stageshowandwait()方法,但線程沒有等待。但是在主控制器初始化方法中啓動了相同的代碼。爲什麼我不知道。

Timer timer = new Timer(); 
    TimerTask timerTask = new TimerTask() { 

     @Override 
     public void run() { 
      Platform.runLater(()->{ 
       for (int i = 0; i<4; i++) { 
        Parent root = null; 
        try { 
         root = new FXMLLoader(getClass().getResource(View.ABBREVIATION)).load(); 
        } catch (IOException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
        Stage stage = new Stage(); 
        stage.setScene(new Scene(root)); 
        stage.setTitle("deneme"); 
        stage.showAndWait(); 
       } 
      }); 
     } 
    }; 

    timer.schedule(timerTask, 6000); 
0

你把你的System.out.println(currentThread().getName())語句轉換成Platform.runLater(),這意味着它會在JavaFX應用程序線程(見JavaDoc)執行。

關於您的安排某些任務重複固定次數與預定義的速度的問題,this post可以幫助你。

0

在循環寫入控制檯currentThread名稱控制檯結果「Java Apllication Thread」。但我設置了我的線程名稱「MyThread」。我很困惑。

使用Platform.runLater你安排Runnable將JavaFX應用程序線程,而不是當前線程,它允許你修改UI上執行,但也導致當前線程是JavaFX應用程序線程,而不是線程你調用Platform.runLater從...

如果你想繼續「循環」 後的窗口已經關閉,您應該安排在打開一個窗口的最後一個已被關閉之後。 Stage.showAndWait()是等待舞臺關閉的便捷方式。

調度,我建議使用ScheduledExecutorService

private ScheduledExecutorService executor; 

@Override 
public void stop() throws Exception { 
    // stop executor to allow the JVM to terminate 
    executor.shutdownNow(); 
} 

@Override 
public void init() throws Exception { 
    executor = Executors.newSingleThreadScheduledExecutor(); 
} 

@Override 
public void start(Stage primaryStage) { 
    Button btn = new Button("Start"); 
    btn.setOnAction(new EventHandler<ActionEvent>() { 

     public void handle(ActionEvent event) { 
      // just display a "empty" scene 
      Scene scene = new Scene(new Pane(), 100, 100); 
      Stage stage = new Stage(); 
      stage.setScene(scene); 

      // schedule showing the stage after 5 sec 
      executor.schedule(new Runnable() { 

       private int openCount = 5; 

       @Override 
       public void run() { 
        Platform.runLater(() -> { 
         stage.showAndWait(); 
         if (--openCount > 0) { 
          // show again after 5 sec unless the window was already opened 5 times 
          executor.schedule(this, 5, TimeUnit.SECONDS); 
         } 
        }); 
       } 

      }, 5, TimeUnit.SECONDS); 

     } 
    }); 

    StackPane root = new StackPane(); 
    root.getChildren().add(btn); 

    Scene scene = new Scene(root); 

    primaryStage.setScene(scene); 
    primaryStage.show(); 
} 
+0

showandwait()沒有在應用程序啓動方法或主要方法中工作。階段沒有等待或線程,循環我現在在maincontroller init方法中嘗試相同的代碼。這個時間階段等待或循環或線程。 Porblem固定但我不明白爲什麼。 –

+0

@VolkanOkçu我知道'showAndWait'的唯一限制是:a)從應用程序線程中調用它,並且b)不能爲主要階段調用。 – fabian