简体   繁体   中英

How can I fix this code so I can add this JFreeChart to a panel

I am trying to make a chart that is embedded specifically in a panel on a JInternalFrame ; it is a pie chart from the library JFreeChart . I want to embed the chart on a panel which goes by the variable name jpPaneles , but it has proven to be impossible. This is really crucial for my project, so if anyone has the time to help me out, I would greatly appreciate it. I am working in NetBeans GUI editor. Here is the code and you can see I try to add frame1 to a panel.

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.data.general.DefaultPieDataset;

public class Eventad extends javax.swing.JInternalFrame {

    public Eventad() {
        initComponents();
    }

    public void updateChart() {
    }

    public static void main(String arg[]) {
        DefaultPieDataset pieDataset = new DefaultPieDataset();
        pieDataset.setValue("One", new Integer(10));
        pieDataset.setValue("Two", new Integer(20));
        pieDataset.setValue("Three", new Integer(30));
        pieDataset.setValue("Four", new Integer(10));
        pieDataset.setValue("Five", new Integer(20));
        pieDataset.setValue("Six", new Integer(10));
        JFreeChart chart = ChartFactory.createPieChart3D(
            "3D Pie Chart", pieDataset, true, true, true);
        PiePlot3D p = (PiePlot3D) chart.getPlot();
        p.setForegroundAlpha(0.5f);

        ChartFrame frame1 = new ChartFrame("3D Pie Chart", chart);
        frame1.setVisible(true);
        frame1.setSize(200, 200);
        //Here im trying to add the frame1 to the Jpanel
        this.jpPaneles.add(frame1);
    }
}

Use your chart to create a ChartPanel and add the ChartPanel to the JInternalFrame . Add the JInternalFrame to a JDesktopPane . See How to Use Internal Frames for more.

Addendum: For example,

内部派

import java.awt.EventQueue;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.data.general.DefaultPieDataset;

/** @see http://stackoverflow.com/questions/8199766 */
public class InternalPie {

    private void display() {
        JFrame f = new JFrame("InternalPie");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        DefaultPieDataset pieDataset = new DefaultPieDataset();
        pieDataset.setValue("One", new Integer(10));
        pieDataset.setValue("Two", new Integer(20));
        pieDataset.setValue("Three", new Integer(30));
        pieDataset.setValue("Four", new Integer(10));
        pieDataset.setValue("Five", new Integer(20));
        pieDataset.setValue("Six", new Integer(10));
        JFreeChart chart = ChartFactory.createPieChart3D(
            "3D Pie Chart", pieDataset, true, true, true);
        ChartPanel cp = new ChartPanel(chart);

        JInternalFrame jif = new JInternalFrame(
            "Chart", true, true, true, true);
        jif.add(cp);
        jif.pack();
        jif.setVisible(true);

        JDesktopPane dtp = new JDesktopPane();
        dtp.add(jif);
        f.add(dtp);
        f.pack();
        f.setSize(700, 500);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new InternalPie().display();
            }
        });
    }
}

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