JavaFX、FXML、登录表单和更改阶段或场景
我用Scene Builder制作了一个简单的登录表单。 登录表单工作正常,但当我输入user/pass并打开主窗口时,登录窗口仍然打开。如何关闭它?问候 下面是代码DomaciFinal.java:JavaFX、FXML、登录表单和更改阶段或场景,java,javafx,login,javafx-8,scenebuilder,Java,Javafx,Login,Javafx 8,Scenebuilder,我用Scene Builder制作了一个简单的登录表单。 登录表单工作正常,但当我输入user/pass并打开主窗口时,登录窗口仍然打开。如何关闭它?问候 下面是代码DomaciFinal.java: @Override public void start(Stage stage) { try { Parent root = FXMLLoader.load(getClass().getResource("Login.fxml")); Scene scen
@Override
public void start(Stage stage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("Login.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
} catch (IOException ex) {
System.out.println(ex);
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
MainController.java:
public class MainController {
@FXML
private Label lblStatus;
@FXML
private TextField txtUserName;
@FXML
private TextField txtPassword;
public void Login(ActionEvent event) throws Exception {
if (txtUserName.getText().equals("user") && txtPassword.getText().equals("pass")) {
lblStatus.setText("Uspešno ste ulogovani.");
Stage primaryStage = new Stage();
Parent root = FXMLLoader.load(getClass().getResource("Main.fxml"));
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
} else {
lblStatus.setText("Neispravni podaci.");
}
}
提前感谢。您可以采取的一种方法是将原始舞台的场景设置为主场景。
您需要更改主类来存储stage,并添加一个getter来获取primaryStage,然后使用它来代替带有Login方法的primaryStage代码>@James\u非常感谢你,它很有效!:)你能给我解释一下这行代码吗?当做没什么好解释的
txtUserName.getScene()
返回包含文本字段txtUserName
的场景getWindow()
返回包含场景的窗口hide()
隐藏(关闭)窗口。如果登录场景显示在此窗口中,则不需要执行此操作,请参阅