Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3397382
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:31:19+00:00 2026-05-18T04:31:19+00:00

I am constructing a simple modified Subtraction-only calculator where z = x – y.

  • 0

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

    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T04:31:20+00:00Added an answer on May 18, 2026 at 4:31 am

    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);
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm constructing a simple form in ERB but the HTML produced by the text_field
I am constructing a large HTML document from fragments supplied by users that have
Given: Constructing an ADO Connection object from one thread and giving it to another
I am constructing a search page with a textbox and a button for now,
I'm constructing a method to take in an ArrayList(presumably full of objects) and then
I am having trouble constructing a single XPath statement to return two different sets
I am currently constructing a Carputer front end and one function that it needs
How would I go about constructing the contour of 2d polygon which is formed
What's the best way to handle a visitor constructing their own URL and replacing
I'd like to know what practical way of constructing reports for EPSON Dot Matrix

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.