-2

My program does not display the JTextField portion of the calculator, and the clear JButton won't work to reset the calculator, so when I clear the button and try to perform a new calculation, I get a runtime error.BTW the Boolean s is supposed to be operationchosen

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


public class calculator extends JFrame 
 {
private JButton num1;
private JButton num2;
private JButton num3;
private JButton num4;
private JButton num5;
private JButton num6;
private JButton num7;
private JButton num8;
private JButton num9;
private JButton num0;
private JButton equal;
private JButton addition;
private JButton sub;
private JButton mult;
private JButton divide;
private JButton modulus;
private JButton solve;
private JButton clear;
private boolean s=false;
private boolean equalsClicked=false;
private double val;
private String temp1,temp2,answer;
private JTextField answerfield;
private JPanel contentPane;
private char operation=' ';


public calculator(String x)
{
    super(x);
    answerfield=new JTextField();
    answerfield.setEditable(false);

    answerfield.setPreferredSize(new Dimension(160, 20));
    answerfield.setBackground(Color.WHITE);
    answerfield.setEnabled(false);
    answerfield.setHorizontalAlignment(4);
    answerfield.setDisabledTextColor(Color.BLACK);

    contentPane = new JPanel();
    contentPane.setLayout(null);
    contentPane.add(answerfield,BorderLayout.NORTH);




    setBounds(400,300,255,400);
    this.setContentPane(contentPane);


    Numbers n=new Numbers();
    Calculation c=new Calculation();

    num1=new JButton("1");
    num1.setBounds(10, 130, 52, 37);
    add(num1);
    num1.setActionCommand("1");
    num1.addActionListener(n);


    num2=new JButton("2");
    num2.setBounds(72, 130, 52, 37);
    add(num2);
    num2.setActionCommand("2");
    num2.addActionListener(n);


    num3=new JButton("3");
    num3.setBounds(134, 130, 52, 37);
    add(num3);
    num3.setActionCommand("3");
    num3.addActionListener(n);


    num4=new JButton("4");
    num4.setBounds(10, 178, 52, 37);
    add(num4);
    num4.setActionCommand("4");
    num4.addActionListener(n);


    num5=new JButton("5");
    num5.setBounds(72, 178, 52, 37);
    add(num5);
    num5.setActionCommand("5");
    num5.addActionListener(n);


    num6=new JButton("6");
    num6.setBounds(134, 178, 52, 37);
    add(num6);
    num6.setActionCommand("6");
    num6.addActionListener(n);


    num7=new JButton("7");
    num7.setBounds(10, 226, 52, 37);
    add(num7);
    num7.setActionCommand("7");
    num7.addActionListener(n);


    num8=new JButton("8");
    num8.setBounds(72, 226, 52, 37);
    add(num8);
    num8.setActionCommand("8");
    num8.addActionListener(n);



    num9=new JButton("9");
    num9.setBounds(134, 226, 52, 37);
    add(num9);
    num9.setActionCommand("9");
    num9.addActionListener(n);


    num0=new JButton("0");
    num0.setBounds(10, 274, 52, 37);
    add(num0);
    num0.setActionCommand("0");
    num0.addActionListener(n);


    equal=new JButton("=");
    equal.setBounds(72, 274, 52, 37);
    add(equal);
    equal.setActionCommand("=");
    equal.addActionListener(c);


    addition=new JButton("+");
    addition.setBounds(193, 130,45, 35);
    add(addition);
    addition.setActionCommand("+");

    addition.addActionListener(c);

    sub=new JButton("-");
    sub.setBounds(193, 179, 45, 35);
    add(sub);
    sub.setActionCommand("-");

    sub.addActionListener(c);

    mult=new JButton("*");
    mult.setBounds(193,226, 45, 35);
    add(mult);
    mult.setActionCommand("*");

    mult.addActionListener(c);

    divide=new JButton("/");
    divide.setBounds(193, 274, 45, 35);
    add(divide);
    divide.setActionCommand("/");

    divide.addActionListener(c);

    modulus=new JButton("%");
    modulus.setBounds(193,322, 45, 35);
    add(modulus);
    modulus.setActionCommand("%");

    modulus.addActionListener(c);


    clear=new JButton("C");
    clear.setBounds(53, 343, 89, 23);
    add(clear);
    clear.setActionCommand("C");
    clear.addActionListener(c);







    setVisible(true);
    show();
    setResizable(false);


}
private class Numbers implements ActionListener
{
    public void actionPerformed(ActionEvent event)
    {
        JButton x=(JButton)event.getSource();
        if(x.equals(num1))
        {
            if(s==false)
            {
                if(temp1==null)
                {
                    temp1="1";
                }
                else
                    temp1+="1";
            }
            else
            {
                if(temp2==null)
                {
                    temp2="1";
                }
                else
                    temp2+="1";
            }

        }
        if(x.equals(num2))
        {
            if(s==false)
            {
                if(temp1==null)
                {
                    temp1="2";
                }
                else
                    temp1+="2";
            }
            else
            {
                if(temp2==null)
                {
                    temp2="2";
                }
                else
                    temp2+="2";
            }

        }
        if(x.equals(num3))
        {
            if(s==false)
            {
                if(temp1==null)
                {
                    temp1="3";
                }
                else
                    temp1+="3";
            }
            else
            {
                if(temp2==null)
                {
                    temp2="3";
                }
                else
                    temp2+="3";
            }

        }
        if(x.equals(num4))
        {
            if(s==false)
            {
                if(temp1==null)
                {
                    temp1="4";
                }
                else
                    temp1+="4";
            }
            else
            {
                if(temp2==null)
                {
                    temp2="4";
                }
                else
                    temp2+="4";
            }

        }
        if(x.equals(num5))
        {
            if(s==false)
            {
                if(temp1==null)
                {
                    temp1="5";
                }
                else
                    temp1+="5";
            }
            else
            {
                if(temp2==null)
                {
                    temp2="5";
                }
                else
                    temp2+="5";
            }

        }
        if(x.equals(num6))
        {
            if(s==false)
            {
                if(temp1==null)
                {
                    temp1="6";
                }
                else
                    temp1+="6";
            }
            else
            {
                if(temp2==null)
                {
                    temp2="6";
                }
                else
                    temp2+="6";
            }

        }
        if(x.equals(num7))
        {
            if(s==false)
            {
                if(temp1==null)
                {
                    temp1="7";
                }
                else
                    temp1+="7";
            }
            else
            {
                if(temp2==null)
                {
                    temp2="7";
                }
                else
                    temp2+="7";
            }

        }
        if(x.equals(num8))
        {
            if(s==false)
            {
                if(temp1==null)
                {
                    temp1="8";
                }
                else
                    temp1+="8";
            }
            else
            {
                if(temp2==null)
                {
                    temp2="8";
                }
                else
                    temp2+="8";
            }

        }
        if(x.equals(num9))
        {
            if(s==false)
            {
                if(temp1==null)
                {
                    temp1="9";
                }
                else
                    temp1+="9";
            }
            else
            {
                if(temp2==null)
                {
                    temp2="9";
                }
                else
                    temp2+="9";
            }

        }
        if(x.equals(num0))
        {
            if(s==false)
            {
                if(temp1==null)
                {
                    temp1="0";
                }
                else
                    temp1+="0";
            }
            else
            {
                if(temp2==null)
                {
                    temp2="0";
                }
                else
                    temp2+="0";
            }

        }
        if(equalsClicked==false)
        {
            if(s==false)
            {
                answerfield.setText(temp1);
            }
            else
            {
                answerfield.setText(temp2);
            }
        }



    }
}
public void Clear()
{
    temp1="";
    temp2="";
    s=false;
    operation=' ';
    val=0;
    answer="";
    equalsClicked=false;
    System.out.println("hey");
}
private class Calculation implements ActionListener
{
    public void actionPerformed(ActionEvent event)
    {
        JButton x=(JButton)event.getSource();
        if(x.equals(addition))
        {
            if(temp1==null)
                System.out.println("choose numbers");
            else
            {
                if(temp1!=null&&temp2==null)
                {
                    s=true;
                    operation='+';

                }

            }
        }
        if(x.equals(sub))
        {
            if(temp1==null)
                System.out.println("choose numbers");
            else
            {
                if(temp1!=null&&temp2==null)
                {
                    s=true;
                    operation='-';

                }

            }
        }
        if(x.equals(mult))
        {
            if(temp1==null)
                System.out.println("choose numbers");
            else
            {
                if(temp1!=null&&temp2==null)
                {
                    s=true;
                    operation='*';

                }

            }
        }
        if(x.equals(divide))
        {
            if(temp1==null)
                System.out.println("choose numbers");
            else
            {
                if(temp1!=null&&temp2==null)
                {
                    s=true;
                    operation='/';

                }

            }
        }
        if(x.equals(modulus))
        {
            if(temp1==null)
                System.out.println("choose numbers");
            else
            {
                if(temp1!=null&&temp2==null)
                {
                    s=true;
                    operation='%';

                }

            }
        }
        if(x.equals(equal))
        {
            if(temp1==null)
                System.out.println("choose numbers");
            else
            {
                if(temp1!=null&&temp2==null)
                    System.out.println("choose numbers");


            }
            if(temp1!=null&&temp2!=null)
            {
                double d=0.0;
                double s=0.0;
                d=Double.parseDouble(temp1);
                s=Double.parseDouble(temp2);
                switch(operation)
                {
                    case '+':
                    {
                        val=d+s;
                        break;
                    }
                    case '-':
                    {
                        val=d-s;
                        break;
                    }
                    case'*':
                    {
                        val=d*s;
                        break;
                    }
                    case'/':
                    {
                        val=d/s;
                        break;
                    }
                    case'%':
                    {
                        val=d%s;
                        break;
                    }

                }
                answer=Double.toString(val);
                answerfield.setText(answer);
                System.out.println(answer);
            }
        }
        if(x.equals(clear))
        {
            Clear();
        }
    }
}

public static void main(String args[])
{
    new calculator("math");
}

}

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366

