3

I am a java beginner and I've just made my first calculator but it is not compiling. It is showing some problems during compilation, at frame.setPreferredSize(new Dimension(200, 250)); and frame.setDefaultCloserOperation(JFrame.EXIT_ON_CLOSE): These errors are being shown as <identifier> expected and illegal start of expression. What is the problem?

import java.sql.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.awt.*;


public class Cal extends JFrame implements ActionListener
{

JFrame frame = new JFrame();
    frame.setPreferredSize(new Dimension(200, 250));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


public Cal(){             

    Button btn_1 = new Button("1");
    btn_1.setSize(5, 5);
    Button btn_2 = new Button("2");
    btn_2.setSize(5, 5);
    Button btn_3 = new Button("3");
    btn_3.setSize(5, 5);
Button btn_4 = new Button("4");
    btn_4.setSize(5, 5);
Button btn_5 = new Button("5");
    btn_5.setSize(5, 5);
    Button btn_6 = new Button("6");
    btn_6.setSize(5, 5);
    Button btn_7 = new Button("7");
    btn_7.setSize(5, 5);
Button btn_8 = new Button("8");
    btn_8.setSize(5, 5);
Button btn_9 = new Button("9");
    btn_9.setSize(5, 5);
Button btn_0 = new Button("0");
    btn_0.setSize(5, 5);
Button btn_dot = new Button(".");
    btn_dot.setSize(5, 5);
Button btn_div = new Button("/");
    btn_div.setSize(5, 5);
Button btn_mult = new Button("*");
    btn_mult.setSize(5, 5);
Button btn_sub = new Button("-");
    btn_sub.setSize(5, 5);
Button btn_addd = new Button("+");
    btn_addd.setSize(5, 5); 
Button btn_equ = new Button("=");
    btn_equ.setSize(5, 5);

JTextField jt = new JTextField();
jt.setHorizontalAlignment(JTextField.RIGHT);
jt.setEditable(false);


double fnum, snum, total;
String op = null;



JPanel players = new JPanel();   

players.setLayout(new GridLayout(1, 1));
players.add(jt, BorderLayout.NORTH);
players.setPreferredSize(new Dimension(10, 50));

JPanel players1 = new JPanel(new GridLayout(4, 3)); // adding buttons  
players1.add(btn_7);
    players1.add(btn_8);
players1.add(btn_9);
players1.add(btn_div);

players1.add(btn_4);
players1.add(btn_5);
    players1.add(btn_6);
players1.add(btn_mult);

    players1.add(btn_1);
players1.add(btn_2);
    players1.add(btn_3);
players1.add(btn_sub);        


players1.add(btn_0);    
players1.add(btn_dot);
players1.add(btn_equ);
players1.add(btn_addd);
    players1.setPreferredSize(new Dimension(150, 150));

//applying actionlistener

btn_1.addActionListener(this);
btn_2.addActionListener(this);
btn_3.addActionListener(this);
btn_4.addActionListener(this);
btn_5.addActionListener(this);
btn_6.addActionListener(this);
btn_7.addActionListener(this);
btn_8.addActionListener(this);
btn_9.addActionListener(this);
btn_0.addActionListener(this);
btn_dot.addActionListener(this);
btn_addd.addActionListener(this);
btn_mult.addActionListener(this);
btn_div.addActionListener(this);
btn_sub.addActionListener(this);
btn_equ.addActionListener(this);

//setting contents to be available

JPanel content = new JPanel();
content.setLayout(new BorderLayout());
frame.setContentPane(content);
content.add(players, BorderLayout.NORTH);
content.add(players1, BorderLayout.SOUTH);
frame.setTitle("Calculator");
frame.pack();

frame.setVisible(true);

}

//applying actions to be performed

