简体   繁体   English

如何在NetBeans的面板中添加Jfreechart(饼图)

[英]How to add a Jfreechart(pie chart) to a panel in netbeans

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): 我正在使用netbeans gui编辑器,并试图添加一个本身在内部框架中的Jfreechart,并将这个内部框架添加到面板中,如您在此图像中所见(抱歉,我无法直接发布图像,因为im新手):

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

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: 当我运行它时,内部框架甚至都没有显示在面板“ 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

}

You did not add dtp to the JFrame's content pane. 您没有将dtp添加到JFrame的内容窗格中。 You can use the UI editor of NetBeans. 您可以使用NetBeans的UI编辑器。

I think following code will work for you : 我认为以下代码将为您工作:

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

        }
    });
}
}

Well the problem is that the NetBeans GUI editor will generate the initComponents() method for you where it does all the configuration of layouts etc. Now you cannot modify this method yourself because NetBeans won't let you and even if you modify it outside the IDE it will change it back to the original form. 很好的问题是,NetBeans GUI编辑器将为您生成initComponents()方法,该方法在其中进行布局的所有配置。现在您无法自行修改此方法,因为NetBeans不允许您这样做,即使您在Windows之外进行修改也是如此。 IDE会将其更改回原始形式。 But there is a way to modify parts of the code, which should work for this case. 但是有一种方法可以修改部分代码,在这种情况下应该可以使用。 GETah was right. GETah是对的。 Add a panel to your frame just like you would add any other component in the editor. 将面板添加到框架中,就像在编辑器中添加其他任何组件一样。 Right click on it and select Customize Code. 右键单击它,然后选择“定制代码”。 Now there should be a line looking something like this: 现在应该有一行看起来像这样:

jPanel1 = new javax.swing.jPanel();

Next to that line should be a dropdown menu where you can choose between default code and custom creation. 该行旁边应该是一个下拉菜单,您可以在其中选择默认代码和自定义创建。 You want to select custom creation and change the line to this: 您要选择自定义创建并将行更改为此:

jPanel1 = cp;

Should work now, does for me. 现在应该工作,为我做。

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) 通过this.ji.getContentPane().add(cp)

Or better: In NetBeans, under the GUI editor (not code editor) add a panel into your main frame and call it something like chartPanel . 或更好:在NetBeans中,在GUI编辑器(而非代码编辑器)下,将一个面板添加到您的主框架中,然后将其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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM