简体   繁体   中英

Applet won't run

I'm new to the java language and I tried to make a program with the help of google, this is an applet program but it doesn't run even though there are no errors in eclipse. I am also confused with the cardLayout if it can be used in the applet, so I'll be grateful for the help.

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

import java.io.*;
import javax.swing.*;

public class Proto extends Applet implements ActionListener {
    Label Store = new Label("WELCOME TO SCHOOL SUPPLIES STORE");
    double Price, Subtotal;
    String Code;
    String ITname;
    String quan;
    String choice;
    int qty;
    double Total;
    CardLayout card1;
    Container c, d;
    File writer;
    Button buttonBUY = new Button ("BUY");
    Button buttonCONFIRM = new Button("Confirm");
    Button buttonCONFIRM1 = new Button("Confirm your transaction?");
    Button back = new Button ("Back");
    Button close = new Button ("Close");
    Font StoreFont = new Font("Times New Roman", Font.BOLD, 15);


    Label flb1 = new Label ("Item");
    Label flb2 = new Label ("Quantity");
    Label flb3 = new Label ("Price");
    Label flb4 = new Label ("SUbtotal");
    Label lb1 = new Label("ITEM CODE  ");
    Label lb2 = new Label("  PRODUCT NAME  ");
    Label lb3 = new Label("  PRICE  ");
    Label lb4 = new Label ("  QUANTITY ");


    Label c1 = new Label ("HBW01");
    Label HBW01pn = new Label ("  HBW Ballpen  ");
    Label HBW01p = new Label ("  8.00PHP  ");

    Label c2 = new Label ("MNG02");
    Label MNG02pn = new Label ("  Mongol Pencil  ");
    Label MNG02p = new Label ("  6.00PHP  ");

    Label c3 = new Label ("MRK03");
    Label MRK03pn = new Label ("  Marker/Pentel  ");
    Label MRK03p = new Label ("  30.00PHP    ");

    Label c4 = new Label ("YLW04");
    Label YLW04pn = new Label ("  One Whole Yellow Pad  ");
    Label YLW04p = new Label ("  30.00PHP    ");

    Label c5 = new Label ("CRT05");
    Label CRT05pn = new Label ("  Cartolina  ");
    Label CRT05p = new Label ("  10.00PHP    ");

    Label c6 = new Label ("MNL06");
    Label MNL06pn = new Label ("  Manila Paper  ");
    Label MNL06p = new Label ("  6.00PHP    ");

    Label c7 = new Label ("EGL07");
    Label EGL07pn = new Label ("  Elmer's Glue  ");
    Label EGL07p = new Label ("  12.00PHP    ");

    Label c8 = new Label ("CPR08");
    Label CPR08pn = new Label ("  Colored Paper ");
    Label CPR08p = new Label ("  1.00PHP    ");

    Label c9 = new Label ("BPR09");
    Label BPR09pn = new Label ("  Bond Paper  ");
    Label BPR09p = new Label ("  1.00PHP    ");

    Label c10 = new Label ("OPR10");
    Label OPR10pn = new Label ("  Oslo Paper  ");
    Label OPR10p = new Label ("  1.00PHP    ");

    Label c11 = new Label ("GPR11");
    Label GPR11pn = new Label ("  Graphing Paper  ");
    Label GPR11p = new Label ("  1.00PHP    ");

    Label c12 = new Label ("RLR12");
    Label RLR12pn = new Label ("  Ruler  ");
    Label RLR12p = new Label ("  10.00PHP    ");

    Label c13 = new Label ("PRT13");
    Label PRT13pn = new Label ("  Protractor  ");
    Label PRT13p = new Label ("  10.00PHP    ");

    Label c14 = new Label ("ERS14");
    Label ERS14pn = new Label ("  Eraser  ");
    Label ERS14p = new Label ("  5.00PHP    ");

    Label c15 = new Label ("FLR15");
    Label FLR15pn = new Label ("  Notebook Filler  ");
    Label FLR15p = new Label ("  15.00PHP    ");

