Eclipse 编译 JFrame的子类不过,DOS下是好的。
新手刚开始学习。
Eclipse SDK
Version: 3.5.2
Build id: M20100211-1343
就两个文件MyRect2D.java和MyWindowListener.java。 在外面DOS下, javac 和java都可以正常编译运行,但是在eclipse里面就老是报如下错误。如果不是建立JFrame的子类,比如hello world之类的就程序就可以在eclipse里面正常运行。在Run Configurations...里面设置JRE 为jdk1.7.0或jre1.7.0都不行,是不是还有什么地方需要设置啊?我装了一个jre1.7.0,还有一个jdk1.7.0,或者是下面学习的代码太老的关系?
Error: Could not find main class MyRect2D
Exception in thread "main"
java.lang.NoClassDefFoundError: MyRect2D
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: MyRect2D
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 1 more
以下是源码:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.geom.*;
public class MyRect2D extends JFrame{
public MyRect2D() {
super("MyRect2D:绘2D图形");
setSize(240, 160);
show();
}
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setPaint(Color.red);
g2d.setStroke(new BasicStroke(6.0f));
g2d.draw(new Rectangle2D.Double(80, 40, 70, 100));
}
/**
* @param args
*/
public static void main(String[] args){
// TODO Auto-generated method stub
MyRect2D app = new MyRect2D();
app.addWindowListener(new MyWindowListener());
}
}
import java.awt.*;
import java.awt.event.*;
class MyWindowListener extends WindowAdapter {
public void windowClosing(WindowEvent e)
{
System.exit(1);
}
}
------解决方案--------------------
在工程里的java buildpath里设定好source