JavaFX问题切换场景

时间:2016-11-01 18:58:34

标签: javafx scene stage

我遇到了以下问题:

我试图在一个阶段内切换两个场景。基本。但它不应该如何工作。

这是我的代码:

public class Controller {

    private Stage stage = new Stage();
    private Scene sceneLogin, sceneRegister;
    private Pane loginPane, registerPane;

    public void login(ActionEvent actionEvent) {

        try {
            loginPane = FXMLLoader.load(getClass().getResource("loginform.fxml"));
            sceneLogin = new Scene(loginPane, 280, 353);
            stage.setScene(sceneLogin);
            stage.setTitle("Authentification");
            stage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void register(ActionEvent actionEvent) {

        try {
            registerPane = FXMLLoader.load(getClass().getResource("registerform.fxml"));
            sceneRegister = new Scene(registerPane, 280, 353);
            stage.setScene(sceneRegister);
            stage.setTitle("Registration");
            stage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

现在:一旦我点击登录表单中的按钮,它就应该切换到第二个场景。但相反,它会在登录表单的顶部打开一个新窗口。

提前致谢。

---更多代码---

主要课程:

public class Main extends Application {

@Override
public void start(Stage primaryStage) throws Exception {
    // add sample.fxml as main FXML file for primaryStage
    Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
    primaryStage.setTitle("SecureTalk V1.0");

    // create primaryScene and add style.css as stylesheet for primaryScene
    Scene primaryScene = new Scene(root, 600, 400);
    primaryScene.getStylesheets().add(getClass().getResource("style.css").toExternalForm());

    // set icon for primaryStage
    Image icon = new Image(getClass().getResourceAsStream("icon.png"));
    primaryStage.getIcons().add(icon);

    // setScene and resizable false
    primaryStage.setScene(primaryScene);
    primaryStage.setResizable(false);
    primaryStage.show();

    // Check if admin in a group which requires new vigenere phrase

}


public static void main(String[] args) {
    launch(args);
}

}

0 个答案:

没有答案