Question

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(); }
}
Was it helpful?

Solution

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("+")) {}

OTHER TIPS

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top