0
import java.awt.*;
import java.awt.event.*;
class Calc extends Frame implements ActionListener
{
    Calc()
 {
    TextField t1,t2,t3;
    t1=new TextField();
    t2=new TextField(),
    t3=new TextField();
    Label l1=new Label("Number one"),l2=new Label("Number two"),l3=new Label("Answer :");
    Button b1=new Button("Add"),b2=new Button("Subtract"),b3=new Button("Exit");
    add(l1);
    add(l2);
    add(l3);
    add(t1);
    add(t2);
    add(t3);
    add(b1);
    add(b2);
    add(b3);
    setLayout("500");
    setVisible(True);
    setSize(400,350);
    setBackground(Color.GREEN);
    b1.addActionListener(this);
    b2.addActionListener(this);
    b3.addActionListener(this);
 }  
    public void actionPerfomed(ActionEvent e)
    {
        int n1=Integer.parseInt(t1.getText());
        int n2=Integer.parseInt(t2.getText());
        if(e.getSource==b1)
        {
            t3.setText(String.valueOf(n1+n2));
        }
        else if(e.getSource==b2)
        {
            t3.setText(String.valueOf(n1-n2));
        }
        else if(e.getSource==b3)
        System.exit(0);
    }
}
class GUIAmain
{
    public static void main(String args[])
    {
        new Calc();
    }
}

This is the error I'm getting:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
        Syntax error on tokens, delete these tokens
        The type Calc must implement the inherited abstract method ActionListener.actionPerformed(ActionEvent)
        The method setLayout(LayoutManager) in the type Container is not applicable for the arguments (int)   
        True cannot be resolved to a variable
        t1 cannot be resolved
        t2 cannot be resolved
        getSource cannot be resolved or is not a field
        b1 cannot be resolved to a variable
        t3 cannot be resolved
        getSource cannot be resolved or is not a field
        b2 cannot be resolved to a variable
        t3 cannot be resolved
        getSource cannot be resolved or is not a field
        b3 cannot be resolved to a variable
        The method Exit(int) is undefined for the type System

        at Calc.<init>(Calc.java:1)
        at GUIAmain.main(GuiAmain.java:50)
khelwood
  • 55,782
  • 14
  • 81
  • 108
  • 2
    You need to fix the compilation errors *before* you try to run your code. – Stephen C Jan 24 '22 at 09:42
  • 1
    `t1, t2, t3, b1, b2` are local variables in your constructor. They are not available to other methods. Perhaps you intended them to be instance variables. – khelwood Jan 24 '22 at 09:46
  • Your code is full of typos. eG `t2=new TextField(),` <- you use `,` instead of `;` and in `setVisible(True);` you misspelled `true` with a capital T (Java is case sensitive). On top of that you don't seem to to know what scope is in java because you try to reference local variables from your constructor in other methods. With that many error I would recommend reading [What is 'scope' in Java?](https://stackoverflow.com/questions/38177140/what-is-scope-in-java) and then starting over and starting small instead of typing down lots of code without ever testing it inbetween. – OH GOD SPIDERS Jan 24 '22 at 09:48

0 Answers0