Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
101 views
in Technique[技术] by (71.8m points)

java - 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);
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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,

InternalPie

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();
            }
        });
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...