1

我有 2 个几乎相同的程序。唯一的区别是类的名称,因此程序的一行引用了该名称(以构造类的实例)。第一个运行良好。导致问题的行(尽管它在两个程序中并且在第一个程序中都没有引起问题),netbeans 报告的不是语句是
panel.add(chartPanel); 错误详情:

Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Uncompilable source code - not a statement
at splinepanel.SplinePanel.createContentPane(SplinePanel.java:139)
at splinepanel.SplinePanel.createAndShowGUI(SplinePanel.java:157)
at splinepanel.SplinePanel.access$000(SplinePanel.java:33)
at splinepanel.SplinePanel$1.run(SplinePanel.java:170)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

有人可以帮忙吗?第一个程序(有效)称为 PanelExmpale.java。第二个不起作用的程序如下:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package splinepanel;

/*

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
* 
* */
import javax.swing.*;
import java.awt.*;       // Using AWT containers and components
import java.awt.event.*; // Using AWT events and listener interfaces
import javax.swing.*;    // Using Swing components and containers
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.ChartPanel;
import org.jfree.data.general.Series;
import org.jfree.util.PublicCloneable;
import org.jfree.chart.renderer.xy.XYSplineRenderer;

/**
 *
 * @author User
 */
public class SplinePanel{

    public JPanel createContentPane(){
        // This is where we'll put all our widgets
        // in the next tutorials!
        JPanel panel = new JPanel();

        /*

        SplineFactory s = new SplineFactory(); 

        double[] c = new double[12];
    c[0]  =   0.0;  // x0
    c[1]  =   0.0;  // y0
    c[2]  =   0.0;  // z0      

    c[3]  =   1.0;  // x1
    c[4]  =   1.0;  // y1
    c[5]  =   0.0;  // z1      

    c[6]  =   2.0;  // x2
    c[7]  =  -1.0;  // y2
    c[8]  =   0.0;  // z2      

    c[9]  =  10.0;  // x3
    c[10] =   0.0;  // y3
    c[11] =   0.0;  // z3      

    double[] spline1 = SplineFactory.createBezier (c,     20);
    double[] spline2 = SplineFactory.createCubic (c,      20);
    double[] spline3 = SplineFactory.createCatmullRom (c, 20); 

    /*

    System.out.println ("-- Bezier");
    for (int i = 0; i < spline1.length; i+=3)
      System.out.println (spline1[i] + "," + spline1[i+1] + "," + spline1[i+2]);

    System.out.println ("-- Cubic");
    for (int i = 0; i < spline2.length; i+=3)
      System.out.println (spline2[i] + "," + spline2[i+1] + "," + spline2[i+2]);

    System.out.println ("-- Catmull-Rom");
    for (int i = 0; i < spline3.length; i+=3)
      System.out.println (spline3[i] + "," + spline3[i+1] + "," + spline3[i+2]);

    */



        panel.setLayout(new BorderLayout());

        XYSeries series = new XYSeries("MyGraph");

        /*

        System.out.println ("-- Bezier");
        for (int i = 0; i < spline1.length; i+=3)
         System.out.println (spline1[i] + "," + spline1[i+1] + "," + spline1[i+2]);
        // series.add(spline1[i])

        System.out.println ("-- Cubic");
        for (int i = 0; i < spline2.length; i+=3)
         System.out.println (spline2[i] + "," + spline2[i+1] + "," + spline2[i+2]);

        System.out.println ("-- Catmull-Rom");
        for (int i = 0; i < spline3.length; i+=3)
         System.out.println (spline3[i] + "," + spline3[i+1] + "," + spline3[i+2]); 

         */

        /* 
        series.add(0, 1);
        series.add(1, 2);
        series.add(2, 5);
        series.add(7, 8);
        series.add(9, 10);
        */

         series.add(0, 1);
        series.add(1, 2);
        series.add(2, 5);
        series.add(7, 8);
        series.add(9, 10);

        XYSeriesCollection dataset = new XYSeriesCollection();
        dataset.addSeries(series);

        JFreeChart chart = ChartFactory.createXYLineChart(
                "XY Chart",
                "x-axis",
                "y-axis",
                dataset, 
                PlotOrientation.VERTICAL,
                true,
                true,
                false
                );
        ChartPanel chartPanel = new ChartPanel(chart);

        //chart.getXYPlot().setRenderer(new XYSplineRenderer());





        panel.add(chartPanel);
        //panel.setSize(800, 500);

        //content panes must be opaque



        panel.setOpaque(true); 
        return panel;  
    }

    private static void createAndShowGUI() {

        JFrame.setDefaultLookAndFeelDecorated(true);
        JFrame frame = new JFrame("[=] There's a JPanel in here! [=]");

        //Create and set up the content pane.
        SplinePanel demo = new SplinePanel();
        frame.setContentPane(demo.createContentPane());

        // The other bits and pieces that make our program a bit more stable.
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(1300, 650);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

运行 clean and build 会导致 100 个错误,其中第一个是:

error: duplicate class: org.jfree.experimental.chart.demo.CombinedCategoryPlotDemo1 
public class CombinedCategoryPlotDemo1 extends ApplicationFrame { 
4

1 回答 1

4

如评论中所述并如下所示,您的代码运行良好。看起来您的构建环境中的类路径已经超出了您的需要。在命令行上,如此处所示您通常只需要jfreechartjcommon。在 NetBeans 中,检查库管理器,如下所示

NetBeans Ant 库管理器显示JFreeChart

蚂蚁库管理器

运行时的图像:

图片

代码,如运行:

import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
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.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class SplinePanel {

    public JPanel createContentPane() {
        JPanel panel = new JPanel(new GridLayout());
        XYSeries series = new XYSeries("MyGraph");
        series.add(0, 1);
        series.add(1, 2);
        series.add(2, 5);
        series.add(7, 8);
        series.add(9, 10);
        XYSeriesCollection dataset = new XYSeriesCollection();
        dataset.addSeries(series);
        JFreeChart chart = ChartFactory.createXYLineChart(
            "XY Chart",
            "x-axis",
            "y-axis",
            dataset,
            PlotOrientation.VERTICAL,
            true,
            true,
            false);
        ChartPanel chartPanel = new ChartPanel(chart);
        panel.add(chartPanel);
        panel.setOpaque(true);
        return panel;
    }

    private static void createAndShowGUI() {
        JFrame.setDefaultLookAndFeelDecorated(true);
        JFrame frame = new JFrame("[=] There's a JPanel in here! [=]");
        SplinePanel demo = new SplinePanel();
        frame.setContentPane(demo.createContentPane());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(640, 480);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                createAndShowGUI();
            }
        });
    }
}
于 2013-10-01T18:06:16.607 回答