1 Answers1

1

First you do this...

answerfield = new JTextField();
answerfield.setEditable(false);

answerfield.setPreferredSize(new Dimension(160, 20));
answerfield.setBackground(Color.WHITE);
answerfield.setEnabled(false);
answerfield.setHorizontalAlignment(4);
answerfield.setDisabledTextColor(Color.BLACK);

Which all seems cool...

But then you do this...

contentPane = new JPanel();
contentPane.setLayout(null);
contentPane.add(answerfield, BorderLayout.NORTH);

...answerfield has no defined size or position, so it's size is 0x0 and it's location is 0x0. Swing is smart enough not to paint it...

The answer is, don't use null layouts...

Avoid using null layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify

Take a look at Why is it frowned upon to use a null layout in SWING? and Laying Out Components Within a Container for more details

Updated with a layout example

This example uses GridBagLayout which is probably one of the most powerful of the default layout managers, but it is also one of the most difficult use...

Layout

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;

public class Calculator extends JFrame {

    private boolean s = false;
    private boolean equalsClicked = false;
    private double val;
    private String temp1, temp2, answer;
    private JTextField answerfield;
    private char operation = ' ';

    public Calculator(String x) {
        super(x);
        answerfield = new JTextField();
        answerfield.setEditable(false);

        answerfield.setBackground(Color.WHITE);
        answerfield.setEnabled(false);
        answerfield.setHorizontalAlignment(4);
        answerfield.setDisabledTextColor(Color.BLACK);
        answerfield.setFont(answerfield.getFont().deriveFont(Font.BOLD, 24));

        JPanel field = new JPanel(new BorderLayout());
        field.setBorder(new EmptyBorder(4, 4, 0, 4));
        field.add(answerfield);
        add(field, BorderLayout.NORTH);

        JPanel numbers = new JPanel(new GridBagLayout());

        Numbers n = new Numbers();
        Calculation c = new Calculation();

        addButtonTo(numbers, "1", n, 0, 0);
        addButtonTo(numbers, "2", n, 1, 0);
        addButtonTo(numbers, "3", n, 2, 0);
        addButtonTo(numbers, "+", c, 3, 0);
        addButtonTo(numbers, "4", n, 0, 1);
        addButtonTo(numbers, "5", n, 1, 1);
        addButtonTo(numbers, "6", n, 2, 1);
        addButtonTo(numbers, "-", c, 3, 1);
        addButtonTo(numbers, "7", n, 0, 2);
        addButtonTo(numbers, "8", n, 1, 2);
        addButtonTo(numbers, "9", n, 2, 2);
        addButtonTo(numbers, "*", c, 3, 2);
        addButtonTo(numbers, "0", n, 0, 3);
        addButtonTo(numbers, "=", c, 1, 3);
        addButtonTo(numbers, "", null, 2, 3);
        addButtonTo(numbers, "/", n, 3, 3);
        addButtonTo(numbers, "C", c, 0, 4, 3);
        addButtonTo(numbers, "%", c, 3, 4);

        add(numbers);

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();
        setVisible(true);

    }

