如何修复此代码,以便将此JFreeChart添加到面板中

时间:2011-11-20 05:58:31

标签: java swing user-interface jfreechart

我正在尝试制作一个专门嵌入JInternalFrame面板中的图表;它是来自图书馆JFreeChart的饼图。我想将图表嵌入到变量名为jpPaneles的面板上,但事实证明这是不可能的。这对我的项目来说非常重要,所以如果有人有时间帮助我,我会非常感激。我在NetBeans GUI编辑器中工作。这是代码,您可以看到我尝试将frame1添加到面板。

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

1 个答案:

答案 0 :(得分:8)

使用chart创建ChartPanel并将ChartPanel添加到JInternalFrame。将JInternalFrame添加到JDesktopPane。有关详情,请参阅How to Use Internal Frames

附录:例如,

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