【发布时间】:2014-08-06 19:30:18
【问题描述】:
我在应用程序中有 2 个屏幕。第一个屏幕(MainController 类)从 Eclipse 运行应用程序打开。
第二个屏幕(SecondController 类)在位于第一个屏幕的按钮上打开。
如何在第二个屏幕中制作某种“返回”按钮来显示第一个屏幕?
如果重要的话,我会在 JavaFX Scene Builder 中创建应用程序的可视化部分。
【问题讨论】:
标签: javafx back scenebuilder
我在应用程序中有 2 个屏幕。第一个屏幕(MainController 类)从 Eclipse 运行应用程序打开。
第二个屏幕(SecondController 类)在位于第一个屏幕的按钮上打开。
如何在第二个屏幕中制作某种“返回”按钮来显示第一个屏幕?
如果重要的话,我会在 JavaFX Scene Builder 中创建应用程序的可视化部分。
【问题讨论】:
标签: javafx back scenebuilder
这是一个包含屏幕的小示例,以展示如何实现您正在寻找的内容
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;
}
}
【讨论】: