本文介绍了如何修复此代码以便我可以将此 JFreeChart 添加到面板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

使用您的 chart 创建一个 ChartPanel 并将 ChartPanel 添加到 JInternalFrame.将 JInternalFrame 添加到 JDesktopPane.请参阅如何使用内部框架了解更多.

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.

附录:例如,

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

这篇关于如何修复此代码以便我可以将此 JFreeChart 添加到面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 00:58