如何在netbeans中向面板添加Jfreechart(饼图) [英] How to add a Jfreechart(pie chart) to a panel in netbeans

查看:182
本文介绍了如何在netbeans中向面板添加Jfreechart(饼图)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用netbeans gui编辑器,我试图添加一个本身在内部框架中的Jfreechart,而这个内部框架我想将它添加到面板中,正如你在这张图片中看到的那样(抱歉,我无法发布图片直接因为我是新手):

Im using netbeans gui editor and im trying to add a Jfreechart that is itself in a internal frame, and this internal frame I am wanting to add it to a panel, as you can see in this image (sorry I cant post image directly because im a newbie):

http://www.flickr.com/photos/63259070@N06/6370734167/

内部框架甚至没有显示在面板Estadisticas,当我运行它,我认为它更难,因为我没有通过代码做gui但它不应该那么难,如果有人可以帮我正确添加这个我会非常感激它,这里是我的代码一直在尝试:

The internal frame doesn't even show up on the panel "Estadisticas" when I run it, I think its harder because im not doing the gui by code but it shouldn't be that hard, If anyone could help me add this properly I would greatly appreciate it, here is the code that I have been trying out:

 private void display() {
       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);
    this.ji.add(cp); //ji is the name of the internal frame
    this.ji.pack();
    this.ji.setVisible(true);
    this.ji.setSize(100, 100);

    JDesktopPane dtp = new JDesktopPane();
    dtp.add(ji);
    this.jpEstadisticas.add(dtp);   //jpEstadisticas the name of the main "Estadisticas"panel

}


推荐答案

不要将图表面板添加到主框架中,而是将其添加到其内容窗格中。
替换this.ji.add(cp); this.ji.getContentPane()。add(cp)

Don't add the chart panel into the main frame, add it to its content pane instead. replace this.ji.add(cp); by this.ji.getContentPane().add(cp)

或更好:
在NetBeans中,在GUI编辑器(不是代码编辑器)下添加一个面板到主框架,并称之为 chartPanel 。添加要显示的所有其他控件并根据需要定位它们。完成后,切换回代码编辑器。在主框架的构造函数上,执行以下操作:

Or better: In NetBeans, under the GUI editor (not code editor) add a panel into your main frame and call it something like chartPanel. Add all other controls you want to display and position them as you like. Once done, switch back to code editor. On the main frame's constructor, do the following:

// Inside the initializeComponents() method 
// Find and replace 
chartPanel = new JPanel(); 
// By 
chartPanel = createChartPanel();

// Create chart panel method
public JPanel createChartPanel(){
    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);
    return new ChartPanel(chart);
}

这篇关于如何在netbeans中向面板添加Jfreechart(饼图)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