NetBeans java.lang.ClassNotFoundException
这里还有一个初学者问题。我的项目中有三个*.java文件。我应该能够运行“Main.java”,如果我输入1作为输入,它将反过来运行“example.java” 这在Eclipse中运行良好。但是在NetBeans中,输入1后,java小程序将打开,并且不会绘制任何内容。错误是: 无法处理形状示例 java.lang.ClassNotFoundException:示例 所以它找不到“example.java”,但这三个文件都在同一个“src”文件夹中。。有什么想法吗 以下是所有三个文件: Painter.java:NetBeans java.lang.ClassNotFoundException,java,eclipse,netbeans,Java,Eclipse,Netbeans,这里还有一个初学者问题。我的项目中有三个*.java文件。我应该能够运行“Main.java”,如果我输入1作为输入,它将反过来运行“example.java” 这在Eclipse中运行良好。但是在NetBeans中,输入1后,java小程序将打开,并且不会绘制任何内容。错误是: 无法处理形状示例 java.lang.ClassNotFoundException:示例 所以它找不到“example.java”,但这三个文件都在同一个“src”文件夹中。。有什么想法吗 以下是所有三个文件: Pai
package recursion;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
/*
* open a frame named aShape and drew the given shape
*/
public class Painter extends Component {
private static final long serialVersionUID = 1L;
private static int SIZE = 600;
private static Painter painter;
private static Graphics g;
private static String shape = null;
// Create a frame and display it
public static void draw(String aShape) {
shape = aShape;
JFrame frame = new JFrame(shape);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationByPlatform(true);
painter = new Painter();
frame.add(painter, null);
frame.pack();
frame.setVisible(true);
}
// returns the Frame's width
public static int getFrameWidth () {
return painter.getSize().width;
}
// returns the Frame's height
public static int getFrameHeight () {
return painter.getSize().height;
}
// changes the color of the lines to be drawn
public static void setColor (String color) {
if (color.equals("red")){
g.setColor(Color.red);
}
else if (color.equals("blue")){
g.setColor(Color.blue);
}
else if (color.equals("green")){
g.setColor(Color.green);
}
}
// public static void drawLine (Pixel p1, Pixel p2) {
// drawLine((int)Math.round(p1.getX()),(int)Math.round(p1.getY()), (int)Math.round(p2.getX()),(int)Math.round(p2.getY()));
//
// }
// Draw a line on the frame
public static void drawLine (int x1, int y1, int x2, int y2) {
g.drawLine(x1, getFrameHeight()-y1, x2, getFrameHeight()-y2);
}
// Set the default size of the window frame to SIZE*SIZE pixels
public Dimension getPreferredSize() {
return new Dimension(SIZE, SIZE);
}
// paint the frame - draw the shape given (call the draw method in that shape object)
public void paint(Graphics g) {
Painter.g = g;
try{
Object myShape = (Class.forName(shape)).newInstance();
Object [] objs = null;
Class [] classes = null;
(Class.forName(shape)).getMethod("draw", classes).invoke(myShape, objs);
}
catch(Exception e)
{
System.out.println("Can't handle shape " + shape);
System.out.println(e.toString());
System.out.println(e.getCause());
}
}
}
example.java:
package recursion;
/*
the class example draw a line.
*/
public class example {
public void draw(){
int width = Painter.getFrameHeight()/2; // find the x coordinate of the center of the frame
int height = Painter.getFrameWidth()/2; // find the y coordinate of the center of the frame
int maxRadius = Math.min(width, height)/2;
// change the color of the line to be drawn to red
Painter.setColor("red");
// draw a line from (width, height) to (width+maxRadius, height+maxRadius)
Painter.drawLine(width, height, width+maxRadius, height+maxRadius);
}
}
Main.java:
package recursion;
import java.util.Scanner;
/*
* the class main get from the user the shape he wish to draw,
* and call the drew method of the desired shape .
*/
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Please enter the number of the shape you wish to draw:\n" +
" 1-example\n" +
" 2-BasicStar\n" +
" 3-Snowflake\n" +
" 4-SuperSnowflake\n" +
" 5-KochCurve\n" +
" 6-KochSnowflake\n");
int shape = sc.nextInt();
// chooses which shape to draw based on the number received
switch(shape){
/*
* An example given to you so you can see how the painted works.
* This example opens a frame, and draws a red line.
*/
case 1:
drawExample();
break;
case 2:
drawBasicStar();
break;
case 3:
drawSnowflake();
break;
case 4:
drawSuperSnowflake();
break;
case 5:
drawKochCurve();
break;
case 6:
drawKochSnowflake();
break;
default: System.out.println("invalid shape");
}
sc.close();
}
// Draw the example line
public static void drawExample(){
Painter.draw("example");
}
// Draw a BasicStar
public static void drawBasicStar(){
Painter.draw("BasicStar");
}
// Draw a Snowflake
public static void drawSnowflake(){
Painter.draw("Snowflake");
}
// Draw a SuperSnowflake
public static void drawSuperSnowflake(){
Painter.draw("SuperSnowflake");
}
// Draw a KochCurve
public static void drawKochCurve(){
Painter.draw("KochCurve");
}
// Draw a KochSnowflake
public static void drawKochSnowflake(){
Painter.draw("KochSnowflake");
}
}
在绘制(图形g)
功能中:
(Class.forName(shape)).getMethod("draw", classes).invoke(myShape, objs);
尝试使用class.forName(String)
函数获取类时,应提供所需类的名称。也就是说,要获取类“example”
,您应该提供:class.forName(“recursion.example”)
- 对于自定义绘制,我们不应覆盖
函数,而应覆盖paint(Graphics g)
函数和paintComponent(Graphics g)
super。应在此函数内调用paintComponent(g)
- 您的
类正在扩展Painter
组件,而不是扩展
JComponent
- 您正在传递
以代替null
:不要这样做。JFrame的内容窗格默认使用frame的约束。添加(组件、边框\u布局\u约束)
。因此,使用此布局之一的约束BorderLayout
离题:类名应以大写字母开头。请添加
stacktrace
并指向该行。可能缺少某些导入。您应该检查bin文件夹或netbeans放置编译类的位置,并查看是否有示例.class
文件。我检查了存储所有类的文件夹(\build\classes\recursion),文件也在那里。斯密特,我不知道你是什么意思(这里是初学者:/)检查下面的答案。有趣的是,这些初级课程都是由我们大学的老师们开设的。我们现在应该实施这些方法。他们对这些惯例的错误扣分,然后他们犯同样的错误。。他们真丢脸:)无论如何,谢谢!