java-如何在jFrame的jPanel中添加jFreeChart?

我查看了许多相似的问题/答案,发现其中一个问题是exactly the same,但是我没有设法使我的程序正常工作.输出是空白的JFrame,它应该输出一个JGraph,其中包含一个graph(100×200).

这是包含的代码:

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                   
}

解决方法:

您永远不会将ChartPanel添加到JFrame.

经测试:

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);
            }
        });
    }
}
上一篇:java-Netbeans IDE中的企业应用程序项目和Web项目应用程序之间的区别


下一篇:java-将属性文件转换为excel