【问题标题】:Back button that will open main Stage in Javafx 2将在 Javafx 2 中打开主舞台的后退按钮
【发布时间】:2014-08-06 19:30:18
【问题描述】:

我在应用程序中有 2 个屏幕。第一个屏幕(MainController 类)从 Eclipse 运行应用程序打开。

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

如何在第二个屏幕中制作某种“返回”按钮来显示第一个屏幕?

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

【问题讨论】:

    标签: javafx back scenebuilder


    【解决方案1】:

    这是一个包含屏幕的小示例,以展示如何实现您正在寻找的内容

    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;
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2015-05-21
      • 1970-01-01
      • 2011-11-28
      • 1970-01-01
      • 2014-05-12
      • 1970-01-01
      • 1970-01-01
      • 2013-05-14
      • 2018-09-06
      相关资源
      最近更新 更多