开始:applet未初始化(使用书中的示例)

时间:2014-02-22 17:39:16

标签: java applet

我一直在为此敲打几个小时。我正在使用完全相同的代码,这些代码直接来自本书,声称它本应该起作用。但是每次我运行html文件时,applet都会给我一个错误,说“开始:applet没有初始化,当我知道它应该被初始化时就好了。我已经在网上查找了每一个同名的错误,他们都有自己的错误自己的理由是他们的代码有问题,但我的直接来自声称有用的书。(Java:如何编程 - Deitel第6版)

如我的命令提示历史记录所示,我在appletviewer Assignment2.html中输入文件中相应的html代码并获得相同的结果。我之前编译它并将类文件放在同一目录中。这些是我得到的错误:

C:\Users\my name\Documents\NetBeansProjects\Assignment2\src\assignment2>appletviewe
r Assignment2.html
java.lang.NoClassDefFoundError: Assignment2 (wrong name: assignment2/Assignment2)
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
        at sun.applet.AppletClassLoader.findClass(AppletClassLoader.java:217)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
        at sun.applet.AppletClassLoader.loadClass(AppletClassLoader.java:152)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
        at sun.applet.AppletClassLoader.loadCode(AppletClassLoader.java:625)
        at sun.applet.AppletPanel.createApplet(AppletPanel.java:793)
        at sun.applet.AppletPanel.runLoader(AppletPanel.java:722)
        at sun.applet.AppletPanel.run(AppletPanel.java:379)
        at java.lang.Thread.run(Thread.java:744)

这是我的applet代码,与书籍相同:

package assignment2;
// Fig. 20.10: AdditionApplet.java
// Adding two floating-point numbers.
import java.awt.Graphics;       // program uses class Graphics
import javax.swing.JApplet;     // program uses class JApplet
import javax.swing.JOptionPane; // program uses class JOptionPane


public class Assignment2 extends JApplet 
{
   private double sum; // sum of values entered by user

   // initialize applet by obtaining values from user
   public void init()
   {
      String firstNumber;  // first string entered by user
      String secondNumber; // second string entered by user

      double number1; // first number to add
      double number2; // second number to add

      // obtain first number from user
      firstNumber = JOptionPane.showInputDialog(
         "Enter first floating-point value" );

      // obtain second number from user
      secondNumber = JOptionPane.showInputDialog(
         "Enter second floating-point value" );

      // convert numbers from type String to type double
      number1 = Double.parseDouble( firstNumber ); 
      number2 = Double.parseDouble( secondNumber );

      sum = number1 + number2; // add numbers
   } // end method init

   // draw results in a rectangle on applet�s background
   public void paint( Graphics g )
   {
      super.paint( g ); // call superclass version of method paint

      // draw rectangle starting from (15, 10) that is 270 
      // pixels wide and 20 pixels tall
      g.drawRect( 15, 10, 270, 20 );

      // draw results as a String at (25, 25)
      g.drawString( "The sum is " + sum, 25, 25 );
   } // end method paint
} // end class AdditionApplet

这是我的html文件代码,以防你想确定。

<html>
<applet code = "Assignment2.class" width = "300" height = "50">
</applet>
</html>

1 个答案:

答案 0 :(得分:0)

尝试:

<html>
<applet code = "assignment2.Assignment2.class" width = "300" height = "50">
</applet>
</html>