饼图到 JPanel netbeans

Pie charts to a JPanel netbeans

我正在尝试向我的 ui 添加一些图形图表 ui 有一个单独的按钮来查看饼图,当它单击时图表应该出现在面板中 我试过这个代码

DefaultPieDataset defaultPieDataset = new DefaultPieDataset();
        defaultPieDataset.setValue("Monday", new Integer(0));
        defaultPieDataset.setValue("Tuesday", new Integer(2));
        defaultPieDataset.setValue("Wednesday", new Integer(1));
        defaultPieDataset.setValue("Thursday", new Integer(2));
        defaultPieDataset.setValue("Friday", new Integer(0));
        defaultPieDataset.setValue("Saturday", new Integer(3));
        defaultPieDataset.setValue("Sunday", new Integer(2));
        JFreeChart chart = ChartFactory.createPieChart(Daily Usage", defaultPieDataset, true, true, true);
        PiePlot p = (PiePlot)chart.getPlot();
        p.setDirection(Rotation.CLOCKWISE);
        ChartFrame frame = new ChartFrame("Daily Usage", chart);
        frame.setVisible(true);
        frame.setSize(500,500);
        frame.setLocationRelativeTo(null);

但这使用的是框架而不是面板,有什么方法可以在 jpanel 中显示它。我没有使用任何框架

您可以将图表添加到 ChartPanel,而不是框架,您将拥有 JPanel

这是 ChartPanel 内的 JFrame 的完整示例,其中还包含典型的 JPanel:

public class ChartPanelExample extends JFrame {
    public ChartPanelExample() {
        super("");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        JPanel topPanel = new JPanel(new FlowLayout());

        JButton button = new JButton("Something");
        topPanel.add(button);

        JLabel label = new JLabel("This is another JPanel");
        topPanel.add(label);

        add(topPanel, BorderLayout.PAGE_START);

        DefaultPieDataset defaultPieDataset = new DefaultPieDataset();
        defaultPieDataset.setValue("Monday", new Integer(0));
        defaultPieDataset.setValue("Tuesday", new Integer(2));
        defaultPieDataset.setValue("Wednesday", new Integer(1));
        defaultPieDataset.setValue("Thursday", new Integer(2));
        defaultPieDataset.setValue("Friday", new Integer(0));
        defaultPieDataset.setValue("Saturday", new Integer(3));
        defaultPieDataset.setValue("Sunday", new Integer(2));
        JFreeChart chart = ChartFactory.createPieChart("Daily Usage", defaultPieDataset, true, true, true);
        PiePlot p = (PiePlot) chart.getPlot();
        p.setDirection(Rotation.CLOCKWISE);

        ChartPanel chartPanel = new ChartPanel(chart);

        add(chartPanel, BorderLayout.CENTER);

        setLocationByPlatform(true);
        pack();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new ChartPanelExample().setVisible(true));
    }
}