    Label c16 = new Label ("SCR16");
    Label SCR16pn = new Label ("  Scissor  ");
    Label SCR16p = new Label ("  10.00PHP    ");
    TextArea TA = new TextArea (10, 10);
    TextArea RP = new TextArea (10, 80);
    TextArea CH = new TextArea (10, 10);
    TextArea TOT = new TextArea (10, 10);


public void init() {
    back.addActionListener(this);
    buttonBUY.addActionListener(this);
    buttonCONFIRM.addActionListener(this);
    buttonCONFIRM1.addActionListener(this);
    close.addActionListener(this);
    card1 =new CardLayout(800,600);
    c.setLayout(card1);
    c.add(Store);
    c.add(lb1);c.add(lb2);c.add(lb3);c.add(lb4);
    c.add(c1);c.add(HBW01pn);c.add(HBW01p);
    c.add(c2);c.add(MNG02pn);c.add(MNG02p);
    c.add(c3);c.add(MRK03pn);c.add(MRK03p);
    c.add(c4);c.add(YLW04pn);c.add(YLW04p);
    c.add(c5);c.add(CRT05pn);c.add(CRT05p);
    c.add(c6);c.add(MNL06pn);c.add(MNL06p);
    c.add(c7);c.add(EGL07pn);c.add(EGL07p);
    c.add(c8);c.add(CPR08pn);c.add(CPR08p);
    c.add(c9);c.add(BPR09pn);c.add(BPR09p);
    c.add(c10);c.add(OPR10pn);c.add(OPR10p);
    c.add(c11);c.add(GPR11pn);c.add(GPR11p);
    c.add(c12);c.add(RLR12pn);c.add(RLR12p);
    c.add(c13);c.add(PRT13pn);c.add(PRT13p);
    c.add(c14);c.add(ERS14pn);c.add(ERS14p);
    c.add(c15);c.add(FLR15pn);c.add(FLR15p);
    c.add(c16);c.add(SCR16pn);c.add(SCR16p);
    c.add(buttonBUY);



} //end of init()


public void actionPerformed (ActionEvent ae) {


    if (ae.getSource() == buttonBUY) {
        second(c);
    }
    if (ae.getSource() == buttonCONFIRM)
     if (Code=="HBW01"){
            Price = 8.00;
            ITname = "HBW Ballpen ";
        }
     else if (Code=="MNG02"){
            Price = 6.00;
            ITname = "Mongol Pencil ";
        }
     else if (Code=="MRK03"){
            Price = 30.00;
            ITname = "Marker";
        }
     else if (Code=="YLW04"){
            Price = 30.00;
            ITname = "Yellow Pad ";
        }
     else if (Code=="CRR05"){
            Price = 10.00;
            ITname = "Cartolina ";
        }
     else if (Code=="MNL06"){
            Price = 6.00;
            ITname = "Manila Paper";
        }
     else if (Code=="EGL07"){
            Price = 12.00;
            ITname = "Elmer’s Glue ";
        }
     else if (Code=="CPR08"){
            Price = 1.00;
            ITname = "Colored Paper ";
        }
     else if (Code=="BPR09"){
            Price = 1.00;
            ITname = "Bond Paper ";
        }
     else if (Code=="OPR10"){
            Price = 1.00;
            ITname = "Oslo Paper ";
        }
     else if (Code=="GPR11"){
            Price = 1.00;
            ITname = "Graphing Paper ";
        }
     else if (Code=="RLR12"){
            Price = 10.00;
            ITname = "Ruler";
        }
     else if (Code=="PTR13"){
            Price = 10.00;
            ITname = "Protractor";
        }
     else if (Code=="ERS14"){
            Price = 5.00;
            ITname = "Eraser ";
        }
     else if (Code=="FLR15"){
            Price = 15.00;
            ITname = "Notebook Filler ";
        }
     else if (Code=="SCR16"){
            Price = 10.00;
            ITname = "Scissor";
        }
     else{
         error(d);
     }
    if (ae.getSource()== back){
        second(c);
    }
    if (ae.getSource() == close){
        System.exit(0);
    }
}



public void second(Container d){
    Label Enter = new Label("Enter Code");
    d.add(Enter);
    d.add(TA); d.add(buttonCONFIRM);
    String Code = TA.getText();
    third(d);







}
public void third(Container e){
    Label Quantity = new Label("Enter  Quantity");
    e.add(Quantity);
    quan = Quantity.getText();
    qty = Integer.parseInt(quan);
    Subtotal = Price * qty;
    Total = Total + Subtotal;
    e.add(buttonCONFIRM1);



}
public void error(Container f){
    Label derror = new Label("Code Not Found");
    f.add(derror);
    f.add(back);

}
public void ask(Container g){
    RP.append(flb1.getText() + "\t" + flb2.getText() + "\t" + flb3.getText() + "\t" + flb4.getText() + "\t  TOTAL");
    RP.append(ITname + "\t" + quan + "\t" + Price + "\t" + Subtotal);

    Label eof = new Label("Would you like to have another transaction? [Y/N]" );
    g.add(eof); g.add(CH);
    choice = CH.getText();
    if (choice == "Y"){
        second(g);
    }
    else 
        last(g);
}
public void last(Container h){
    Label T = new Label ("Total is:");
    String Final = String.valueOf(Total);
    TOT.append(Final);
    h.add(RP);
    h.add(T); h.add(TOT);
    h.add(close);


}
}

Replace this line :

Container c, d;

With this one :

Container c = new Container(), d = new Container();

Because you're trying to apply a Layout to an unexisting object with a null reference.

For the rest, the applet launches but it printed exceptions in the console. You should keep an eye on it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM