使饼图在 JPanel 中可见

Making a PieChart visible in JPanel

我对编程还很陌生,为了上学,我必须创建一个包含一定数量 类 的程序。目前一切进展顺利,但我被困在数据的图形输出上。该程序是一个费用计算器。它是一个分为 6 个 JPanel 的框架。其中一个应该是我创建的 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();
    }
}

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

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

提前致谢

因为您正在使用 JavaFX PieChart in a Swing application, you'll need to enclose the chart in a JFXPanel. Instead of extending JPanel, follow the outline shown here. A complete example that displays a JavaFX Label is shown 。顺便说一句,您的片段永远不会将 pieChart 添加到布局中;也许尝试这样的事情:

root.setCenter(pieChart);

或者,使用 to your layout; a complete example, included in the distribution, is cited 添加 ChartPanel

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

我用空面板测试了这个,饼图除外,它成功了!