后退按钮将打开Javafx 2中的主舞台

use*_*480 1 javafx back scenebuilder

我申请了2个屏幕.第一个屏幕(MainController类),它使用Eclipse运行的应用程序打开.

第二个屏幕(SecondController类)在第一个屏幕上的按钮上打开.

如何在第二个屏幕上显示某种"后退"按钮,这将显示第一个屏幕?

如果重要的话,我正在JavaFX Scene Builder中创建应用程序的可视部分.

Ita*_*iha 6

这是一个小例子,包含屏幕,展示如何实现您的目标

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class TwoScreensWithInterchange extends Application {


    @Override
    public void start(Stage stage) throws Exception {
        Scene scene = new Scene(loadScreenOne(), 200, 200);
        stage.setScene(scene);
        stage.show();
    }

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

    public VBox loadScreenOne()
    {
        VBox vBox = new VBox();
        vBox.setAlignment(Pos.CENTER);
        final Button button = new Button("Switch Screen");
        button.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent arg0) {
                button.getScene().setRoot(loadScreenTwo());             
            }
        });
        Text text = new Text("Screen One");
        vBox.getChildren().addAll(text, button);
        vBox.setStyle("-fx-background-color: #8fbc8f;");
        return vBox;
    }

    public VBox loadScreenTwo()
    {
        VBox vBox = new VBox();
        vBox.setAlignment(Pos.CENTER);
        final Button button = new Button("Back");
        button.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent arg0) {
                button.getScene().setRoot(loadScreenOne());             
            }
        });
        Text text = new Text("Screen Two");
        vBox.getChildren().addAll(text, button);
        return vBox;
    }

}
Run Code Online (Sandbox Code Playgroud)