NetBeans java.lang.ClassNotFoundException

时间:2013-12-11 19:52:41

标签: java eclipse netbeans

这里有另一个初学者问题。我的项目中有三个* .java文件。我应该能够运行“Main.java”,如果输入1作为输入,它将依次运行“example.java”。

这适用于Eclipse。但是在输入1后的NetBeans中,java applet将打开,不会绘制任何内容。错误是:

无法处理形状示例 java.lang.ClassNotFoundException:example

所以它没有找到“example.java”,但所有三个文件都在同一个“src”文件夹中..任何想法?

以下是所有三个文件:

Painter.java:

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

}

1 个答案:

答案 0 :(得分:2)

paint(Graphics g)函数中:

(Class.forName(shape)).getMethod("draw", classes).invoke(myShape, objs);

在尝试使用Class.forName(String)函数获取课程时,您应该提供所需课程的fully qualified name。也就是说,要获得课程"example",您应该提供:Class.forName("recursion.example")

  • 对于自定义绘制,我们不应覆盖paint(Graphics g)函数,而应覆盖paintComponent(Graphics g)函数,并且应在此函数中调用super.paintComponent(g)

  • 您的Painter课程正在延长Component,而是延长JComponent

  • 您正在传递null代替frame.add(component, border_layout_constraint)的约束:不要这样做。 JFrame的内容窗格默认使用BorderLayout。因此,请使用此布局中的约束。

关闭主题:类名应以大写字母开头。