简体   繁体   English

Javafx-如何将饼图从不同的类添加到BorderPane

[英]Javafx - How to Add Pie Chart to BorderPane from Different Class

I have two classes. 我有两节课。 A Main class and a Chart class. 主类和图表类。 My Chart class extends PieChart and instantiates a Pie Chart in its constructor. 我的Chart类扩展了PieChart并在其构造函数中实例化了一个饼图。 My main class creates a BorderPane, I then instantiate an object of the Chart class(creating the Pie Chart) and add it to the center pane.The only problem is I don't know the exact syntax to make this work properly, specifically in the constructor of my Chart class. 我的主类创建一个BorderPane,然后实例化Chart类的一个对象(创建饼图)并将其添加到中心窗格中。唯一的问题是我不知道使它正常工作的确切语法,特别是在我的Chart类的构造函数。

I know that for example, if I was creating an HBox instead of a Pie Chart in the constructor, I could simply use this. 我知道例如,如果我在构造函数中创建的是HBox而不是饼图,则可以简单地使用它。 and a number of methods. 和许多方法。 But Pie Charts have additional elements with Observable Lists so i'm not sure how to make this work? 但是饼形图还有带有可观察列表的其他元素,因此我不确定如何使它工作? Here are my two classes below. 这是我下面的两节课。 Please help. 请帮忙。 Thank you. 谢谢。

MAIN CLASS 主班

public class Main extends Application {
@Override
public void start(Stage primaryStage) {
    BorderPane border = new BorderPane();

    Chart chart = new Chart();

    border.setCenter(chart);

    Scene scene = new Scene (border, 640,680);
    primaryStage.setScene(scene);
    primaryStage.show();

}
public static void main(String[] args) {
    launch(args);
}}

CHART CLASS 图表类

public class Chart extends PieChart{

public Chart(){
    ObservableList<PieChart.Data>pieChart = FXCollections.observableArrayList(
            new PieChart.Data("Fruits", 75),
            new PieChart.Data("Vegetables", 25)
            );

    PieChart chart = new PieChart(pieChart);

    //What code can create the Pie Chart here? Is the solution to place the chart on it's own pane instead?
}}

You are probably looking for 您可能正在寻找

public Chart(){
    ObservableList<PieChart.Data>pieChart = FXCollections.observableArrayList(
            new PieChart.Data("Fruits", 75),
            new PieChart.Data("Vegetables", 25)
            );
    setData(pieChart);
    // no need for this -> PieChart chart = new PieChart(pieChart);
}

or 要么

public Chart(){
   super(FXCollections.observableArrayList(
         new PieChart.Data("Fruits", 75),
         new PieChart.Data("Vegetables", 25)
   ));
}

Edit after comment: setData(ObservableList data) is a method from PieChart class. 注释后编辑: setData(ObservableList data)是PieChart类的方法。 And it will set data objects. 它将设置数据对象。 It is just easy and convenient way to work with data in chart or any other collection powered view. 这是使用图表或任何其他基于集合的视图中的数据的简便方法。 (Yep, it is not chart-only style . It is pretty much the same for all JavaFX views. Chart will listen for data changes and redraw itself internaly.You just need to set data you'd like to display. In your case you have this method because your Chart extends PieChart (是的,它不是仅图表样式 。对于所有JavaFX视图,它几乎都是相同的。图表将侦听数据更改并在内部重绘其自身。您只需要设置要显示的数据即可。在这种情况下,您可以使用此方法,因为您的Chart extends PieChart

When you do PieChart charts = new PieChart(data); 当您做PieChart charts = new PieChart(data); in PieChart it will call setData internaly 在PieChart中,它将内部调用setData

public PieChart(ObservableList<PieChart.Data> data){
   setData(data);
   ...
}

So that's why you have to call super(data) or setData(data) yourself in your extended class. 这就是为什么您必须在扩展类中自己调用super(data)或setData(data)的原因。 But if you want to chage charts, you don't have to recreate whole chart every time. 但是,如果您想更改图表,则不必每次都重新创建整个图表。 Can just change data with charts.setData(someData) or get data List object with charts.getData() and then change it. 可以只需更改与数据charts.setData(someData)或获取数据列表对象charts.getData()然后改变它。

For example: 例如:

PieChart charts = new PieChart();
ObservableList<PieChart.Data> pieChart = ...
charts.setData(pieChart);
// pieChart == charts.getData() // this is the same object. so
pineChart.add(new PieChart.Data("New Fruit", 75)) // will change chart

Btw, take a look at this tutorials https://docs.oracle.com/javafx/2/charts/jfxpub-charts.htm 顺便说一句,看看这个教程https://docs.oracle.com/javafx/2/charts/jfxpub-charts.htm

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

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