如何使用javaFX创建多个拆分窗格(包括子窗格)?

如何使用javaFX创建多个拆分窗格(包括子窗格)?,javafx,duplicates,containers,nodes,children,Javafx,Duplicates,Containers,Nodes,Children,我的结构: AnchorPane | ScrollPane | FlowPane | SplitPane | StackedBarChart TableView 如何将我的SplitPane及其子项复制到未知数量的副本 如何为每个副本设置新的变量名 在场景生成器中,我右键单击拆分窗格,然后选择复制。但是,问题是,在编写程序之前,副本的数量是不可用的,并且在运行时才知道副本的数量。实现这一点的最佳方法是为拆分窗格创建一个单独的FXML,根据需要加

我的结构:

AnchorPane
 | 
 ScrollPane
  |
  FlowPane
   |
   SplitPane
    |
    StackedBarChart
    TableView
如何将我的
SplitPane
及其子项复制到未知数量的副本

如何为每个副本设置新的变量名


在场景生成器中,我右键单击
拆分窗格
,然后选择
复制
。但是,问题是,在编写程序之前,副本的数量是不可用的,并且在运行时才知道副本的数量。

实现这一点的最佳方法是为
拆分窗格创建一个单独的FXML,根据需要加载多次,然后将其添加到
流程窗格中

让我们将新的fxml称为
split.fxml
。fxml的控制器称为
SplitController

然后,我们将根据需要多次加载此fxml,获取根目录并将其添加到
流程窗格
,它现在位于单独的fxml中

让我们将主fxml称为
container.fxml
。fxml的控制器称为
ContainerController

回答您的一些问题:

我如何复制拆分窗格及其子项以获得未知数量的副本

只要根据需要多次加载fxml即可

如何为每个副本设置新的变量名

你有控制器,你可以用一个变量来识别它。您还可以在拆分窗格中设置
id

完整的代码

split.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.chart.CategoryAxis?>
<?import javafx.scene.chart.NumberAxis?>
<?import javafx.scene.chart.StackedBarChart?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>

<SplitPane fx:id="splitPane" dividerPositions="0.25" prefHeight="363.0" prefWidth="773.0" fx:controller="SplitController"  xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
    <items>
        <TableView fx:id="tableView" prefHeight="458.0" prefWidth="251.0">
            <columns>
                <TableColumn prefWidth="75.0" text="C1" />
                <TableColumn prefWidth="75.0" text="C2" />
            </columns>
        </TableView>
        <StackedBarChart fx:id="stackedBarChart" prefHeight="210.0" prefWidth="107.0">
            <xAxis>
                <CategoryAxis fx:id="categoryAxis" side="BOTTOM" label="Year" />
            </xAxis>
            <yAxis>
                <NumberAxis fx:id="numberAxis" side="LEFT" label="Value"/>
            </yAxis>
        </StackedBarChart>
    </items>
</SplitPane>
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.FlowPane?>

<BorderPane xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ContainerController">
   <center>
        <ScrollPane fx:id="scrollPane" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" BorderPane.alignment="CENTER">
            <content>
                <FlowPane fx:id="flowPane" maxHeight="-Infinity" maxWidth="-Infinity" prefHeight="353.0" prefWidth="745.0">

                </FlowPane>
            </content>
        </ScrollPane>
   </center>
</BorderPane>
屏幕截图

<强>注释< /强> -<代码> TabelVIEW/COD>是空的,因为我认为按问题填充是不重要的。

< P>工作:

    SplitPane sp=new SplitPane(splitPane);
    sp.setId(machine);
    flowPane.getChildren().add(new SplitPane(sp));

我还是不明白你的问题。您所说的“复制我的拆分窗格以获得未知数量的副本”是什么意思?我有一个包含StackedBarChart和TableView的拆分窗格。例如,我将在it中显示机器数据。一个图表和表格视图中的一台机器数据。现在我想复制第一个SplitPane及其子项,以显示下一个机器数据。机器的数量是可变的。是的,我知道。但是您是否只想将
StackedBarChart
添加到拆分窗格的一部分,而将
TableView
添加到另一部分?或者,要求是不同的?将条形图堆叠到拆分窗格的一部分,另一部分堆叠到TableView。我认为Gerhard实际上试图做的是在一个场景中创建整个场景的多个实例,使用fxml作为模板。例如,他有一个机器列表(他在运行时知道机器的数量,但在设计GUI时不知道),他想为每台机器显示一个单独的图表。但我可能错了,也许格哈德可以发表评论来澄清。我错过了,因为它后来被编辑了。让我编辑我的答案。是的,我想为每台机器显示一个单独的图表。(我在运行时知道的数目)我发现:****不起作用:flowPane.getChildren().add(新的拆分窗格(拆分窗格.setId(机器));****Works:flowPane.getChildren().add(新建拆分窗格(拆分窗格))@GerhardLaib我已经编辑了我的答案,请看一看,如果您还有其他问题,请告诉我。
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.SplitPane;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.FlowPane;

import java.io.IOException;
import java.net.URL;
import java.util.Random;
import java.util.ResourceBundle;

public class ContainerController implements Initializable{

    @FXML
    private AnchorPane anchorPane;

    @FXML
    private ScrollPane scrollPane;

    @FXML
    private FlowPane flowPane;

    private final int NUMBER_OF_COPIES = 5;
    private final int NUMBER_OF_ROWS = 10;
    private ObservableList<String> tableList = FXCollections.observableArrayList();
    private ObservableList<Integer> chartValueList = FXCollections.observableArrayList();


    @Override
    public void initialize(URL location, ResourceBundle resources) {
        try {
            for(int i=0; i<NUMBER_OF_COPIES; i++) {
                FXMLLoader loader = new FXMLLoader(getClass().getResource("/split.fxml"));
                SplitPane pane = loader.load();
                SplitController controller = loader.getController();
                fillChartValueList();
                controller.setChartValueList(chartValueList);
                controller.setChart();
                flowPane.getChildren().add(pane);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void fillChartValueList(){

        // clear previous values
        chartValueList.clear();

        Random rand = new Random();
        int min = 1000;
        int max = 8000;


        for(int i=0; i<NUMBER_OF_ROWS; i++) {
            int randomNum = rand.nextInt((max - min) + 1) + min;
            chartValueList.add(randomNum);
        }
    }
}
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.io.IOException;

public class Main extends Application {

    public Main() {
        super();
    }

    @Override
    public void start(Stage primaryStage) throws IOException{

        FXMLLoader loader = new FXMLLoader(getClass().getResource("/container.fxml"));
        Parent parent = loader.load();
        ContainerController controller = loader.getController();

        Scene scene = new Scene(parent, 600, 600);
        Stage stage = new Stage();
        stage.setScene(scene);
        stage.show();

    }

    public static void main(String[] args) {
        launch(args);
    }
}
    SplitPane sp=new SplitPane(splitPane);
    sp.setId(machine);
    flowPane.getChildren().add(new SplitPane(sp));