0

I am trying to figure out how to implement a few buttons in the current code but after many attempts, I can't seems to figure it out. I need to do the following:

So my question is: When I add 2 numbers together and if they are not 0 (for example x=10 and y=10), when I click on the plus button the contents in the y field disappear, how can I fix this?

Also my clear button doesn't clear all the contents in the x, y and result field, how do I fix this?

How do I right align my x and y fields?

package democalc;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;


class Calculator implements ActionListener {

    private JFrame frame;
    private JTextField xfield, yfield;
    private JLabel rslt;
    private JButton multiButton, clearButton, addButton, subButton, divideButton;
    private JPanel xpanel, buttonPanel;

    public Calculator() {

        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());

        xpanel = new JPanel();
        xpanel.setBorder(new EtchedBorder());
        xpanel.setLayout(new GridLayout(3, 2));

        xpanel.add(new JLabel("x ="));
        xfield = new JTextField("0", 5);
        xpanel.add(xfield);

        xpanel.add(new JLabel("y ="));
        yfield = new JTextField("0", 5);
        xpanel.add(yfield);

        xpanel.add(new JLabel());
        rslt = new JLabel("0");
        rslt.setBackground(Color.green);
        rslt.setBorder(new EtchedBorder());
        rslt.setOpaque(true);
        xpanel.add(rslt);
        frame.add(xpanel, BorderLayout.NORTH);

        buttonPanel = new JPanel();
        frame.add(buttonPanel, BorderLayout.CENTER);

        buttonPanel.add(addButton = new JButton("+"));
        addButton.addActionListener(this);

        buttonPanel.add(subButton = new JButton("-"));
        subButton.addActionListener(this);

        buttonPanel.add(multiButton = new JButton("*"));
        multiButton.addActionListener(this);

        buttonPanel.add(divideButton = new JButton("/"));
        divideButton.addActionListener(this);

        buttonPanel.add(clearButton = new JButton("Clear"));
        clearButton.addActionListener(this);

        frame.pack();
        frame.setVisible(true);
    }

    public void actionPerformed(ActionEvent event) {

        int x = 0, y = 0;

        try {
            String xStr = xfield.getText();
            x = Integer.parseInt(xStr);
        }
        catch (NumberFormatException e) {
                // The string xStr is not a legal number.
            rslt.setText("Illegal data for x.");
            xfield.requestFocusInWindow();
            return;
        }

        /* Get a number from yfield in the same way. */

        try {
            String yStr = yfield.getText();
            y = Integer.parseInt(yStr);
        }
        catch (NumberFormatException e) {
            rslt.setText("Illegal data for y.");
            yfield.requestFocusInWindow();
            return;
        }

        /* Perform the operation based on the action command
             from the button.  Note that division by zero produces
             an error message. */

        String op = event.getActionCommand();
        if (op.equals("+"))
            rslt.setText( "x + y = " + (x+y) );
        else if (op.equals("-"))
            rslt.setText( "x - y = " + (x-y) );
        else if (op.equals("*"))
            rslt.setText( "x * y = " + (x*y) );
        else if (op.equals("/")) {
            if (y == 0)
                rslt.setText("ERROR");
            else
                rslt.setText( "x / y = " + (x/y) );


        }

        if (event.getSource() == clearButton)
            xfield.setText("0");
            yfield.setText("0");


    }
}
  • 3
    What is your question? – Vince Mar 28 '16 at 02:27
  • I have updated my questions and code –  Mar 28 '16 at 03:04
  • 1
    *"I have updated my questions"* On that note, SO is a Q&A site, not a help desk. Ideally each question thread should have ***one*** clear, specific question. Other questions need to be split into separate question threads. – Andrew Thompson Mar 28 '16 at 03:18
  • See also this [calculator example](http://stackoverflow.com/a/7441804/418556). It uses `ScriptEngine` to evaluate the expression in the text field. – Andrew Thompson Mar 28 '16 at 03:19

1 Answers1

1

your if curly brackets Are missing. Thats why

yfield.setText("0");

is executing every time. Replace Your code with Correct code

if (event.getSource() == clearButton){
    xfield.setText("0");
    yfield.setText("0");
}
Vinoth Krishnan
  • 2,925
  • 6
  • 29
  • 34
Pritpal Singh
  • 163
  • 13