簡體   English   中英

javafx如何關閉其他類的窗口

[英]how javafx close window from other class

Sembako.java此主類

public class Sembako extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("rootScene.fxml"));

        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.setTitle("Login");
        stage.setResizable(false);
        stage.show();
    }

rootSceneController.java

@FXML私有void loginAction(ActionEvent事件)引發IOException {

for (indrian16.oulook.id.co.sembako.entity.Login l : listLogin) {

    if(username.getText().equals(l.getUsername())) {
        if(password.getText().equals(l.getPassword())) {
            Parent rootDashBoard = FXMLLoader.load(getClass().getResource("dashboard.fxml"));

            Stage stage = new Stage();
            Scene scene = new Scene(rootDashBoard);
            stage.setScene(scene);
            stage.setTitle("Sembako");
            stage.show();

            stage.close(); //how close Sembako scene :)

        } else {
            msgLogin.setText("Password Invalid");
        }
    } else {
        msgLogin.setText("Username Invalid");
    }

}

}

rootSceneController.java中的窗口Sembako場景如何關閉

對不起,我英語不好

我想您是在問如何關閉以前打開的窗口。 假設這是包含username文本字段的窗口,則可以

username.getScene().getWindow().hide();

有一個簡單的方法,

Stage windows;

public void start(Stage primaryStage) throws Exception {
    windows = primaryStage;
}

然后在所需的類中傳遞舞台窗口並使用

windows.close();

這可以從其他類中關閉應用程序:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM