开始:Applet未在计算器程序中初始化

时间:2017-04-11 07:33:09

标签: java initialization applet

这是我的大学里的计算机程序实现它的代码,并且在执行AppletViewer时我将错误视为Start:Applet Not Initialised我目前使用notepad ++和cmd执行。

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code=Cal.class" width=300 height=300>
</applet>
*/
class Cal extends Applet implements ActionListener {
    String msg=" ";
    int num1,num2,result;
    TextField T1;
    Button NumButtons[]=new Button[10];
    Button Add,Sub,Mul,Div,Clear,Mod,EQ;
    char Operation;
    Panel nPanel,cPanel,sPanel;
    public void init() {
        T1=new TextField(30);
        nPanel=new Panel();
        nPanel.setBackground(Color.GREEN);
        nPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
        nPanel.add(T1);
        T1.addActionListener(this);
        cPanel=new Panel();
        cPanel.setBackground(Color.BLUE);
        cPanel.setLayout(new GridLayout(5,5,3,3));
        for(int i=0;i<10;i++) {
            NumButtons[i]=new Button(""+i);
        }
        Add=new Button("+");
        Sub=new Button("-");
        Mul=new Button("*");
        Div=new Button("/");
        Mod=new Button("%");
        Clear=new Button("Clear");
        EQ=new Button("=");
        for(int i=0;i<10;i++) {
            cPanel.add(NumButtons[i]);
        }
        cPanel.add(Add);
        cPanel.add(Sub);
        cPanel.add(Mul);
        cPanel.add(Div);
        cPanel.add(Mod);
        cPanel.add(EQ);
        for(int i=0;i<10;i++) {
            NumButtons[i].addActionListener(this);
        }
        Add.addActionListener(this);
        Sub.addActionListener(this);
        Mul.addActionListener(this);
        Div.addActionListener(this);
        Mod.addActionListener(this);
        Clear.addActionListener(this);
        EQ.addActionListener(this);
        sPanel=new Panel();
        sPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
        sPanel.setBackground(Color.YELLOW);
        sPanel.add(Clear);
        this.setLayout(new BorderLayout());
        add(nPanel,BorderLayout.NORTH);
        add(cPanel,BorderLayout.CENTER);
        add(sPanel,BorderLayout.SOUTH);
    }

    public void actionPerformed(ActionEvent ae) {
        String str=ae.getActionCommand();
        char ch=str.charAt(0);
        if(Character.isDigit(ch))
        T1.setText(T1.getText()+str);
        else if(str.equals("+")) {
            num1=Integer.parseInt(T1.getText());
            Operation='+';
            T1.setText("");
        } else if(str.equals("-")) {
            num1=Integer.parseInt(T1.getText());
            Operation='-';
            T1.setText("");
        } else if(str.equals("*")) {
            num1=Integer.parseInt(T1.getText());
            Operation='*';
            T1.setText("");
        } else if(str.equals("/")) {
            num1=Integer.parseInt(T1.getText());
            Operation='/';
            T1.setText("");
        } else if(str.equals("%")) {
            num1=Integer.parseInt(T1.getText());
            Operation='%';
            T1.setText("");
        }
        if(str.equals("=")) {
            num2=Integer.parseInt(T1.getText());
            switch(Operation) {
                case '+':result=num1+num2;
                break;
                case '-':result=num1-num2;
                break;
                case '*':result=num1*num2;
                break;
                case '/':result=num1/num2;
                break;
                case '%':result=num1%num2;
                break;
            }
            T1.setText(""+result);
        }
        if(str.equals("clear")) {
            T1.setText("");
        }
    }
}

1 个答案:

答案 0 :(得分:0)

两次更改

  1. 您的班级“Cal”必须是公开的
  2. 小程序标签属性代码应为“Cal”

    applet code =“Cal”width = 300 height = 300&gt;

  3. 使用命令appletviewer Cal.java

  4. 运行applet