0

我正在为我的第一个 java 类完成一个项目,并且正在创建一个嵌入 HTML 的小程序。我得到一个调用目标异常,我想不通。我所有的类与 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

我一直在寻找答案一段时间,我觉得异常没有给我足够的信息来解决所有问题。您能给我的任何帮助将不胜感激,谢谢。

4

1 回答 1

0

Applet 不使用 main 方法来执行。Applet 是java.applet.AppletAWT 面板的子类,呈现方式类似于java.awt.Panel. 小程序以它们的init()start()方法开始。

请参阅有关 Applet 的官方 Java 教程文章以了解如何使用 Applet。

于 2013-05-04T15:37:16.957 回答