将JFrame放在jPanel中

时间:2014-02-25 18:45:38

标签: java swing jfreechart

我试图使用名为“jfreechart”的库来绘制我的饼图。这里的问题是我无法整合结果witsh在我的窗口中包含2个JPanel的不同Jframe中。最后“Statistiques”应该包含不在另一个窗口中的饼图。 enter image description here

这是饼图的类

    public class PieChart extends JFrame {

  private static final long serialVersionUID = 1L;

  public PieChart(String applicationTitle, String chartTitle) {
        super(applicationTitle); 
        PieDataset dataset = createDataset();

        JFreeChart chart = createChart(dataset, chartTitle);

        ChartPanel chartPanel = new ChartPanel(chart);


        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));


        setContentPane(chartPanel);

    }


    private  PieDataset createDataset() {
        DefaultPieDataset result = new DefaultPieDataset();
        result.setValue("Tunis", 29);
        result.setValue("Ariana", 20);
        result.setValue("Sousse", 51);
        return result;

    }


    private JFreeChart createChart(PieDataset dataset, String title) {

  JFreeChart chart = ChartFactory.createPieChart3D(title,dataset,true,true,false);

        PiePlot3D plot = (PiePlot3D) chart.getPlot();
        plot.setStartAngle(290);
        plot.setDirection(Rotation.CLOCKWISE);
        plot.setForegroundAlpha(0.5f);
        return chart;

    }
} 

1 个答案:

答案 0 :(得分:3)

JFrame放在JPanel中是不可能的,但您可以将饼图放在JPanel中。

PieDataset dataset = createDataset();

JFreeChart chart = createChart(dataset, chartTitle);

ChartPanel chartPanel = new ChartPanel(chart);

chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));

yourPanel.add(chartPanel);     // this line is new