0
import javax.swing.*;

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

class MyCalculator extends JFrame implements ActionListener {
   static JFrame jf;
   static JTextField t;

   static double a = 0, b = 0, result = 0;
   static int operation = 0;

   MyCalculator() {
       jf = new JFrame("MyCalculator");
       try {
           UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
       } catch (Exception e) {
           System.out.println(e.getMessage());
       }

       t = new JTextField(10);
       t.setBounds(50, 10, 200, 50);
       t.setEditable(false);


       JPanel p = new JPanel();

       JButton b1, b2, b3, b4, b5, b6, b7, b8, b9, b0, badd, bsub, bmul, bdiv, beq, be, bclr;

       b1 = new JButton("1");
       b2 = new JButton("2");
       b3 = new JButton("3");
       b4 = new JButton("4");
       b5 = new JButton("5");
       b6 = new JButton("6");
       b7 = new JButton("7");
       b8 = new JButton("8");
       b9 = new JButton("9");
       b0 = new JButton("0");
       badd = new JButton("+");
       bsub = new JButton("-");
       bmul = new JButton("*");
       bdiv = new JButton("/");
       beq = new JButton("=");
       be = new JButton(".");
       bclr = new JButton("C");

       p.add(t);
       p.add(bclr);
       p.add(b1);
       p.add(b2);
       p.add(b3);
       p.add(badd);
       p.add(b4);
       p.add(b5);
       p.add(b6);
       p.add(bsub);
       p.add(b7);
       p.add(b8);
       p.add(b9);
       p.add(bmul);
       p.add(be);
       p.add(b0);
       p.add(beq);
       p.add(bdiv);

       p.setBackground(Color.blue);

       jf.add(p);
       jf.setSize(210, 210);
       jf.setVisible(true);

       b1.addActionListener(this);
       b2.addActionListener(this);
       b3.addActionListener(this);
       badd.addActionListener(this);
       b4.addActionListener(this);
       b5.addActionListener(this);
       b6.addActionListener(this);
       bsub.addActionListener(this);
       b7.addActionListener(this);
       b8.addActionListener(this);
       b9.addActionListener(this);
       bmul.addActionListener(this);
       be.addActionListener(this);
       b0.addActionListener(this);
       beq.addActionListener(this);
       bdiv.addActionListener(this);
       bclr.addActionListener(this);
   }

   @Override
   public void actionPerformed(ActionEvent e) {
       if (e.getSource() == b1) {
           t.setText(t.getText().concat("1"));
       }
       if (e.getSource() == b2) {
           t.setText(t.getText().concat("2"));
       }
       if (e.getSource() == b3) {
           t.setText(t.getText().concat("3"));
       }
       if (e.getSource() == b4) {
           t.setText(t.getText().concat("4"));
       }
       if (e.getSource() == b5) {
           t.setText(t.getText().concat("5"));
       }
       if (e.getSource() == b6) {
           t.setText(t.getText().concat("6"));
       }
       if (e.getSource() == b7) {
           t.setText(t.getText().concat("7"));
       }
       if (e.getSource() == b8) {
           t.setText(t.getText().concat("8"));
       }
       if (e.getSource() == b9) {
           t.setText(t.getText().concat("9"));
       }
       if (e.getSource() == b0) {
           t.setText(t.getText().concat("0"));
       }
       if (e.getSource() == be) {
           t.setText(t.getText().concat("."));
       }
       if (e.getSource() == badd) {
           a = Double.parseDouble(t.getText());
           operation = 1;
           t.setText("");
           String s = "";
           s = Double.toString(a);
       }
       if (e.getSource() == bsub) {
           a = Double.parseDouble(t.getText());
           operation = 2;
           t.setText("");
           String s = "";
           s = Double.toString(a);
       }
       if (e.getSource() == bmul) {
           a = Double.parseDouble(t.getText());
           operation = 3;
           t.setText("");
           String s = "";
           s = Double.toString(a);
       }
       if (e.getSource() == bdiv) {
           a = Double.parseDouble(t.getText());
           operation = 4;
           t.setText("");
           String s = "";
           s = Double.toString(a);
       }
       if (e.getSource() == beq) {
           b = Double.parseDouble(t.getText());

           switch (operation) {
               case 1:
                   result = a + b;
                   break;
               case 2:
                   result = a - b;
                   break;
               case 3:
                   result = a * b;
                   break;
               case 4:
                   result = a / b;
                   break;
               default:
                   result = 0;
           }
           t.setText("" + result);

       }
       if (e.getSource() == bclr) {
           t.setText("");
           String s = "";
           t.setText("");
       }
   }

   public static void main(String args[]) {
       MyCalculator c = new MyCalculator();
       jf.setVisible(true);

   }
}

Error:

MyCalculator.java:104: error: cannot find symbol
  if(e.getSource()==b1)
                    ^
  symbol:   variable b1
  location: class MyCalculator
MyCalculator.java:108: error: cannot find symbol
  if(e.getSource() == b2)
                      ^
  symbol:   variable b2
  location: class MyCalculator
MyCalculator.java:112: error: cannot find symbol
  if(e.getSource() == b3)
                      ^
  symbol:   variable b3
  location: class MyCalculator
MyCalculator.java:116: error: cannot find symbol
  if(e.getSource() == b4)
                      ^
  symbol:   variable b4
  location: class MyCalculator
MyCalculator.java:120: error: cannot find symbol
  if(e.getSource() == b5)
                      ^
  symbol:   variable b5
  location: class MyCalculator
MyCalculator.java:124: error: cannot find symbol
  if(e.getSource() == b6)
                      ^
  symbol:   variable b6
  location: class MyCalculator
MyCalculator.java:128: error: cannot find symbol
  if(e.getSource() == b7)
                      ^
  symbol:   variable b7
  location: class MyCalculator
MyCalculator.java:132: error: cannot find symbol
  if(e.getSource() == b8)
                      ^
  symbol:   variable b8
  location: class MyCalculator
MyCalculator.java:136: error: cannot find symbol
  if(e.getSource() == b9)
                      ^
  symbol:   variable b9
  location: class MyCalculator
MyCalculator.java:140: error: cannot find symbol
  if(e.getSource() == b0)
                      ^
  symbol:   variable b0
  location: class MyCalculator
MyCalculator.java:144: error: cannot find symbol
  if(e.getSource() == be)
                      ^
  symbol:   variable be
  location: class MyCalculator
MyCalculator.java:148: error: cannot find symbol
  if(e.getSource() == badd)
                      ^
  symbol:   variable badd
  location: class MyCalculator
MyCalculator.java:156: error: cannot find symbol
    if(e.getSource() == bsub)
                        ^
  symbol:   variable bsub
  location: class MyCalculator
MyCalculator.java:164: error: cannot find symbol
    if(e.getSource() == bmul)
                        ^
  symbol:   variable bmul
  location: class MyCalculator
MyCalculator.java:172: error: cannot find symbol
    if(e.getSource() == bdiv)
                        ^
  symbol:   variable bdiv
  location: class MyCalculator
MyCalculator.java:180: error: cannot find symbol
    if(e.getSource() == beq)
                        ^
  symbol:   variable beq
  location: class MyCalculator
MyCalculator.java:204: error: cannot find symbol
    if(e.getSource() == bclr)
                        ^
  symbol:   variable bclr
  location: class MyCalculator
17 errors
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 1
    Please [edit] your question to format your code properly. – maloomeister Nov 25 '20 at 07:22
  • 1
    The issue here is, that you are referencing all of your `JButton`s from your `actionPerformed` method, but these buttons are only in the scope of the constructor of `MyCalculator`. See more information on the [Scope of Variables in Java](https://www.java-made-easy.com/variable-scope.html). To fix this, make them class level variables. – maloomeister Nov 25 '20 at 07:27
  • 1
    Please add a bit of text to your question to describe your problem next time and not only code and your stack trace. – Milgo Nov 25 '20 at 07:29

1 Answers1

1

You define many variables only in your constructor but use these outside of your constructor. You have to move the declaration of your buttons outside of your constructor.

class MyCalculator extends JFrame implements ActionListener
{
    static JFrame jf;
    static JTextField t;

    static double a=0,b=0,result=0;
    static int operation=0;

    private JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,badd,bsub,bmul,bdiv,beq,be,bclr;

    MyCalculator()
    {

        jf = new JFrame("MyCalculator");
        try
        {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());
        }

        t = new JTextField(10);
        t.setBounds(50,10,200,50);
        t.setEditable(false);


        JPanel p = new JPanel();

The error message cannot find symbol means that your are using something that is not defined. In your case, you use e.g. the variable b1 which is not defined for your method actionPerformed() but only inside the constructor.

Milgo
  • 2,617
  • 4
  • 22
  • 37