Reputation: 1
There is the problem overriding the function actionPerformed , it says unable to find the symbols op1, op2 & add. What am i doing wrong ? Note : This isnt the complete program. I am trying to make a simple program to add,divide,subtract and multiply the numbers fetched from the textfields and then displaying it in the another textfield named result.
package arithmetic;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class Arithmetic extends JFrame implements ActionListener {
private JPanel Pbutton;
private JPanel Ptext ;
private JPanel result;
private JPanel main;
Arithmetic () {
main = new JPanel();
Pbutton = new JPanel();
Ptext = new JPanel ();
result = new JPanel();
JButton add = new JButton("ADD");
JButton div = new JButton("DIVIDE");
JButton sub = new JButton("SUBTRACT");
JButton mul = new JButton("MULTIPLY");
JButton ex = new JButton ("EXIT");
JTextField op1 = new JTextField(10);
JTextField op2 = new JTextField(10);
JTextField res = new JTextField(10);
JLabel frst = new JLabel("Operand 1");
JLabel sec = new JLabel("Operand 2");
JLabel rel = new JLabel ("Result");
main.add(Pbutton);
main.add(Ptext);
main.add(result);
Ptext.add(frst);
Ptext.add(op1);
Ptext.add(sec);
Ptext.add(op2);
add(main);
Pbutton.add(add);
Pbutton.add(div);
Pbutton.add(mul);
Pbutton.add(sub);
result.add(rel);
result.add(res);
result.add(ex);
add.addActionListener(this);
sub.addActionListener(this);
mul.addActionListener(this);
div.addActionListener(this);
ex.addActionListener(this);
setSize(800,145);
Pbutton.setBackground(Color.BLACK);
Ptext.setBackground(Color.BLACK);
result.setBackground(Color.BLACK);
setVisible(true);
setLayout(null);
main.setLayout(new BoxLayout(main,BoxLayout.X_AXIS));
Pbutton.setLayout( new BoxLayout(Pbutton, BoxLayout.Y_AXIS));
}
@Override
public void actionPerformed(ActionEvent a) {
if (a.getSource() == add) {
int sum = op1.getText() + op2.getText();
}
}
public static void main(String[] args) {
new Arithmetic ();
}
}
Upvotes: 0
Views: 2588
Reputation: 347194
You have a context issue...add
, op1
and op2
are declared within a local context of the constructor...
Arithmetic() {
//...
JButton add = new JButton("ADD");
//...
JTextField op1 = new JTextField(10);
JTextField op2 = new JTextField(10);
JTextField res = new JTextField(10);
This means that they can not be accessed from any other context
Instead, declare them as instance/class fields, for example...
private JButton add;
private JTextField op1;
private JTextField op2;
private JTextField res;
Arithmetic() {
//...
add = new JButton("ADD");
//...
op1 = new JTextField(10);
op2 = new JTextField(10);
res = new JTextField(10);
Oddly enough, you will probably want to do the same for you div
, sub
, mul
, and ex
buttons as well as your res
field...
Take a look at Understanding Class Members for more details
I would also suggest having a look at How to Use Actions, which might help simplify your code
The next problem you will face will be...
int sum = op1.getText() + op2.getText();
As getText
returns a String
, which can't be assigned to an int
. You will need to parse the results, for example...
int sum = Integer.parseInt(op1.getText()) + Integer.parseInt(op2.getText());
This is not the best idea in the world, as the JTextField
allows the user to enter whatever they like, meaning that they might enter some text which can't be parsed. Now you could trap the NumberFormatException
which Integer.parseInt
will throw when it can't parse a value, or your could use a JFormattedTextField
or JSpinner
to validate the user input directly...
Have a look at How to Use Formatted Text Fields and How to Use Spinners for more details
Upvotes: 1
Reputation: 7894
You need to make op1 and op2 instance members of the Arithmetic class.
public class Arithmetic extends JFrame implements ActionListener {
//...
private JTextField op1;
private JTextField op2;
Arithmetic () {
this.op1 = new JTextField(10);
this.op2 = new JTextField(10);
}
//...
@Override
public void actionPerformed(ActionEvent a) {
if (a.getSource() == add) {
int sum = this.op1.getText() + this.op2.getText();
}
}
}
People will say that using this is redundant here but I think it makes the code clearer.
Upvotes: 1
Reputation: 1054
You are declaring variables op1, op2 and other only in the constructor, so they are local variables. The method actionPerformed() does not have access to the local variables of other methods - in your case the constructor.
Make them member variables of the class as you did with result
for example.
Upvotes: 1
Reputation: 121998
That op1
and op2
are limited to the scope of constructor. To avail them in whole class, move them to top (instance members).
Like
public class Arithmetic extends JFrame implements ActionListener {
private JTextField op1,op2;
later initialize them in constructor.
Arithmetic() {
op1 = new JTextField(10);
op2 = new JTextField(10);
//.....
Upvotes: 1