-1

I am trying to add an ActionListener to one of the buttons in my Java program, but its method causes an error to occur. This is my code :

import javax.swing.*;//import the packages needed for gui
import java.awt.*;
import java.awt.event.*;
public class Calculator {
    public static void main(String[] args) {
        JFrame window = new JFrame("Window");// makes a JFrame
        window.setSize(300, 350);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel(new FlowLayout());// makes the panel,
                                                    // textfield and buttons
        JTextField textField = new JTextField(20);
        JButton openbracket = new JButton("(");
        JButton closebracket = new JButton(")");
        JButton clearbutton = new JButton("C");
        JButton arcsin = new JButton("arcsin");
        JButton arccos = new JButton("arccos");
        JButton arctan = new JButton("arctan");
        JButton sin = new JButton("sin");
        JButton cos = new JButton("cos");
        JButton tan = new JButton("tan");
        JButton log = new JButton("log");
        JButton seven = new JButton("7");
        JButton eight = new JButton("8");
        JButton nine = new JButton("9");
        JButton four = new JButton("4");
        JButton five = new JButton("5");
        JButton six = new JButton("6");
        JButton one = new JButton("1");
        JButton two = new JButton("2");
        JButton three = new JButton("3");
        JButton zero = new JButton("0");
        JButton radixpoint = new JButton(".");
        JButton equal = new JButton("=");
        String values = null;
        class Listener implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                values = values + "3";
                textField.setText(values);
            }
        }
        panel.add(textField);// adding all the things
        window.add(panel);
        panel.add(openbracket);
        panel.add(closebracket);
        panel.add(clearbutton);
        panel.add(arcsin);
        panel.add(arccos);
        panel.add(arctan);
        panel.add(sin);
        panel.add(cos);
        panel.add(tan);
        panel.add(log);
        panel.add(nine);
        panel.add(eight);
        panel.add(seven);
        panel.add(six);
        panel.add(five);
        panel.add(four);
        panel.add(three);
        panel.add(two);
        panel.add(one);
        panel.add(zero);
        panel.add(radixpoint);
        panel.add(equal);
        window.setVisible(true);
    }
}

The error messages I got were:

Calculator.java:36: local variable values is accessed from within inner class; needs to be declared final values = values + "3"; ^

Calculator.java:36: local variable values is accessed from within inner class; needs to be declared final values = values + "3"; ^

Calculator.java:37: local variable values is accessed from within inner class; needs to be declared final textField.setText(values); ^

Calculator.java:37: local variable textField is accessed from within inner class; needs to be declared final textField.setText(values); ^

4 errors

The compiler wants me to declare the String object and JTextField object as final, but if I declare them as final, I won't be able to change them. How do I make my calculator work without changing the objects?

Syam S
  • 8,421
  • 1
  • 26
  • 36

1 Answers1

1

Well, in order to make this code to compile you need to move String values and JTextField textField to be static members of main. This is because those from static main method.

public class Calculator {

static String values = null;
static JTextField textField = new JTextField(20);
...
Frakcool
  • 10,915
  • 9
  • 50
  • 89
akm
  • 34
  • 4