【问题标题】:how to open all stage in only one window? [duplicate]如何在一个窗口中打开所有舞台? [复制]
【发布时间】:2020-01-09 21:03:24
【问题描述】:

我用 JavaFX 创建了一个小软件。主窗口有几个按钮可以打开其他页面。但是当我点击这些按钮时,它们会打开新窗口

希望所有这些页面都像普通软件一样在单个窗口中打开。

这是所有这些按钮上的事件代码。

@FXML
void Agenda(ActionEvent event) {

}

@FXML
void Informations(ActionEvent event) {
    try{

        FXMLLoader loade = new FXMLLoader(getClass().getResource("/cm/Project/View/Informations.fxml"));
        VBox root = (VBox) loade.load();
        Stage stage = new Stage();
        stage.setTitle("Bernon Storage");
        stage.setScene(new Scene(root));

        stage.show();
    } catch (Exception e){
        e.printStackTrace();
    }

}

@FXML
void Inscription(ActionEvent event) {
    try{
        FXMLLoader loade = new FXMLLoader(getClass().getResource("/cm/Project/View/Inscription.fxml"));
        VBox root = (VBox) loade.load();
        Stage stage = new Stage();
        stage.setTitle("Bernon Storage");
        stage.setScene(new Scene(root));
        stage.show();
    } catch (Exception e){
        e.printStackTrace();
    }

}

@FXML
void Notes(ActionEvent event) {
    try{
        FXMLLoader loade = new FXMLLoader(getClass().getResource("/cm/Project/View/Notes1.fxml"));
        VBox root = (VBox) loade.load();
        Stage stage = new Stage();
        stage.setTitle("Bernon Storage");
        stage.setScene(new Scene(root));
        stage.show();
    } catch (Exception e){
        e.printStackTrace();
    }

}

@FXML
void Staff(ActionEvent event) {
    try{
        FXMLLoader loade = new FXMLLoader(getClass().getResource("/cm/Project/View/Staff.fxml"));
        VBox root = (VBox) loade.load();
        Stage stage = new Stage();
        stage.setTitle("Bernon Storage");
        stage.setScene(new Scene(root));
        stage.show();
    } catch (Exception e){
        e.printStackTrace();
    }

}

@FXML
void Student(ActionEvent event) {
    try{
        FXMLLoader loade = new FXMLLoader(getClass().getResource("/cm/Project/View/Student.fxml"));
        VBox root = (VBox) loade.load();
        Stage stage = new Stage();
        stage.setTitle("Bernon Storage");
        stage.setScene(new Scene(root));
        stage.show();
    } catch (Exception e){
        e.printStackTrace();
    }

}

请帮我解决这个问题。这是我唯一必须完成这个项目的事情。

【问题讨论】:

  • 不要在新阶段打开,而是尝试在主窗口的根目录中设置加载的视图。
  • 这样做的方法是什么?
  • 通过快速演示更新了答案。
  • 一些想法here

标签: javafx javafx-8


【解决方案1】:

请查看下面的演示,快速了解如何在同一窗口中加载不同的视图。

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ToolBar;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class Window_Demo extends Application {
    BorderPane root;
    @Override
    public void start(Stage primaryStage) throws Exception {
        root = new BorderPane();
        Scene scene = new Scene(root, 500,500);
        primaryStage.setScene(scene);
        primaryStage.show();

        Button view1 = new Button("View 1");
        view1.setOnAction(e->{
            StackPane view = new StackPane(); // Load your fxml and get the node
            view.setStyle("-fx-background-color:red;-fx-opacity:.5;");
            root.setCenter(view);
        });
        Button view2 = new Button("View 2");
        view2.setOnAction(e->{
            StackPane view = new StackPane();  // Load your fxml and get the node
            view.setStyle("-fx-background-color:green;-fx-opacity:.5;");
            root.setCenter(view);
        });
        Button view3 = new Button("View 3");
        view3.setOnAction(e->{
            StackPane view = new StackPane();  // Load your fxml and get the node
            view.setStyle("-fx-background-color:blue;-fx-opacity:.5;");
            root.setCenter(view);
        });
        ToolBar toolBar = new ToolBar(view1,view2,view3);
        root.setTop(toolBar);
    }
}

【讨论】:

  • 我理解你的例子。但是我有几个独立于主要课程的课程。我不知道如何让它们从主要的显示出来。
  • 当你点击一个按钮时,你可以从按钮中获取场景并将根设置在那里,例如使用 Sai 示例中的一些变量,在 view3 的 on action 处理程序中,编写 view3.getScene().getRoot().setCenter(view);
  • 我通过输入Stage window = (Stage)((Node)event.getSource()).getScene().getWindow();解决它
猜你喜欢
  • 2017-05-15
  • 2012-06-22
  • 1970-01-01
  • 2019-08-05
  • 2011-11-04
  • 1970-01-01
  • 2013-06-30
  • 2013-06-01
  • 1970-01-01
相关资源
最近更新 更多