如何使用JavaFX创建流窗格?
默认情况下,在实例化FlowPane类时,将创建一个水平流窗格,您可以使用setOrientation()方法更改其方向。您可以使用setPrefWrapLength()方法在高(垂直流窗格)或宽(水平流窗格)窗格中包裹节点。
要将节点添加到此窗格中,可以将其作为构造函数的参数传递,也可以将它们添加到窗格的可观察列表中,如下所示:
getChildren().addAll();
示例
import javafx.application.Application;import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.chart.AreaChart;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.BubbleChart;
import javafx.scene.chart.CategoryAxis;
import javafx.stage.Stage;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.PieChart;
import javafx.scene.chart.XYChart;
import javafx.scene.layout.FlowPane;
public class FlowPaneExample extends Application {
public void start(Stage stage) {
//Defining the X and Y axes
CategoryAxis xAxis = new CategoryAxis();
NumberAxis yAxis = new NumberAxis();
xAxis.setLabel("Months");
yAxis.setLabel("Rainfall (mm)");
//Creating the Area chart
AreaChart areaChart = new AreaChart(xAxis, yAxis);
XYChart.Series series = new XYChart.Series();
series.getData().add(new XYChart.Data("July", 169.9));
series.getData().add(new XYChart.Data("Aug", 178.7));
series.getData().add(new XYChart.Data("Sep", 158.3));
series.getData().add(new XYChart.Data("Oct", 97.2));
series.getData().add(new XYChart.Data("Nov", 22.4));
series.getData().add(new XYChart.Data("Dec", 5.9));
series.setName("Rainfall In Hyderabad");
//Setting data to the area chart
areaChart.getData().addAll(series);
areaChart.setPrefSize(290, 200);
//Defining the axes
CategoryAxis xAxis1 = new CategoryAxis();
xAxis.setLabel("Year");
NumberAxis yAxis1 = new NumberAxis();
yAxis.setLabel("Population (In millions)");
//Creating the Bar chart
BarChart barchart = new BarChart(xAxis1, yAxis1);
//Prepare XYChart.Series objects by setting data
XYChart.Series<String, Number> series1 = new XYChart.Series<>();
series1.setName("Asia");
series1.getData().add(new XYChart.Data<>("2012", 900));
series1.getData().add(new XYChart.Data<>("2013", 1000));
series1.getData().add(new XYChart.Data<>("2014", 1170));
series1.getData().add(new XYChart.Data<>("2015", 1250));
series1.getData().add(new XYChart.Data<>("2016", 1530));
series1.setName("Population of Asia");
//Setting the data to bar chart
barchart.getData().addAll(series1);
barchart.setPrefSize(290, 200);
//Creating a Pie chart
PieChart pieChart = new PieChart();
ObservableList<PieChart.Data> data = FXCollections.observableArrayList(
new PieChart.Data("Work", 10),
new PieChart.Data("Chores", 2),
new PieChart.Data("Sleep", 8),
new PieChart.Data("Others", 4));
pieChart.setData(data);
//Setting the other properties
pieChart.setTitle("Activities");
pieChart.setLabelLineLength(10);
pieChart.setPrefSize(290, 250);
NumberAxis xAxis2 = new NumberAxis(0, 100, 10);
NumberAxis yAxis2 = new NumberAxis(20, 100, 10);
//Creating labels to the axes
xAxis.setLabel("Age");
yAxis.setLabel("Weight");
BubbleChart bubbleChart = new BubbleChart(xAxis2, yAxis2);
XYChart.Series series2 = new XYChart.Series();
series2.getData().add(new XYChart.Data(10, 30, 4));
series2.getData().add(new XYChart.Data(25, 40, 5));
series2.getData().add(new XYChart.Data(40, 50, 6));
series2.getData().add(new XYChart.Data(55, 60, 8));
series2.getData().add(new XYChart.Data(70, 70, 9));
bubbleChart.getData().add(series2);
series2.setName("work");
bubbleChart.setPrefSize(290, 250);
//Creating a flow pane
FlowPane pane = new FlowPane();
pane.getChildren().addAll(areaChart, barchart, pieChart, bubbleChart);
//Setting the Scene
Scene scene = new Scene(pane, 600, 450);
stage.setTitle("Flow Pane");
stage.setScene(scene);
stage.show();
}
public static void main(String args[]){
launch(args);
}
}
输出结果
以上是 如何使用JavaFX创建流窗格? 的全部内容, 来源链接: utcz.com/z/327270.html