Question

I am constructing a simple modified Subtraction-only calculator where z = x - y. I have created 3 textfields: x, y, and z. The user will input the value using the keypad (buttons) I have designed for the x and y.

Just by looking at the code below, there are couple of issues right away.

1) The x textfield only accepts one digit. (I want it to accept up to 3 digits). Any suggestions on how I can amend the else if statement to incorporate that?

2) When I am done with the x textfield, I want to do it with the same with the y textfield. But the buttons are strictly allocated to the x textfield. It would be inefficient to make one more keypad just for y textfield. So how can I switch between x and y text field. Is there some code where if the user has the cursor on the x text field, the keypad is reserved for that x text field and if the user changes to the y textfield, the keypad can be used for the y then.

3) And once user hits enter, its a simple operation z = x - y . Which should be easy to implement. i.e. Convert the strings to integers etc. and perform the difference.

Thank you all in advance!

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

    public class EmbeddedMain extends JFrame
    {
    private JTextField x,y,z;
    private JButton button1, button2, button3, button4, button5, button6, button7, button8, button9, button0, buttonR, buttonE;

        public static void main (String[] args)
            {
            EmbeddedMain em = new EmbeddedMain();
            }

        public EmbeddedMain() //constructor begins, method for embedded main class
        {
            setTitle("Subtraction");
            setSize(450,350);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLocationRelativeTo(null);
            setLayout(new GridLayout(4,5,3,3));
            setVisible(true);   

         button1= new JButton("7");
         button2= new JButton("8");
         button3= new JButton("9");
         button4= new JButton("4");
         button5= new JButton("5");
         button6= new JButton("6");
         button7= new JButton("1");
         button8= new JButton("2");
         button9= new JButton("3");
         button0= new JButton("0");
         buttonR= new JButton("Reset");
         buttonE= new JButton("Enter");

     x = new JTextField("   ");
     y = new JTextField("   ");
     z = new JTextField("   ");
     z.setEditable(false);      

        add(button1);
        add(button2);
        add(button3);
        add(new JLabel("    x:")); 
        add(x); 
        add(button4);
        add(button5);
        add(button6);
        add(new JLabel("    y:")); 
        add(y); 
        add(button7);
        add(button8);
        add(button9);
        add(new JLabel("    z:")); 
        add(z); 
        add(buttonR);
        add(button0);
        add(buttonE);

        thehandler handler = new thehandler();
        button1.addActionListener(handler);
        button2.addActionListener(handler);
        button3.addActionListener(handler); 
        button4.addActionListener(handler);
        button5.addActionListener(handler);
        button6.addActionListener(handler);
        button7.addActionListener(handler);
        button8.addActionListener(handler);
        button9.addActionListener(handler);
        button0.addActionListener(handler);
        buttonR.addActionListener(handler);
        buttonE.addActionListener(handler);

    }

        //building class inside class
        private class thehandler implements ActionListener{

            public void actionPerformed(ActionEvent event){

            if(event.getSource()== button1)
                x.setText("7");
            else if (event.getSource()== button2)
                x.setText("8");
            else if (event.getSource()== button3)
                x.setText("9");
            else if (event.getSource()== button4)
                x.setText("4");
            else if (event.getSource()== button5)
                x.setText("5"); 
            else if (event.getSource()== button6)
                x.setText("6");
            else if (event.getSource()== button7)
                x.setText("1");
            else if (event.getSource()== button8)
                x.setText("2");
            else if (event.getSource()== button9)
                x.setText("3");
            else if (event.getSource()== button0)
                x.setText("0");
            else if (event.getSource()== buttonR)
                x.setText("   ");   
            }
        }

    }
Was it helpful?

Solution

You can add a MouseListener to the textfields so you know when the user has switched from x to y and vice versa:

private JTextField selected;

selected = x;
    x.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent e) {
            selected = x;
        }
    });
    y.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent e) {
            selected = y;
        }
    });

To do the subtraction, convert the text present in x and y to ints and subtract. You can refactor your actionPerformed to:

    public void actionPerformed(ActionEvent event) {

        String s = selected.getText();
        if (event.getSource() == buttonR) {
            x.setText("");
            y.setText("");
            z.setText("");
        } else if (event.getSource() == buttonE) {
            int result = Integer.parseInt(x.getText()) - Integer.parseInt(y.getText());
            z.setText(String.valueOf(result));
        } else {
            String command = event.getActionCommand();
            selected.setText(s + command);
        }
    }

OTHER TIPS

This seems homework so I'm not gonna give you details, anyway

  1. instead that using setText you should first getText and then concatenate the already present number with the new digit
  2. you could use a variable at the beginning of your actionPerformed and switch between two textfields with a boolean value for example. Eg. JTextField cur = first ? x : y
  3. just read the documentation of Integer or String classes and you'll find what you need (toString, valueOf, parse)

By the way I answered you also yesterday and I suggested you not to use an if/else chain for every button since you know which is the number that is shown.. example:

((JButton)e.getSource()).getText() == "1"

this means that you are wasting a lot of lines of code..

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