简体   繁体   中英

Put a JFrame in a jPanel

I tried to use a Library called "jfreechart" to draw my Pie Chart.The problem here is that i can't integrate the result witsh is in a different Jframe in my window that contains 2 JPanels.So finally " Statistiques " should contains the Pie Chart not in a different window. 在此处输入图片说明

Here is the class of the Pie Chart

    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;

    }
} 

Putting a JFrame in a JPanel is afaik not possible, but you can put the pie chart in a 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM