使饼图在 JPanel 中可见 [英] Making a PieChart visible in JPanel

查看:59
本文介绍了使饼图在 JPanel 中可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对编程还很陌生,对于学校,我必须创建一个包含一定数量课程的程序.目前一切进展顺利,但我一直停留在数据的图形输出上.该程序是一个费用计算器.它是一个框架,分为 6 个 JPanel.其中之一应该是我创建的 Piechart.饼图有效,但不会显示在我的框架中.所以我被困在如何将正确的内容调用到面板上.我要添加饼图的面板的代码是:

I'm fairly new to programming and for school I have to create a program with a certain amount of classes. It's all going quite well for now, but I'm stuck on the graphic output of my data. The program is an expenses calculator. It is an frame devided in 6 JPanels. one of them should be a Piechart I created. The piechart works but won't show in my Frame. So I'm stuck on how to call in the right content to the panel. The code of the panel where I want to add the piechart is :

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();
    }
}

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

The output of the code so far is :

如果有人能帮助我,我会很高兴:)

If someone could help me I would be very happy:)

提前致谢

推荐答案

这样修改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);
}
}

我用空面板测试了这个,除了饼图,它奏效了!

I tested this with empty panels, except for the PieChart, and it worked!

这篇关于使饼图在 JPanel 中可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