gpt4 book ai didi

java - 几乎相同的程序。一个有效,一个无效

转载 作者:行者123 更新时间:2023-11-29 03:29:30 24 4
gpt4 key购买 nike

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

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 {

最佳答案

如评论中所述和如下所示,您的代码运行良好。看起来您在构建环境中的类路径 中获得了超出需要的内容。在命令行中,如图here ,您通常只需要 jfreechartjcommon。在 NetBeans 中,检查库管理器,看到 here及以下。

NetBeans Ant 库管理器显示 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();
}
});
}
}

关于java - 几乎相同的程序。一个有效,一个无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19117109/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com