提问人:Madara's Ghost 提问时间:8/29/2011 最后编辑:Madara's Ghost 更新时间:6/13/2016 访问量:2103
Java 小程序错误
Java applet error
问:
我刚刚开始学习Java(呜呜!!),我正在读一本名为《Introduction to Programming Using Java》的书。
书中有一些例子,比如说,关于如何创建小程序,我一直在尝试自己做,但没有成功,即使尝试复制他们的例子,它仍然不起作用。我编写类并编译它,然后将类复制到与我的 html 文件和状态相同的文件夹中
<applet code="StaticRects.class" height=160 width=300></applet>
代码如下:
package helloworld;
import java.awt.*;
/**
*
* @author RikudoSennin
*/
public class StaticRects extends AnimationBase {
public void drawFrame(Graphics g) {
// Draw set of nested black rectangles on a red background.
// Each nested rectangle is separated by 15 pixels on all sides
// from the rectangle that encloses it. The applet is
// assumed to be 300 pixels wide and 160 pixels high.
int inset; // Gap between borders of applet and one of the rectangles.
int rectWidth, rectHeight; // The size of one of the rectangles.
g.setColor(Color.red);
g.fillRect(0, 0, 300, 160); // Fill the entire applet with red.
g.setColor(Color.black); // Draw the rectangles in black.
inset = 0;
rectWidth = 299; // Set size of the first rect to size of applet
rectHeight = 159;
while (rectWidth >= 0 && rectHeight >= 0) {
g.drawRect(inset, inset, rectWidth, rectHeight);
inset += 15; // rects are 15 pixels apart
rectWidth -= 30; // width decreases by 15 pixels on left and 15 on right
rectHeight -= 30; // height decreases by 15 pixels on top and 15 on bottom
}
} // end paint()
} // end class StaticRects
(这是复制版本)
现在,我收到以下错误:
java.lang.NoClassDefFoundError: StaticRects (wrong name: helloworld/StaticRects)
请注意,这是在项目中其他位置定义的类,它扩展了 ,并且其类包含在同一个目录中。AnimationBase
JApplet
我做错了什么?(这可能是一些菜鸟错误,但话又说回来,我是 Java 中的菜鸟)。
将不胜感激任何和所有答案,提前致谢:)
编辑:哦,是的,我正在将JDK 1.7.0与NetBeans 7.0.1一起使用。
答:
对于基本错误,您可以查看有关小程序标签的更多信息:
http://download.oracle.com/javase/1.4.2/docs/guide/misc/applet.html
这是重要的部分:
CODE = appletFile
This REQUIRED attribute gives the name of the file that contains the applet's compiled Applet subclass. This file is relative to the base URL of the applet. It cannot be absolute. One of CODE or OBJECT must be present. The value appletFile can be of the form classname.class or of the form packagename.classname.class.
尝试使用helloworld.StaticRects.class
下面是他们使用包名称的另一个示例:
http://download.oracle.com/javase/tutorial/deployment/applet/html.html
评论
Exception: java.lang.ClassNotFoundException: helloworld.StaticRects.class
是的,菜鸟错误:您正在创建一个名为 helloworld 的类。StaticRects(注意语句),然后仅引用 StaticRects。取出 package 语句将修复它。package helloworld
编辑:James提到了完全相反的解决问题的方法,即提到helloworld。StaticRects.class。当然,你的电话,但我投票支持我的方式,因为你对包没有任何明显的需求(有些人会说你总是需要一些包)。
评论
java.lang.NoClassDefFoundError: StaticRects (wrong name: helloworld/StaticRects)
helloworld
curl
包名称必须存在于三个位置:
在 Java 源文件中
package helloworld;
在 applet 标记中:
<applet code="helloworld.StaticRects" height=160 width=300></applet>
作为代码库目录的子目录,默认情况下,该目录是包含 HTML 文件的目录:
blah/mypage.html
blah/helloworld/StaticRects.class
如果可以让不同的小程序在命名包中工作,那么问题一定出在代码上。如果我使用以下最小类,您的类对我来说效果很好:StaticRects
AnimationBase
package helloworld;
import java.awt.Graphics;
import javax.swing.JApplet;
public abstract class AnimationBase extends JApplet {
public void paint(Graphics g) {
this.drawFrame(g);
}
public abstract void drawFrame(Graphics g);
}
我的目录层次结构如下所示:
html/: 小程序.html helloworld
html/helloworld: 动画库.class StaticRects.class
评论
Detected from bootclasspath: C:\\PROGRA~2\\Java\\jre7\\lib\\deploy.jar
上一个:截断元素与非截断元素并排
下一个:致命错误:找不到类“SELF”
评论