JavaFX和线程

时间:2017-03-31 00:35:28

标签: java multithreading javafx

我想尝试什么?我想首先用main方法打开Setting.fxml。我的RandomQuestionThread开始了。我希望每个xmin或小时或秒这个线程等待然后运行问题。 for循环语句中的缩写或翻译问题。示例5次打开新问题。但线程必须等待关闭阶段。接下来的问题。我不确定自己是否可以表达自己。请看我的代码。

驱动程序

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

public static void main(String[] args) throws InterruptedException {
    DBContext.settings = DBContext.getInstance().settings().getSettings();
    RandomQuestionThread thread = new RandomQuestionThread();
    if (DBContext.settings.isAbbreviation() || DBContext.settings.isTranslation()) {
        thread.start();
    }
    launch(args);
    HibernateUtil.getSessionFactory().close();
}

}

MyThread的

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


@Override
public void run() {
    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();
            //Thread must be stop there for stage is closed. 
        });
    }
}

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";
}

}

0 个答案:

没有答案