如何使用 javaFX 包含多个 SplitPanes,包括 children?
How can I have multiple SplitPanes including children with javaFX?
我的结构:
AnchorPane
|
ScrollPane
|
FlowPane
|
SplitPane
|
StackedBarChart
TableView
如何复制我的 SplitPane
及其 children 以获得 未知数 的重复项?
如何为每个副本设置新的变量名?
在 Scene Builder 中,我右键单击 SplitPane
和 select Duplicate
。但是,问题是重复的数量在编写程序之前是不可用的,直到运行时才知道。
最好的方法是为 SplitPane
创建一个单独的 FXML,根据需要多次加载它并将其添加到您的 FlowPane
.
让我们将新的 fxml 命名为 split.fxml
。称为 SplitController
的 fxml 控制器
然后,我们将根据需要多次加载此 fxml,获取根并将其添加到 FlowPane
,现在存在于单独的 fxml 中。
让我们将主 fxml 称为 container.fxml
。称为 ContainerController
的 fxml 控制器
回答您的一些问题:
How can I, duplicate my SplitPane with its children for a unknown numbers of duplicates?
只需根据需要多次加载 fxml。
How can i set for each copy new variable names?
你有控制器,你可以用一个变量来识别它。您还可以为拆分窗格设置 class
或 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>
SplitController.java
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.StackedBarChart;
import javafx.scene.chart.XYChart;
import javafx.scene.control.SplitPane;
import javafx.scene.control.TableView;
import java.net.URL;
import java.util.ResourceBundle;
public class SplitController implements Initializable{
@FXML
private SplitPane splitPane;
@FXML
private StackedBarChart<String, Number> stackedBarChart;
@FXML
private TableView<String> tableView;
@FXML
private CategoryAxis categoryAxis;
@FXML
private NumberAxis numberAxis;
private ObservableList<String> tableList;
private ObservableList<Integer> chartValueList = FXCollections.observableArrayList();
public void setTableList(ObservableList<String> tableList) {
this.tableList = FXCollections.observableList(tableList);
}
public void setChartValueList(ObservableList<Integer> chartValueList) {
this.chartValueList = FXCollections.observableList(chartValueList);
}
@Override
public void initialize(URL location, ResourceBundle resources) {
}
public void setChart(){
XYChart.Series<String,Number> series1 = new XYChart.Series();
series1.getData().add(new XYChart.Data("January", chartValueList.get(0)));
series1.getData().add(new XYChart.Data("February", chartValueList.get(1)));
series1.getData().add(new XYChart.Data("March", chartValueList.get(2)));
series1.getData().add(new XYChart.Data("April", chartValueList.get(3)));
XYChart.Series<String,Number> series2 = new XYChart.Series();
series2.getData().add(new XYChart.Data("January", chartValueList.get(0)));
series2.getData().add(new XYChart.Data("February", chartValueList.get(1)));
series2.getData().add(new XYChart.Data("March", chartValueList.get(2)));
series2.getData().add(new XYChart.Data("April", chartValueList.get(3)));
stackedBarChart.getData().addAll(series1, series2);
}
}
container.fxml
<?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>
ContainerController.java
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);
}
}
}
Main.java - 加载容器,将其设置为舞台并显示
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);
}
}
截图
注意 - TableView
是空的,因为根据问题,我认为填写它并不重要。
有效:
SplitPane sp=new SplitPane(splitPane);
sp.setId(machine);
flowPane.getChildren().add(new SplitPane(sp));
我的结构:
AnchorPane
|
ScrollPane
|
FlowPane
|
SplitPane
|
StackedBarChart
TableView
如何复制我的 SplitPane
及其 children 以获得 未知数 的重复项?
如何为每个副本设置新的变量名?
在 Scene Builder 中,我右键单击 SplitPane
和 select Duplicate
。但是,问题是重复的数量在编写程序之前是不可用的,直到运行时才知道。
最好的方法是为 SplitPane
创建一个单独的 FXML,根据需要多次加载它并将其添加到您的 FlowPane
.
让我们将新的 fxml 命名为 split.fxml
。称为 SplitController
然后,我们将根据需要多次加载此 fxml,获取根并将其添加到 FlowPane
,现在存在于单独的 fxml 中。
让我们将主 fxml 称为 container.fxml
。称为 ContainerController
回答您的一些问题:
How can I, duplicate my SplitPane with its children for a unknown numbers of duplicates?
只需根据需要多次加载 fxml。
How can i set for each copy new variable names?
你有控制器,你可以用一个变量来识别它。您还可以为拆分窗格设置 class
或 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>
SplitController.java
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.StackedBarChart;
import javafx.scene.chart.XYChart;
import javafx.scene.control.SplitPane;
import javafx.scene.control.TableView;
import java.net.URL;
import java.util.ResourceBundle;
public class SplitController implements Initializable{
@FXML
private SplitPane splitPane;
@FXML
private StackedBarChart<String, Number> stackedBarChart;
@FXML
private TableView<String> tableView;
@FXML
private CategoryAxis categoryAxis;
@FXML
private NumberAxis numberAxis;
private ObservableList<String> tableList;
private ObservableList<Integer> chartValueList = FXCollections.observableArrayList();
public void setTableList(ObservableList<String> tableList) {
this.tableList = FXCollections.observableList(tableList);
}
public void setChartValueList(ObservableList<Integer> chartValueList) {
this.chartValueList = FXCollections.observableList(chartValueList);
}
@Override
public void initialize(URL location, ResourceBundle resources) {
}
public void setChart(){
XYChart.Series<String,Number> series1 = new XYChart.Series();
series1.getData().add(new XYChart.Data("January", chartValueList.get(0)));
series1.getData().add(new XYChart.Data("February", chartValueList.get(1)));
series1.getData().add(new XYChart.Data("March", chartValueList.get(2)));
series1.getData().add(new XYChart.Data("April", chartValueList.get(3)));
XYChart.Series<String,Number> series2 = new XYChart.Series();
series2.getData().add(new XYChart.Data("January", chartValueList.get(0)));
series2.getData().add(new XYChart.Data("February", chartValueList.get(1)));
series2.getData().add(new XYChart.Data("March", chartValueList.get(2)));
series2.getData().add(new XYChart.Data("April", chartValueList.get(3)));
stackedBarChart.getData().addAll(series1, series2);
}
}
container.fxml
<?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>
ContainerController.java
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);
}
}
}
Main.java - 加载容器,将其设置为舞台并显示
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);
}
}
截图
注意 - TableView
是空的,因为根据问题,我认为填写它并不重要。
有效:
SplitPane sp=new SplitPane(splitPane);
sp.setId(machine);
flowPane.getChildren().add(new SplitPane(sp));