在JPanel中显示PieChart

时间:2017-10-23 10:56:57

标签: java swing jpanel pie-chart

我是编程和学校的新手,我必须创建一个包含一定数量的课程。现在一切都很顺利,但我仍然停留在数据的图形输出上。该计划是费用计算器。它是一个分为6个JPanels的框架。其中一个应该是我创建的Piechart。饼图有效,但不会显示在我的框架中。所以我坚持如何在面板中调用正确的内容。我要添加饼图的面板代码是:

class CenterPaneel extends JPanel{
    private static final long serialVersionUID = -835526987955952967L;

    public CenterPaneel(){
        setLayout( new GridLayout(2,3,10,10));
        add( new Catagorie1Paneel() );                add( new Catagorie3Paneel() );           add( new SamenvattingPaneel() );
        add( new Catagorie2Paneel() );                add( new Catagorie4Paneel() );           add( new DataPaneel());
    }

饼图的代码是:

    class DataPaneel extends JPanel{
    private static final long serialVersionUID = -2980457707385367851L;
    private final ObservableList<PieChart.Data> details = FXCollections.observableArrayList();
    private BorderPane root;
    private PieChart pieChart;

    public void start(Stage primaryStage) {
        primaryStage.setTitle("test");
        details.addAll(new PieChart.Data("test1", 25),
                new PieChart.Data("test 2", 25),
                new PieChart.Data("test 3", 25),
                new PieChart.Data("test 4", 25)
            );


        root = new BorderPane();
        Scene scene = new Scene(root,600,500);

        pieChart = new PieChart();
        pieChart.setData(details);
        pieChart.setTitle("test");
        pieChart.setLegendSide(Side.BOTTOM);
        pieChart.setLabelsVisible(true);

        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

到目前为止,代码的输出是:

enter image description here

如果有人可以帮助我,我会非常高兴:)

提前致谢

2 个答案:

答案 0 :(得分:2)

当您在Swing应用程序中使用JavaFX PieChart时,您需要将图表括在JFXPanel中。请按照显示的here大纲,而不是展开JPanel。显示JavaFX Label的完整示例显示为here。另外,您的片段永远不会将pieChart添加到布局中;也许尝试这样的事情:

root.setCenter(pieChart);

或者,使用为您的布局添加ChartPanel;包含在发行版中的完整示例引用here

答案 1 :(得分:2)

enter image description here

以这种方式修改CenterPaneel的构造函数:

   import javafx.collections.FXCollections;
   import javafx.collections.ObservableList;
   import javafx.embed.swing.JFXPanel;
   import javafx.geometry.Side;
   import javafx.scene.Scene;
   import javafx.scene.chart.PieChart;
   import javafx.scene.control.ScrollPane;
   import javax.swing.JFrame;
   import javax.swing.JPanel;

public class Pie_Chart extends JFrame {

public static void main(String[] args) {
    Pie_Chart pc=new Pie_Chart();
    pc.getContentPane().add(new CenterPaneel());
    pc.setVisible(true);
}
}


class CenterPaneel extends JPanel{
private final static ObservableList<PieChart.Data> details =   FXCollections.observableArrayList();
private static PieChart pieChart;
    public CenterPaneel(){
    setLayout( new GridLayout(2,3,10,10));


    final JFXPanel dataPaneel = new JFXPanel();

    ScrollPane sp = new ScrollPane();

    details.addAll(new PieChart.Data("test1", 25),
            new PieChart.Data("test 2", 25),
            new PieChart.Data("test 3", 25),
            new PieChart.Data("test 4", 25)
        );
    pieChart = new PieChart();
    pieChart.setData(details);
    pieChart.setTitle("test");
    pieChart.setLegendSide(Side.BOTTOM);
    pieChart.setLabelsVisible(true);
    sp.setContent(pieChart);

    Scene scene = new Scene(sp, 600, 500);
    dataPaneel.setScene(scene);

    add( new Catagorie1Paneel() );                add( new Catagorie3Paneel() );           add( new SamenvattingPaneel() );
    add( new Catagorie2Paneel() );                add( new Catagorie4Paneel() );           add( dataPaneel);
}
}

我用空面板测试了这个,除了PieChart,它还有效!