首先我给出源程序:
下面是第一个类:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
class ToggleButton extends Button implements ActionListener
   {    
        private String label1;     //two labels to toggle between
        private String label2;
   public ToggleButton(String s1,String s2)   //构造函数
    { 
      super(s1);     //call Button's constructor to set the label
      label1=s1;     //Set my two labels
      label2=s2;
      addActionListener(this);
   
    }
   public void actionPerformed(ActionEvent e)   
 {
  String temps=label1;  //swap the labels
  label1=label2;
  label2=temps;
  setLabel(label1);
     }
}
下面是第2个类:
  import java.applet.*;
  import java.awt.*;
  import java.awt.event.*;
public class ToggleTest extends Applet implements ActionListener
 {
private ToggleButton lightSwitch;
public void init()
{
   lightSwitch=new ToggleButton("off","on");
   add(lightSwitch);
   lightSwitch.addActionListener(this);

}
public void actionPerformed(ActionEvent e)
{
showStatus("The Light is"+lightSwitch.getLabel());
}

}
这个程序编译的时候没有问题,但是在运行的时候说:
--------------------Configuration: ToggleButton - j2sdk1.4 <Default> - <Default>--------------------
Initializing jdb ...
> run sun.applet.AppletViewer ToggleButton.htm
Set uncaught java.lang.Throwable
Set deferred uncaught java.lang.Throwable

VM Started: 载入:无法实例化 ToggleButton.class。
java.lang.InstantiationException: ToggleButton
        at java.lang.Class.newInstance0(Class.java:293)
        at java.lang.Class.newInstance(Class.java:261)
        at sun.applet.AppletPanel.createApplet(AppletPanel.java:619)
        at sun.applet.AppletPanel.runLoader(AppletPanel.java:548)
        at sun.applet.AppletPanel.run(AppletPanel.java:299)
        at java.lang.Thread.run(Thread.java:534)
谁能帮我解决一下吗?

解决方案 »

  1.   

    程序没有什么问题哦,你是用什么IDE运行的?在DOS下用AppletViewer ToggleButton.htm看看如何?
      

  2.   

    可能是找不到ToggleButton.class,要在ToggleButton.html中指定codebase, 即applet相关的classpath.
      

  3.   

    我已经实验过了,在JCreator下,在DOS下用Appletviewer看都是同样的结果啊,可能是我的机器哪里设置错了,可是我不知道原因啊!现在好郁闷,今天早上编了一个温度转换器的APPLET,编译也是通过的,可是在运行的时候又出现了“小程序无法初始化”的错误了。。
      

  4.   

    好像是没什么问题的,你再好好试试,用Jcreater试试
      

  5.   

    你看看classpath是不是设置有问题,或者两个class文件是不是在同一目录下