Java applet上的调用目标异常

时间:2013-05-04 15:16:13

标签: java html exception applet awt

我正在为我的第一个java类完成一个项目,我正在创建一个 applet嵌入到HTML中。我收到了一个Invocation目标异常而我不能 想办法。我的所有类都与HTML文件和jar文件在同一个目录中。它们都在我保存所有java项目的文件中,如果该位置与它有任何关系。

这是一个带有两个子类的程序,它们是主要按钮的事件监听器 类。这意味着计算与听众的投票,并且它正常运行,但不是作为HTML小程序。我不认为程序的代码会成为问题,但在这里是:

 import java.awt.*;
 import java.awt.event.*;

 public class votes {

     static int A;
     static int B;
     static TextField yescount;
     static TextField nocount;

     public static void main(String[] args) {

         yeslistener yus = new yeslistener();
         nolistener nos = new nolistener();
         A = 0;
         B = 0;
         Frame frame = new Frame("Votes");
         frame.setSize(600, 600);
         frame.setVisible(true);

         Label label = new Label("Is java a fun programming language?");
         label.setBounds(50, 50, 400, 50);
         frame.add(label);

         nocount = new TextField("0");
         nocount.setBounds(450, 400, 100, 50);
         nocount.setForeground(Color.red);
         frame.add(nocount);

         yescount = new TextField("0");
         yescount.setBounds(50, 400, 100, 50);
         yescount.setForeground(Color.green);
         frame.add(yescount);

         Button yes = new Button("Yes");
         yes.setBounds(50, 500, 100, 50);
         yes.addMouseListener(yus);
         frame.add(yes);

         Button no = new Button("No");
         no.setBounds(450, 500, 100, 50);
         no.addMouseListener(nos);
         frame.add(no);

         Label lbl = new Label();
         frame.add(lbl);
     }

     static class yeslistener extends MouseAdapter {

         public void mouseClicked(MouseEvent me) {

             A++;
             yescount.setText(A + " Votes");
         }
     }

     static class nolistener extends MouseAdapter {

         public void mouseClicked(MouseEvent me) {

             B++;
             nocount.setText(B + " Votes");
         }
     }
 }

我将所有三个类打包成一个jar文件,用作存档。 这是我用来调用类和存档jar文件的HTML文件:

<HTML>
<Head> <title> Votes </title> </head>
<body>
<applet Code= "votes.class"
 archive= "votes.jar"
 width="600" height="600">
</applet>
</body>
</HTML>

我在Java控制台的例外情况如下:

basic: exception: java.lang.reflect.InvocationTargetException.
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at com.sun.deploy.uitoolkit.impl.awt.AWTAppletAdapter.runOnEDTAndWait(Unknown     Source)
at com.sun.deploy.uitoolkit.impl.awt.AWTAppletAdapter.instantiateApplet(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager.initAppletAdapter(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.reflect.InvocationTargetException
    at com.sun.deploy.uitoolkit.impl.awt.OldPluginAWTUtil.invokeAndWait(Unknown Source)
    ... 5 more
Caused by: java.lang.ClassCastException: votes cannot be cast to java.applet.Applet
    at com.sun.deploy.uitoolkit.impl.awt.AWTAppletAdapter$1.run(Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
basic: Removed progress listener: sun.plugin.util.ProgressMonitorAdapter@17c9937

我一直在寻找答案一段时间了,我觉得Exception没有给我足够的信息来解决这个问题。非常感谢您给予我的任何帮助,谢谢。

1 个答案:

答案 0 :(得分:0)

Applet不使用main方法来执行。小程序是java.applet.Applet的子类,是AWT面板,呈现为java.awt.Panel。小程序以init()start()方法启动。

请参阅The Official Java Tutorials' article on Applets以了解如何使用小程序。