I have create JavaFX login window that after log in into main window the login window will be closed.
我创建了JavaFX登录窗口,登录到主窗口后,登录窗口将被关闭。
I use Platform.exit();
but it exit from entire application instead of login window.
我使用Platform.exit();但它退出整个应用程序而不是登录窗口。
I can't access to stage from controller class. I am using closeLogin()
method below but it doesn't work for me. This is my LoginController class:
我无法访问控制器类的阶段。我正在使用下面的closeLogin()方法,但它对我不起作用。这是我的LoginController类:
public class LoginController implements Initializable {
@FXML
private TextField txt_username;
@FXML
private PasswordField txt_password;
@FXML
private Button btn_login;
@FXML
private Button btn_cancel;
Stage stage1 = null;
/**
* Initializes the controller class.
*/
@FXML
private void btn_login(ActionEvent event) throws IOException, Exception {
Parent root = FXMLLoader.load(getClass().getResource("Library.fxml"));
Stage page1Stage = new Stage();
page1Stage.setResizable(false);
page1Stage.setTitle("Main Form ");
Scene scene = new Scene(root);
page1Stage.setScene(scene);
/*
Some codes
*/
page1Stage.show();
closeLogin();
}
public void closeLogin() {
stage1 = (Stage) stage1.getScene().getWindow();
stage1.close();
}
@FXML
public void cancelButtonAction() {
Platform.exit();
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
1 个解决方案
#1
Use close()
of Stage
. Platform.exit will terminate the JavaFX application.
使用Stage的close()。 Platform.exit将终止JavaFX应用程序。
loginWindow.close();
where loginWindow
is the stage you want to close.
其中loginWindow是您要关闭的阶段。
Update
You can try to get instance of Stage
from stage1
which has not been initialized yet. Using
您可以尝试从stage1获取尚未初始化的Stage实例。运用
stage1.getScene().getWindow();
should throw NullPointerException
.
应该抛出NullPointerException。
Try getting an instance of Window
from the TextField
or the Button
, which are already present on the scene graph.
尝试从TextField或Button获取Window的实例,这些实例已经存在于场景图中。
public void closeLogin() {
stage1 = (Stage) btn_login.getScene().getWindow();
stage1.close();
}
#1
Use close()
of Stage
. Platform.exit will terminate the JavaFX application.
使用Stage的close()。 Platform.exit将终止JavaFX应用程序。
loginWindow.close();
where loginWindow
is the stage you want to close.
其中loginWindow是您要关闭的阶段。
Update
You can try to get instance of Stage
from stage1
which has not been initialized yet. Using
您可以尝试从stage1获取尚未初始化的Stage实例。运用
stage1.getScene().getWindow();
should throw NullPointerException
.
应该抛出NullPointerException。
Try getting an instance of Window
from the TextField
or the Button
, which are already present on the scene graph.
尝试从TextField或Button获取Window的实例,这些实例已经存在于场景图中。
public void closeLogin() {
stage1 = (Stage) btn_login.getScene().getWindow();
stage1.close();
}