[英]How to add a Jfreechart(pie chart) to a panel in netbeans
我正在使用netbeans gui編輯器,並試圖添加一個本身在內部框架中的Jfreechart,並將這個內部框架添加到面板中,如您在此圖像中所見(抱歉,我無法直接發布圖像,因為im新手):
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
}
您沒有將dtp添加到JFrame的內容窗格中。 您可以使用NetBeans的UI編輯器。
我認為以下代碼將為您工作:
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();
}
});
}
}
很好的問題是,NetBeans GUI編輯器將為您生成initComponents()方法,該方法在其中進行布局的所有配置。現在您無法自行修改此方法,因為NetBeans不允許您這樣做,即使您在Windows之外進行修改也是如此。 IDE會將其更改回原始形式。 但是有一種方法可以修改部分代碼,在這種情況下應該可以使用。 GETah是對的。 將面板添加到框架中,就像在編輯器中添加其他任何組件一樣。 右鍵單擊它,然后選擇“定制代碼”。 現在應該有一行看起來像這樣:
jPanel1 = new javax.swing.jPanel();
該行旁邊應該是一個下拉菜單,您可以在其中選擇默認代碼和自定義創建。 您要選擇自定義創建並將行更改為此:
jPanel1 = cp;
現在應該工作,為我做。
不要將圖表面板添加到主框架中,而是將其添加到其內容窗格中。 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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.