无法转换为java.applet.Applet

时间:2013-11-14 14:06:53

标签: java applet appletviewer

import java.awt.*;
import java.sql.*;
import java.awt.event.*;
public class shares 
{
 public static void main(String[] arguments){
String P[]=new String[100];
String Co[]=new String[100];
String N[]=new String[100];
        String Cu[]=new String[100];
String PA[]=new String[100];
String CoA[]=new String[100];
        String NA[]=new String[100];
        String CuA[]=new String[100];
         int number_bef=0;
         int number_aft=0;
         int Hayleys_Amount =0;
         try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection cc = DriverManager.getConnection("jdbc:odbc:me");
Statement ss = cc.createStatement();
        ResultSet rec_bef = ss.executeQuery("select * from List order by Purchased_Date" );
while (rec_bef.next()) {
         P[number_bef]=rec_bef.getString("Purchased_Date");
         Co[number_bef]=rec_bef.getString("Company");
         N[number_bef]=rec_bef.getString("Number_of_Shares");
         Cu[number_bef]=rec_bef.getString("Current_Price");
         number_bef++;
         }
    rec_bef.close();

         ss.executeUpdate("insert into List  (Purchased_Date, Company, Number_of_Shares, Current_Price) values ('Dec 28, 2003', 'Vanik(NV)', '1350','2')"); 

    ss.executeUpdate("delete * from List where Purchased_Date='Jan 21, 2000'");

    ResultSet rec_aft = ss.executeQuery("select * from List order by Purchased_Date" );
while (rec_aft.next()) {
         PA[number_aft]=rec_aft.getString("Purchased_Date");
         CoA[number_aft]=rec_aft.getString("Company");
         NA[number_aft]=rec_aft.getString("Number_of_Shares");
         CuA[number_aft]=rec_aft.getString("Current_Price");
         number_aft++; 
         }
    rec_aft.close();
    }catch  (Exception e) {
    System.out.println("ERROR: " + e.toString() +e.getMessage()); 
    }
    System.out.println("           ");
    System.out.println("Purchased_Date, Company, Number_of_Shares,Current_Price"); 

    for (int i=0; i<number_bef; i++){
    System.out.println(P[i] +  " , "+ Co[i] + " , " + N[i] + " , "+ Cu[i] +  "." );
    }
    System.out.println("           ");
    System.out.println("Inserted ('Dec 28, 2003', 'Vanik(NV)', '1350','2') and deleted Purchased_Date='Jan 21, 2000' ");               

    // PRINTING AFTER UPDATE         
    System.out.println("Purchased_Date, Company, Number_of_Shares, Current_Price");  

       for (int i=0; i<number_aft; i++){
       System.out.println(PA[i] +  " , " + CoA[i] + " , " + NA[i] + " , " + CuA[i] +  "." );
       String str="Hayleys";
       if (CoA[i].equals(str))
         Hayleys_Amount = Hayleys_Amount  + (Integer.parseInt(NA[i])*Integer.parseInt(CuA[i]));

     }
    System.out.println("The current value of the Hayleys shares  = "  +Hayleys_Amount);
}
}

这是我的代码,当我尝试通过命令提示符运行applet时,它表示共享无法转换为java.applet.Applet。请帮我弄清楚我的代码有什么问题。提前谢谢。

2 个答案:

答案 0 :(得分:1)

您的课程需要扩展java.applet.Applet课程。只需将extends Applet添加到类定义中(当然还有import java.applet.Applet)。例如:

public class shares extends Applet {
   ..
}

不相关 您遇到的问题,您应该遵循有关命名的Java code conventions,例如:你的班级名称应以大写字母开头。

答案 1 :(得分:0)

shares cannot be cast to java.applet.Applet因为shares类不是Applet的子类。要解决此问题,您应该使用:

public class shares extends Applet{