如何将jFreeChart添加到jFrame中的jPanel?

时间:2014-02-01 21:54:39

标签: java netbeans jfreechart

我看了很多类似的问题/答案,发现了一个exactly the same,但我没有设法让我的程序工作。输出是一个空白的JFrame,它应该输出一个带有图形(100x200)的JFrame。

以下是包含的代码:

package my.Project;

import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class ChartTest extends javax.swing.JFrame {

    public ChartTest() {
        try {

            XYSeries Goals = new XYSeries("Goals Scored");
            Goals.add(1, 1.0);
            Goals.add(2, 3.0);
            Goals.add(3, 2.0);
            Goals.add(4, 0.0);
            Goals.add(5, 3.0);

            XYDataset xyDataset = new XYSeriesCollection(Goals);

            JFreeChart chart = ChartFactory.createXYLineChart("Goals Scored Over Time", "Fixture Number", "Goals", xyDataset, PlotOrientation.VERTICAL, true, true, false);

            JPanel jPanel1 = new JPanel();
            jPanel1.setLayout(new java.awt.BorderLayout());
            jPanel1.setVisible(true);
            jPanel1.setSize(300, 300);

            ChartPanel CP = new ChartPanel(chart);
            CP.setPreferredSize(new Dimension(100, 200));
            CP.setMouseWheelEnabled(true);

            jPanel1.add(CP, BorderLayout.CENTER);
            jPanel1.validate();

        } catch (Exception e) {
            System.out.print("Chart exception:" + e);
        }
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>                        

    public static void main(String args[]) {

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ChartTest().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                     
    // End of variables declaration                   
}

2 个答案:

答案 0 :(得分:4)

您永远不会将ChartPanel添加到JFrame

image

经测试:

import java.awt.Dimension;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class ChartTest extends javax.swing.JFrame {

    public ChartTest() {
        XYSeries Goals = new XYSeries("Goals Scored");
        Goals.add(1, 1.0);
        Goals.add(2, 3.0);
        Goals.add(3, 2.0);
        Goals.add(4, 0.0);
        Goals.add(5, 3.0);
        XYDataset xyDataset = new XYSeriesCollection(Goals);
        JFreeChart chart = ChartFactory.createXYLineChart(
            "Goals Scored Over Time", "Fixture Number", "Goals",
            xyDataset, PlotOrientation.VERTICAL, true, true, false);
        ChartPanel cp = new ChartPanel(chart) {

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(320, 240);
            }
        };
        cp.setMouseWheelEnabled(true);
        add(cp);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();
    }

    public static void main(String args[]) {

        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new ChartTest().setVisible(true);
            }
        });
    }
}

答案 1 :(得分:0)

您可以使用以下命令扩展JFrame并添加到JFrame:

加(CP);

或者如果您有另一个扩展JFrame的GuI类,您可以在其中添加带有图表的JPanel。