如何在netbeans中向面板添加Jfreechart(饼图)

时间:2011-11-20 18:21:42

标签: java swing user-interface jfreechart

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

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

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

 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

}

4 个答案:

答案 0 :(得分:1)

您没有将dtp添加到JFrame的内容窗格中。您可以使用NetBeans的UI编辑器。

答案 1 :(得分:0)

我认为以下代码适合您:

import org.jfree.chart.ChartPanel;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.ChartFactory;

public class PieChartJFrame extends javax.swing.JFrame {

/** Creates new form PieChartJFrame */
ChartPanel chartPanel;
public PieChartJFrame() {
    initComponents();
}

private PieDataset createPieDataSet() {

    DefaultPieDataset pieDataset = new DefaultPieDataset();
    pieDataset.setValue("Othes", new Integer(15));
    pieDataset.setValue("PHP", new Integer(15));
    pieDataset.setValue("Java", new Integer(30));
    pieDataset.setValue("Perl", new Integer(10));
    pieDataset.setValue("C,C++,C#", new Integer(30));

    return pieDataset;

}

private JFreeChart create3DPieChart(PieDataset dataset){

    /** Create a PieDataSet* */


    /** Create 3D Pie Chart based on this dataset* */
    JFreeChart chart = ChartFactory.createPieChart3D(
            "Popularity of Languages", dataset, true, true, true);

    return chart;


}

/** This method is called from within the constructor to
 * initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is
 * always regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    jInternalChartFrame = new javax.swing.JInternalFrame();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setName("Form"); // NOI18N

    jInternalChartFrame.setName("jInternalChartFrame"); // NOI18N
    jInternalChartFrame.setVisible(true);

    javax.swing.GroupLayout jInternalChartFrameLayout = new javax.swing.GroupLayout(jInternalChartFrame.getContentPane());
    jInternalChartFrame.getContentPane().setLayout(jInternalChartFrameLayout);
    jInternalChartFrameLayout.setHorizontalGroup(
        jInternalChartFrameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 301, Short.MAX_VALUE)
    );
    jInternalChartFrameLayout.setVerticalGroup(
        jInternalChartFrameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 279, Short.MAX_VALUE)
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jInternalChartFrame, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(634, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jInternalChartFrame, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(300, Short.MAX_VALUE))
    );

    pack();
}// </editor-fold>                        

/**
 * @param args the command line arguments
 */

// Variables declaration - do not modify                     
private javax.swing.JInternalFrame jInternalChartFrame;
// End of variables declaration                   

private void display(){

    final PieDataset dataset = this.createPieDataSet();
    final JFreeChart chart   = this.create3DPieChart(dataset);

    ChartPanel chartPanel = new ChartPanel(chart, false);
    this.jInternalChartFrame.setContentPane(chartPanel);
    this.jInternalChartFrame.pack();
    this.jInternalChartFrame.setVisible(true);
    this.jInternalChartFrame.setSize(100, 100);

    this.pack();
    this.setVisible(true);

}

public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(PieChartJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(PieChartJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(PieChartJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(PieChartJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {

        public void run() {

            PieChartJFrame pieChart = new PieChartJFrame();

            pieChart.display();

        }
    });
}
}

答案 2 :(得分:0)

问题是,NetBeans GUI编辑器将为您生成initComponents()方法,它可以完成布局等所有配置。现在您无法自行修改此方法,因为NetBeans不会让您,即使您修改它在IDE外部会将其更改回原始形式。 但是有一种方法可以修改代码的一部分,这应该适用于这种情况。 GETah是对的。向框架添加面板就像在编辑器中添加任何其他组件一样。右键单击它并选择Customize Code。现在应该有一条看起来像这样的线:

jPanel1 = new javax.swing.jPanel();

该行旁边应该是一个下拉菜单,您可以在其中选择默认代码和自定义创建。您想要选择自定义创建并将行更改为:

jPanel1 = cp;

现在应该工作,对我而言。

答案 3 :(得分:-1)

不要将图表面板添加到主框架中,而是将其添加到其内容窗格中。 replace this.ji.add(cp);的{​​{1}}

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

chartPanel