2011-11-20 26 views
0

使用netbeans gui编辑器和im试图添加一个Jfreechart本身在一个内部框架和这个内部框架我想将它添加到面板,你可以在此图像中看到(抱歉,我不能上传图像直接因为我是新手):如何在NetBeans中添加Jfreechart(饼图)到面板

http://www.flickr.com/photos/[email protected]/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 

} 
+0

为什么不简单地手工添加它呢? –

回答

-1

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

或者更好: 在NetBeans中,下的GUI编辑器(不是代码编辑器)添加面板进入您的主框架,并调用它像chartPanel。添加您想要显示的所有其他控件,并根据需要放置它们。完成后,切换回代码编辑器。在主框架的构造函数,请执行下列操作:

// 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); 
} 
+0

仍然不工作的人,有任何其他想法? –

+0

刚刚编辑我的答案,请尝试,让我知道:) – GETah

+0

我不能编辑该方法 –

1

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

+0

饼图仍然不显示? :( –

+0

您必须对JDesktopPane/JInternalFrame的使用有任何问题,请参阅http://www.roseindia.net/java/example/java/swing/AllFrameDesktopContainer.shtml(在http:后添加一个空格)否则尝试显示一个简单的JLabel –

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

     } 
    }); 
} 
} 
+0

它工作thx很多! –

0

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

jPanel1 = new javax.swing.jPanel(); 

下一步,该行应该是一个下拉菜单,你可以默认代码和自定义创建之间进行选择。您想要选择自定义创建并将该行更改为:

jPanel1 = cp; 

现在应该工作,对我来说。