在连接到MySQL数据库之前如何制作加载场景?

时间:2019-03-24 20:31:25

标签: java mysql javafx

我正在尝试创建一个加载窗口。但是我有MySQL驱动程序的问题。问题是,当我在我的场景(到组)中向我的场景(到组)中添加一些对象(例如GIF,但我也尝试了矩形),然后它连接到数据库时,在数据库连接完成之前,stage不显示任何对象。 / p>

我尝试了一些在Internet上找到的示例,但没有任何效果。所以我在这里找到帮助。这是我尝试的代码。

private void afterClick() {
    showLoading();
    username = name.getText();
    password = passField.getText();
    if ((username.length() == 0) && (password.length() == 0)) {
        Alert.printInformation("Nezadali jste přihlašovací údaje.");
    } else if (username.length() == 0) {
        Alert.printInformation("Nezadali jste login.");
    } else if (password.length() == 0) {
        Alert.printInformation("Nezadali jste heslo.");
    } else {
        DBConnection connection = new DBConnection();
        if(connection.connect()){
            if (check.isSelected()) {
                FileStream.writeToFile("Account.txt", LoginMenu.username + ";" + LoginMenu.password, false);
            } else {
                FileStream.writeToFile("Account.txt", "empty", false);
            }
            App.showScene(MenuName.MainMenu);
        }
    }

}



void showLoading(){

    ImageView loading = new ImageView(new Image("file:Wallpapers/loading.gif"));
    loading.setX(App.width -100);
    loading.setY(App.height - 100);
    loading.setFitWidth(60);
    loading.setFitHeight(60);

    Stage stage = new Stage();
    Group group = new Group();
    Scene scene1 = new Scene(group);
    group.getChildren().add(loading);
    stage.setScene(scene1);
    scene1.setFill(Color.TRANSPARENT);
    stage.initStyle(StageStyle.TRANSPARENT);
    stage.show();
}

1 个答案:

答案 0 :(得分:0)

非常简单!您可以使用覆盖初始化方法进行一些“预”动作。

class YourControllerClass implements Initializable{
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        //Do stuff
    }
}

此外,您还可以像这样在初始化方法new Thread中运行,以防止阻塞您的应用。

new Thread(() -> {
    //Your stuff
}).run();