JavaFX回到上一阶段然后使应用程序崩溃

时间:2017-12-21 10:35:17

标签: java javafx

我遇到一个简单的应用程序问题,在一个阶段我已经有一个登录屏幕,当用户输入有效的用户名&密码它导致另一个阶段,在Stage2我有一个关闭会话的按钮,带你回到登录阶段,问题是,如果我再次尝试登录,应用程序停止工作(在控制台那里'没有错误。)

以下是我的代码:

Login.class

public class DocumentController implements Initializable {

    @FXML
    private Button btn_login;
    @FXML
    private TextField et_username;
    @FXML
    private PasswordField et_password;
    @FXML
    private Hyperlink link_signup;
    @FXML
    private AnchorPane ap_login;


    public void login() throws IOException {

        System.out.println("Login");
        Stage stageRegister = (Stage) ap_login.getScene().getWindow();
        Connector conn = new Connector();

        if (conn.checkUser(et_username.getText(), et_password.getText())) {

            System.out.println("1");

            Preferences prefs = Preferences.userNodeForPackage(DocumentController.class);
            prefs.put("username", et_username.getText());
            prefs.put("password", et_password.getText());

            System.out.println("2");

            TaskerController controler = new TaskerController();
            controler.setUser(et_username.getText());
            FXMLLoader task = new FXMLLoader(getClass().getResource("/view/FXMLTasker.fxml"));

            System.out.println("3");

            task.setController(controler);
            System.out.println("3.1");
            Pane pane = task.load();

            System.out.println("4");

            Scene taskScene = new Scene(pane);
            Stage taskStage = new Stage();
            taskStage.setOnCloseRequest(t -> System.exit(0));

            System.out.println("5");


            taskStage.setScene(taskScene);
            taskStage.setTitle("Tasks");
            stageRegister.close();
            taskStage.show();
        } else {
           Alert alert = new Alert(Alert.AlertType.INFORMATION);
           alert.setTitle("Error");
           alert.setHeaderText("Information Alert");
           String s ="Usuario o Contraseña incorrecta";
           alert.setContentText(s);
           alert.show();
        }
    }

    public void register() throws IOException {
        Stage stageRegister = (Stage) ap_login.getScene().getWindow();

        FXMLLoader createTask = new FXMLLoader(getClass().getResource("/view/FXMLRegister.fxml"));
        Pane pane = createTask.load();
        Scene createTaskScene = new Scene(pane);
        Stage createTaskStage = new Stage();
        createTaskStage.setScene(createTaskScene);
        createTaskStage.setTitle("Register");
        stageRegister.close();
        createTaskStage.show();
    }

    @FXML
    private void handleButtonAction(ActionEvent event) throws IOException {
        if (btn_login == event.getSource()) {
            login();
        } else if (link_signup == event.getSource()) {
            register();
        }
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        Preferences prefs = Preferences.userNodeForPackage(DocumentController.class);

        et_username.setText(prefs.get("username", "Introduce el nombre de "));
        et_password.setText(prefs.get("password", "123"));

        Platform.runLater(() -> et_username.requestFocus());

    }

}

返回Login.class的另一个舞台上的方法

@FXML
void cerrar_sesion() throws IOException {
    Stage stageRegister = (Stage) ap_tasker.getScene().getWindow();

    Preferences prefs = Preferences.userNodeForPackage(DocumentController.class);
    prefs.put("username", "");
    prefs.put("password", "");

    FXMLLoader login = new FXMLLoader(getClass().getResource("/view/FXMLLogin.fxml"));
    Pane pane = (Pane) login.load();
    Scene loginScene = new Scene(pane);
    Stage loginStage = new Stage();
    loginStage.setScene(loginScene);
    loginStage.setTitle("Login");
    loginStage.setOnCloseRequest((WindowEvent t) -> System.exit(0));
    stageRegister.close();
    loginStage.show();
}

我发现该应用程序在Login.class中到达此行时停止工作:

Pane pane = task.load();

知道问题是什么?

0 个答案:

没有答案