  public void actionPerformed(ActionListener e){

String input = jt.getText();

if(e.getSource()==btn_1)
    {jt.setText(input + "1");}
else if(e.getSource()==btn_2)
    {jt.setText(input + "2");}
else if(e.getSource()==btn_3)
    {jt.setText(input + "3");}
else if(e.getSource()==btn_4)
    {jt.setText(input + "4");}
else if(e.getSource()==btn_5)
    {jt.setText(input + "5");}
else if(e.getSource()==btn_6)
    {jt.setText(input + "6");}
else if(e.getSource()==btn_7)
    {jt.setText(input + "7");}
else if(e.getSource()==btn_8)
    {jt.setText(input + "8");}
else if(e.getSource()==btn_9)
    {jt.setText(input + "9");}
else if(e.getSource()==btn_0)
    {jt.setText(input + "0");}
else if(e.getSource()==btn_dot)
    {jt.setText(input + ".");}
else if(e.getSource()==btn_addd)
    {
    fnum = Double.parseDouble(jt.getText());
    op = "+";
    jt.setText(" ");
    }
else if(e.getSource()==btn_sub)
    {
    fnum = Double.parseDouble(jt.getText());
    op = "-";
    jt.setText(" ");
    }
else if(e.getSource()==btn_div)
    {
    fnum = Double.parseDouble(jt.getText());
    op = "/";
    jt.setText(" ");
    }
else if(e.getSource()==btn_equ)
    {
    snum = Double.parseDouble(jt.getText());
    if(op.equ("+"))
        {
        total = fnum + snum;
        }
    if(op.equ("-"))
        {
        total = fnum - snum;
        }
    if(op.equ("*"))
        {
        total = fnum * snum;
        }
    if(op.equ("/"))
        {
        total = fnum / snum;
        }

    jt.setText(" " + total);
    }

}
public static void main(String args[]){

Cal obj = new Cal(); }
}
user207421
  • 305,947
  • 44
  • 307
  • 483
Azeem Khalid
  • 145
  • 1
  • 1
  • 7

3 Answers3

4

i think you need to put

frame.setPreferredSize(new Dimension(200, 250));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

inside the Cal() constructor


and about Button and other variables , define it outside the constructor ,Like that:

class Cal extends JFrame implements ActionListener {

    JFrame frame = new JFrame();
    JTextField jt = new JTextField();
    Button btn_1 = new Button("1");
    Button btn_2 = new Button("2");
    .....
    JPanel players1 = new JPanel(new GridLayout(4, 3)); // adding buttons 
    ....

and e is ActionEvent not ActionListener Like this :

public void actionPerformed(ActionEvent e) {...}

and there is no equ method in String class it should be if (op.equals("+")) {}

Alya'a Gamal
  • 5,624
  • 19
  • 34
  • i see it , try to define all JButton in the class not in the constructor , to can read it – Alya'a Gamal Apr 27 '13 at 19:23
  • 75 Errors are there after adding it inside the Constructor. All errors are "Cannot find symbot, jt, getSource etc and all related to jt" and "cannot find variable fnum, snum etc" – Azeem Khalid Apr 27 '13 at 19:29
  • this is because `e` is ActionEvent not ActionListener – Alya'a Gamal Apr 27 '13 at 19:32
  • Thanks a lot Alya'a, Now it is more helpful for me. I'm just a beginner studying in 2nd semester and just have attended 10 lectures of advanced java after studying basics. You gave me a better description of my problem. This was my first experience to implement actionListner therefore I've a limited know-how about it – Azeem Khalid Apr 27 '13 at 19:45
  • I'm very very very glad to see my first GUI calculator functional :) – Azeem Khalid Apr 27 '13 at 20:03
  • check the '*' function , because i didn't see it in your code , and check all the function in your calculator :) and i'm happy for you :) – Alya'a Gamal Apr 27 '13 at 20:04
  • Yes i've checked it and removed it before you identify. Thanks for identifying. All buttons of my Calculator all functional now. I've added some backgrounds colors and font settings. Looking too good now. I've also replaced Button with JButton. – Azeem Khalid Apr 27 '13 at 20:28
2

You are trying to make modifications or execute statements out side of the context of an executable context.

You can only make declarations and assign default values to variables out side of methods and constructors

Move

frame.setPreferredSize(new Dimension(200, 250));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

To within the context of the classes constructor

