且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在netbeans中向面板添加Jfreechart(饼图)

更新时间:2023-12-04 09:34:40

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

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)

或更好:
在NetBeans中,在GUI编辑器(不是代码编辑器)下添加一个面板到主框架,并称之为 chartPanel 。添加要显示的所有其他控件并根据需要定位它们。完成后,切换回代码编辑器。在主框架的构造函数上,执行以下操作:

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