这里还有一个初学者问题。我的项目中有三个 *.java 文件。我应该能够运行“Main.java”,如果我输入 1 作为输入,它将依次运行“example.java”。
这与 Eclipse 配合良好。然而,在 NetBeans 中,输入 1 后,java 小程序将打开,但不会绘制任何内容。错误是:
无法处理形状示例 java.lang.ClassNotFoundException:示例
所以它没有找到“example.java”,但所有三个文件都在同一个“src”文件夹中..有什么想法吗?
这是所有三个文件:
画家.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());
}
}
}
示例.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");
}
}
最佳答案
在您的paint(Graphics g)
函数中:
(Class.forName(shape)).getMethod("draw", classes).invoke(myShape, objs);
在尝试使用 Class.forName(String)
函数获取类时,您应该提供 fully qualified name
所需类(class)的。也就是说,要获取类 "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 的内容 Pane 默认使用BorderLayout
。因此,请使用此布局之一的约束。
题外话:类名应以大写字母开头。
关于java - NetBeans java.lang.ClassNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20528638/