2011-11-20 52 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; 

現在應該工作,對我來說。