    protected void addButtonTo(JPanel panel, String text, ActionListener listener, int gridX, int gridY) {

        addButtonTo(panel, text, listener, gridX, gridY, 1);

    }

    protected void addButtonTo(JPanel panel, String text, ActionListener listener, int gridX, int gridY, int gridWidth) {

        JComponent comp = null;
        if (text == null || text.trim().isEmpty()) {
            comp = new JPanel();
        } else {

            JButton button = new JButton(text);
            button.setActionCommand(text);
            button.addActionListener(listener);
            comp = button;

        }

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = gridX;
        gbc.gridy = gridY;
        gbc.gridwidth = gridWidth;
        gbc.insets = new Insets(4, 4, 4, 4);
        gbc.fill = GridBagConstraints.BOTH;

        panel.add(comp, gbc);

    }

    private class Numbers implements ActionListener {

        public void actionPerformed(ActionEvent event) {
            JButton x = (JButton) event.getSource();
            String text = x.getActionCommand();

            if (s == false) {
                if (temp1 == null) {
                    temp1 = text;
                } else {
                    temp1 += text;
                }
            } else {
                if (temp2 == null) {
                    temp2 = text;
                } else {
                    temp2 += text;
                }
            }

            if (equalsClicked == false) {
                if (s == false) {
                    answerfield.setText(temp1);
                } else {
                    answerfield.setText(temp2);
                }
            }

        }
    }

