运行applet程序时JAVA出错

时间:2015-08-17 16:03:59

标签: java applet

当我运行我的applet程序时,我进入applet查看器窗口“Applet未初始化”并且在cmd中我收到以下错误!

    **java.lang.ClassCastException: application cannot be cast to java.applet.Applet
    at sun.applet.AppletPanel.createApplet(AppletPanel.java:799)
    at sun.applet.AppletPanel.runLoader(AppletPanel.java:728)
    at sun.applet.AppletPanel.run(AppletPanel.java:378)
    at java.lang.Thread.run(Thread.java:745)**

任何人都可以帮助我为什么会发生这种错误?

这是我的代码:

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
//<applet code="application" width=500 height=500> </applet>
public class application extends Frame implements ActionListener,ItemListener
{
Label name,regno
Button submit;
TextField tname,tregno;
String msg=" ";
public void init()
{
    name=new Label("Name");   //Adding name and regno field box
    regno=new Label("Register No");
    submit=new Button("Submit");
    tname=new TextField(20);
    tregno=new TextField(10);
    add(name); add(tname);
    add(regno); add(tregno);
    add(submit);
    submit.addActionListener(this);
    tname.addActionListener(this);
    tregno.addActionListener(this);
    }
public void actionPerformed(ActionEvent ae)
{
    String str=ae.getActionCommand();
    if(str.equals("Submit"))
        msg="Your application form is submitted successfully";
    app a=new app();    //Reference for my next program called "app"
    a.setVisible(true);
    repaint();
}
public void itemStateChanged(ItemEvent ie)
{
    repaint();
}
public void paint(Graphics g)
{
    g.drawString("Name: "+tname.getText(),10,160); //Printing the name and regno
    g.drawString("Register No: "+tregno.getText(),10,180);
    g.drawString(msg,250,350);
}
}

在上面的程序中,我创建了一个基本的applet结构,可以在单击提交按钮时获取人员的详细信息(名称和regno)以及指向另一个程序的超链接。

1 个答案:

答案 0 :(得分:0)

Applet类应该是Applet的子类。修改类声明,使其也extends Applet

public class application extends ..., Applet {
    //code here
}

有关java.applet.Applet的更多信息,请访问Javadocs