加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

在javafx中stackpane和root有什么区别?

发布时间:2020-12-15 04:13:11 所属栏目:Java 来源:网络整理
导读:我正在为 javafx练习做饼图.以下是开发饼图的代码.如果我使用Group和StackPane,我发现输出没有区别.我已经评论了Group部分.只是在两者之间徘徊. import javafx.application.Application;import javafx.collections.FXCollections;import javafx.collections.
我正在为 javafx练习做饼图.以下是开发饼图的代码.如果我使用Group和StackPane,我发现输出没有区别.我已经评论了Group部分.只是在两者之间徘徊.

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.chart.PieChart;
import javafx.scene.chart.PieChart.Data;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class ChartApp1 extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
PieChart pieChart = new PieChart();
//Group p=new Group();

pieChart.setData(getChartData());
primaryStage.setTitle("PieChart");
StackPane root = new StackPane();
root.getChildren().add(pieChart);
//p.getChildren().add(pieChart);
primaryStage.setScene(new Scene(root,400,250));
primaryStage.show();
}
private ObservableList<PieChart.Data> getChartData() {
ObservableList<PieChart.Data> answer = FXCollections.observableArrayList();
answer.addAll(new PieChart.Data("java",17.56),new PieChart.Data("C",17.06),new PieChart.Data("C++",8.25),new PieChart.Data("C#",8.20),new PieChart.Data("ObjectiveC",6.8),new PieChart.Data("PHP",6.0),new PieChart.Data("(Visual)Basic",4.76),new PieChart.Data("Other",31.37));
return answer;
}
}

解决方法

根据官方 documentation,

StackPane lays out its children in a back-to-front stack. The z-order of the children is defined by the order of the children list
with the 0th child being the bottom and last child on top. If a border
and/or padding have been set,the children will be layed out within
those insets.

The stackpane will attempt to resize each child to fill its
content area. If the child could not be sized to fill the stackpane
(either because it was not resizable or its max size prevented it)
then it will be aligned within the area using the alignment
property,which defaults to Pos.CENTER

虽然集团类官方documentation表示

A Group node contains an ObservableList of children that are
rendered in order whenever this node is rendered. A Group will take on the collective bounds of its children and is not directly
resizable.

Any transform,effect,or state applied to a Group will be applied to
all children of that group. Such transforms and effects will NOT be
included in this Group’s layout bounds,however if transforms and
effects are set directly on children of this Group,those will be
included in this Group’s layout bounds.

By default,a Group will “auto-size” its managed resizable children to their preferred sizes during the layout pass to ensure that Regions and Controls are sized properly as their state changes. If an application needs to disable this auto-sizing behavior,then it should set autoSizeChildren to false and understand that if the preferred size of the children change,they will not automatically resize.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读