public Cal(){   
    frame.setPreferredSize(new Dimension(200, 250));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Updated

You are having a number of reference issues. You are define objects within the constructor that you are then wanting to access else where in you code. There is no context for these variables out side of the constructor. Instead, you should be declaring them at the class level, so that they are visible to the whole class.

public class Cal extends JFrame implements ActionListener
{

    private JFrame frame = new JFrame();

    private Button btn_1 = new Button("1");
    private Button btn_2 = new Button("2");
    private Button btn_3 = new Button("3");
    private Button btn_4 = new Button("4");
    private Button btn_5 = new Button("5");
    private Button btn_6 = new Button("6");
    private Button btn_7 = new Button("7");
    private Button btn_8 = new Button("8");
    private Button btn_9 = new Button("9");
    private Button btn_0 = new Button("0");
    private Button btn_dot = new Button(".");
    private Button btn_div = new Button("/");
    private Button btn_mult = new Button("*");
    private Button btn_sub = new Button("-");
    private Button btn_addd = new Button("+");
    private Button btn_equ = new Button("=");
    private JTextField jt = new JTextField();

    public Cal(){             
        frame.setPreferredSize(new Dimension(200, 250));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // This is a bad idea, the layout manager will make these calls redudent...
        btn_1.setSize(5, 5);
        btn_2.setSize(5, 5);
        btn_3.setSize(5, 5);
        btn_4.setSize(5, 5);
        btn_5.setSize(5, 5);
        btn_6.setSize(5, 5);
        btn_7.setSize(5, 5);
        btn_8.setSize(5, 5);
        btn_9.setSize(5, 5);
        btn_0.setSize(5, 5);
        btn_dot.setSize(5, 5);
        btn_div.setSize(5, 5);
        btn_mult.setSize(5, 5);
        btn_sub.setSize(5, 5);
        btn_addd.setSize(5, 5); 
        btn_equ.setSize(5, 5);

        jt.setHorizontalAlignment(JTextField.RIGHT);
        jt.setEditable(false);


        double fnum, snum, total;
        String op = null;



        JPanel players = new JPanel();   

        players.setLayout(new GridLayout(1, 1));
        players.add(jt, BorderLayout.NORTH);
        players.setPreferredSize(new Dimension(10, 50));

        JPanel players1 = new JPanel(new GridLayout(4, 3)); // adding buttons  
        players1.add(btn_7);
        players1.add(btn_8);
        players1.add(btn_9);
        players1.add(btn_div);

        players1.add(btn_4);
        players1.add(btn_5);
        players1.add(btn_6);
        players1.add(btn_mult);

        players1.add(btn_1);
        players1.add(btn_2);
        players1.add(btn_3);
        players1.add(btn_sub);        


        players1.add(btn_0);    
        players1.add(btn_dot);
        players1.add(btn_equ);
        players1.add(btn_addd);
        players1.setPreferredSize(new Dimension(150, 150));

You are also mixing heavy and light weight components, this is never a good idea. Use JButton instead of Button

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • All errors are related to "cannot find variables fnum, snum, jt, getSource()" etc – Azeem Khalid Apr 27 '13 at 19:27
  • That's because you've defined them all locally to within the constructor. None of the other methods can see them... – MadProgrammer Apr 27 '13 at 19:28
  • Thanks a lot. Now it's more helpful for me. I'm gonna try it – Azeem Khalid Apr 27 '13 at 19:43
  • Now it is more helpful for me. I'm just a beginner studying in 2nd semester and just have attended 10 lectures of advanced java after studying basics. You gave me a better description of my problem. This was my first experience to implement actionListner therefore I've a limited know-how about it – Azeem Khalid Apr 27 '13 at 19:46
  • 1
    You could take a look at [how to use action listeners](http://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html) and [creating an UI with Swing](http://docs.oracle.com/javase/tutorial/uiswing/index.html) for more details – MadProgrammer Apr 27 '13 at 19:51
  • Thanks a lot. I'm so so soooooo glad to see my first Calculator functional :) – Azeem Khalid Apr 27 '13 at 20:03
-1

I think frame.setPreferredSize(new Dimension(200, 250)); and frame.setDefaultCloserOperation(JFrame.EXIT_ON_CLOSE): need to be inside a method. Commands always need to be inside of methods.