2017-03-31 151 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