    public void Clear() {
        temp1 = "";
        temp2 = "";
        s = false;
        operation = ' ';
        val = 0;
        answer = "";
        equalsClicked = false;
        System.out.println("hey");
        answerfield.setText("");
    }

    private class Calculation implements ActionListener {

        public void actionPerformed(ActionEvent event) {
            JButton x = (JButton) event.getSource();
            String text = x.getActionCommand();
            System.out.println(text);
            switch (text) {
                case "+":
                case "-":
                case "*":
                case "/":
                case "%":
                    if (temp1 == null) {
                        System.out.println("choose numbers");
                    } else {
                        if (temp1 != null && temp2 == null) {
                            s = true;
                            operation = text.charAt(0);
                        }
                    }
                    break;
                case "=":
                    if (temp1 == null) {
                        System.out.println("choose numbers");
                    } else {
                        if (temp1 != null && temp2 == null) {
                            System.out.println("choose numbers");
                        }

                    }
                    if (temp1 != null && temp2 != null) {
                        double d = 0.0;
                        double s = 0.0;
                        d = Double.parseDouble(temp1);
                        s = Double.parseDouble(temp2);
                        switch (operation) {
                            case '+': {
                                val = d + s;
                                break;
                            }
                            case '-': {
                                val = d - s;
                                break;
                            }
                            case '*': {
                                val = d * s;
                                break;
                            }
                            case '/': {
                                val = d / s;
                                break;
                            }
                            case '%': {
                                val = d % s;
                                break;
                            }

                        }
                        answer = Double.toString(val);
                        answerfield.setText(answer);
                        System.out.println(answer);
                    }
                    break;
                case "C":
                    Clear();
                    break;
            }
        }
    }

    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                new Calculator("math");
            }
        });
    }
}
Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • For this particular assignment I had to set the layout to null...so what do you recommend that I change my code to – user4216980 Nov 05 '14 at 04:06
  • For this particular assignment I had to set the layout to null...so what do you recommend that I change my code to – user4216980 Nov 05 '14 at 04:08
  • *"For this particular assignment I had to set the layout to null"* - Why? You could use a combination of `BorderLayout` and `GridLayout` or if you up for a challenge, `GridBagLayout`... – MadProgrammer Nov 05 '14 at 04:09
  • So what exactly should I delete in my program......and any ideas on why the clear button wont work – user4216980 Nov 05 '14 at 04:11
  • @user4216980 You clear button seems to work just fine, but you never clear the `answerField` – MadProgrammer Nov 05 '14 at 05:42