后退按钮将打开Javafx 2中的主舞台 [英] Back button that will open main Stage in Javafx 2

查看:198
本文介绍了后退按钮将打开Javafx 2中的主舞台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个应用程序的屏幕。第一个屏幕(MainController类),它从Eclipse运行的应用程序打开。

I have 2 screens in application. First screen(MainController class) which opens with running application from Eclipse.

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

And second screen(SecondController class) which opens on button located in first screen.

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

How can I make some kind of 'Back' button in second screen which will show back first screen?

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

I'm creating the visual part of the application in JavaFX Scene Builder if it matters.

推荐答案

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

Here is a small example, containing to screens, to show how you can achieve what you are looking for

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

}

这篇关于后退按钮将打开Javafx 2中的主舞台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