JavaFX : Getting back to main page without FXML

标签 java javafx

我正在自学JavaFx,还没有达到FXML。我被困在一个应用程序中,我计划在用户在第二个场景中输入凭据后让它返回到应用程序的主场景。我设法从主场景中调出第二个场景,但无法从第二个场景转到主场景。我尝试使用 getter 获取主场景和 Pane ,但没有成功。你们能教出正确的方法吗?

提前谢谢您。

public class Landing extends Application {
    BorderPane bp;
    Scene scene;
    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setTitle("Welcome to our Telco!");
        bp = new BorderPane();
        VBox vbox = new VBox();
        Button login = new Button("Login");
        login.setMinWidth(100);

        Button acc = new Button("Account Information");
        acc.setMinWidth(100);

        vbox.getChildren().addAll(acc);

        bp.setCenter(vbox);

        acc.setOnAction(e ->{
            AccountInfo account = new AccountInfo();
            primaryStage.setTitle("Account Information"); // Set the stage title
            primaryStage.getScene().setRoot(account.getbp());; // Place the scene in the stage      
        });

        scene = new Scene(bp, 750, 550);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    public Pane getbp() {
        return bp;
    }
    public Scene getSc(){
        return scene;
    }

获取主场景的按钮

public class AccountInfo {
    BorderPane pane;
    Landing main = new Landing();
    Scene scene;
    AccountInfo() {   
        Button c = (new Button("Back"));
        c.setStyle("-fx-background-color: pink");
        c.setOnAction((ActionEvent e) -> {
        main.getbp();
        main.getSc();
    });
    public Pane getbp() {
        return pane;
    }
}

最佳答案

Landing 不是一个场景,它是一个应用程序。到目前为止,您的整个应用程序中只有一个场景。您绝不能尝试在同一 JavaFX 应用程序生命周期内实例化(并随后运行)任何 Application 类的多个实例。当您在 AccountInfo 类中执行 Landing main = new Landing(); 时,您正朝着这个方向危险地前进。

来自Javadoc对于Application.launch:

Throws: IllegalStateException - if this method is called more than once.

您需要的是第一个登录场景(即输入凭据)。登录成功后,您将创建一个新的场景对象并使用下一个“ View ”填充该场景,然后将该新场景设置到舞台。

public class Landing extends Application {
    BorderPane bp;
    Scene scene;

    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setTitle("Welcome to our Telco!");
        bp = new BorderPane();
        VBox vbox = new VBox();
        Button login = new Button("Login");
        login.setMinWidth(100);

        Button acc = new Button("Account Information");
        acc.setMinWidth(100);

        vbox.getChildren().addAll(acc);

        bp.setCenter(vbox);

        acc.setOnAction(e -> {
            primaryStage.setTitle("Account Information"); // Set the stage title
            BorderPane infoScenePane = new BorderPane();
            Scene infoScene = new Scene(infoScenePane);
            primaryStage.setScene(infoScene);
        });

        scene = new Scene(bp, 750, 550);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

关于JavaFX : Getting back to main page without FXML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44104343/

相关文章:

c# - 理解 wsdl 服务的 java 类

java - 为什么我的下载进度条会多次触发同一个事件?

java - 前后循环在一条线上

java - 如何将 JavaFX ComboBox 中的选择转换为字符串

Javafx文本区域滚动 Pane 边框颜色问题

java - 将 JSF 应用程序迁移到 JavaFX

java - 简化 Tomcat 7 中的 web.xml 配置

java - 从抽象类和子类java创建对象数组

JavaFX 虚拟键盘闪烁

JavaFX 8 图像加载(无效 URL 或未找到资源)