几乎完全相同的程序。一个工作,一个不工作

时间:2013-10-01 13:11:17

标签: java swing jfreechart

我有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();
            }
        });
    }
}

运行一个干净的构建导致100个错误,第一个错误是:

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

1 个答案:

答案 0 :(得分:4)

如评论中所述,如下所示,您的代码运行正常。您的构建环境中的类路径看起来比您需要的更多。在命令行上,如here所示,您通常只需要jfreechartjcommon。在NetBeans中,查看库管理器,查看here及以下。

NetBeans Ant Library Manager显示JFreeChart

Ant Library Manager

图片,运行:

image

代码,运行:

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