问题
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):
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:
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
}
回答1:
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)
Or better:
In NetBeans, under the GUI editor (not code editor) add a panel into your main frame and call it something like 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);
}
回答2:
You did not add dtp to the JFrame's content pane. You can use the UI editor of NetBeans.
回答3:
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();
}
});
}
}
回答4:
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. But there is a way to modify parts of the code, which should work for this case. GETah was right. 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.
来源:https://stackoverflow.com/questions/8203628/how-to-add-a-jfreechartpie-chart-to-a-panel-in-netbeans