简体   繁体   English

JavaFX和在场景生成器中创建的FXML的使用

[英]JavaFX and the use of FXML created in scene builder

I have been trying to figure out how to use two FXML files and their controllers at the same time in a program, but have found it difficult to find a simple example. 我一直试图找出如何在一个程序中同时使用两个FXML文件及其控制器,但发现很难找到一个简单的示例。 Would someone please demonstrate the use of Sample.fxml and Sample1.fxml at the same time, to where both are displayed. 有人可以在同时显示Sample.fxml和Sample1.fxml的地方演示它们的用法。 If you would be able to demonstrate this in the simplest and easiest possible way for a new Java and JavaFX programer to understand, I would be very great full. 如果您能够以最简单,最简单的方式来证明这一点,以使新的Java和JavaFX编程人员能够理解,那么我将非常满意。 Thanks. 谢谢。

You can solve this problem by adding both the fxml files into a single group, and then you have to add the group to the scene. 您可以通过将两个fxml文件都添加到一个组中来解决此问题,然后必须将该组添加到场景中。 It is a little more work if you want to make everything look polished, but this the most simple way to do this. 如果要使所有内容看起来更加优美,还需要做更多的工作,但这是最简单的方法。 Just be sure your controllers are defined in the fxml files. 只要确保在fxml文件中定义了控制器即可。

    GridPane root = new GridPane();
    root.add((Node)(FXMLLoader.load(getClass().getResource("sample1.fxml"))) , 1 , 1);
    root.add((Node)(FXMLLoader.load(getClass().getResource("sample2.fxml"))) , 1 , 2);
    primaryStage.setTitle("Two For One Special");
    primaryStage.setScene(new Scene(root, 800, 600));
    primaryStage.show();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM