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

  • Home
  • SEARCH
  • 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 6614991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:24:25+00:00 2026-05-25T20:24:25+00:00

I have made a calculator using Java Swing, and it’s mostly done but I

  • 0

I have made a calculator using Java Swing, and it’s mostly done but I was wondering how you could make it work so that when you press a key from 0 – 9 on the keyboard, it will perform the same function as manually pressing the JButton on the calculator. I tried using but1.setMnemonic(KeyEvent.VK_1); but I it didn’t do anything when I pressed the 1 key. How can I make it so it will correctly activate said button when I press a keyboard key? Here is the code I am using, thanks for any help!

import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.Action; // For key presses
import java.awt.event.KeyEvent; // key presses
import java.awt.event.KeyListener;

public class ui_calculator implements ActionListener {

JFrame frame = new JFrame("Calculator"); // Creates the frame
JPanel panel = new JPanel(new FlowLayout()); // Add FlowLayout with center alignment

// Make new buttons, text areas
JTextArea text = new JTextArea(1, 20);
JButton but1 = new JButton("1");
JButton but2 = new JButton("2");
JButton but3 = new JButton("3");
JButton but4 = new JButton("4");
JButton but5 = new JButton("5");
JButton but6 = new JButton("6");
JButton but7 = new JButton("7");
JButton but8 = new JButton("8");
JButton but9 = new JButton("9");
JButton but0 = new JButton("0");

JButton butadd = new JButton("+");
JButton butsub = new JButton("-");
JButton butmul = new JButton("*");
JButton butdiv = new JButton("/");
JButton buteq = new JButton("=");
JButton butclear = new JButton("C");

Double number1, number2, result; // Numbers double as precise as a float
int addc = 0, subc = 0, mulc = 0, divc = 0;


public void ui() 
{

    frame.setVisible(true); // Make frame visible
    frame.setSize(250, 200); // Calculator Size
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // What happens when frame closes (exits program when user closes frame.

    frame.add(panel);

    panel.add(text);

    // Adding number buttons
    panel.add(but1);
    panel.add(but2);
    panel.add(but3);
    panel.add(but4);
    panel.add(but5);
    panel.add(but6);
    panel.add(but7);
    panel.add(but8);
    panel.add(but9);
    panel.add(but0);

    // Adding action buttons
    panel.add(butadd);
    panel.add(butsub);
    panel.add(butmul);
    panel.add(butdiv);
    panel.add(buteq);
    panel.add(butclear);

 but1.addActionListener(this);
 but2.addActionListener(this);
 but3.addActionListener(this);
 but4.addActionListener(this);
 but5.addActionListener(this);
 but6.addActionListener(this);
 but7.addActionListener(this);
 but8.addActionListener(this);
 but9.addActionListener(this);
 but0.addActionListener(this);
 butadd.addActionListener(this);
 butsub.addActionListener(this);
 butmul.addActionListener(this);
 butdiv.addActionListener(this);
 buteq.addActionListener(this);
 butclear.addActionListener(this);

 //but1.setMnemonic(KeyEvent.VK_1);

 // Colored buttons (red)
 butadd.setForeground(Color.red);
 butsub.setForeground(Color.red);
 butmul.setForeground(Color.red);
 butdiv.setForeground(Color.red);
 buteq.setForeground(Color.red);
 butclear.setForeground(Color.red);

 // Colored buttons (blue)
 but1.setForeground(Color.blue);
 but2.setForeground(Color.blue);
 but3.setForeground(Color.blue);
 but4.setForeground(Color.blue);
 but5.setForeground(Color.blue);
 but6.setForeground(Color.blue);
 but7.setForeground(Color.blue);
 but8.setForeground(Color.blue);
 but9.setForeground(Color.blue);
 but0.setForeground(Color.blue);

}


@Override
public void actionPerformed(ActionEvent e) { // Invokes when certain action occurs

    Object source = e.getSource(); // Object on which event occured

    if(source == butclear) // Reset all text
    {
        number1 = 0.0;
        number2 = 0.0;
        text.setText("");
    }

    // Specific buttons trigger text
    if(source == but1) 
    {
        text.append("1");
    }

/*  if(KeyEvent.VK_1 = true)
    {
        text.append("1");
    } */

    if(source == but2)
    {
        text.append("2");
    }

    if(source == but3)
    {
        text.append("3");
    }

    if(source == but4)
    {
        text.append("4");
    }

    if(source == but5)
    {
        text.append("5");
    }

    if(source == but6)
    {
        text.append("6");
    }

    if(source == but7)
    {
        text.append("7");
    }

    if(source == but8)
    {
        text.append("8");
    }

    if(source == but9)
    {
        text.append("9");
    }

    if(source == but0)
    {
        text.append("0");
    }

    if(source == butadd) // Addition actions
    {
        number1 = number_reader();
        text.setText("");
        addc = 1;
        subc = 0;
        mulc = 0;
        divc = 0;
    }

    if(source == butsub) // Subtraction actions
    {
        number1 = number_reader();
        text.setText("");
        addc = 0;
        subc = 1;
        mulc = 0;
        divc = 0;
    }

    if(source == butmul) // Multiplication actions
    {
        number1 = number_reader();
        text.setText("");
        addc = 0;
        subc = 0;
        mulc = 1;
        divc = 0;
    }

    if(source == butdiv) // Division actions
    {
        number1 = number_reader();
        text.setText("");
        addc = 0;
        subc = 0;
        mulc = 0;
        divc = 1;
    }

    if(source == buteq) // Equals actions
    {
        number2 = number_reader();
        if(addc > 0)
        {
            result = number1 + number2;
            text.setText(Double.toString(result));
        }

        if(subc > 0)
        {
            result = number1 - number2;
            text.setText(Double.toString(result));
        }

        if(mulc > 0)
        {
            result = number1 * number2;
            text.setText(Double.toString(result));
        }

        if(divc > 0)
        {
            result = number1 / number2;
            text.setText(Double.toString(result));
        }

    }


}

public double number_reader()
{
    Double num1;
    String s;
    s = text.getText();
    num1 = Double.valueOf(s);

    return num1;
}

}

  • 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-25T20:24:26+00:00Added an answer on May 25, 2026 at 8:24 pm

    The mnemonic will make the button press when used with a modifier (usually Alt or Ctrl), so pressing Alt+1 (or Ctrl+1) will work.

    To make the buttons correspond to the Keypad, you should have a look at InputMap and ActionMap.

    See http://download.oracle.com/javase/tutorial/uiswing/misc/keybinding.html for more details.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have made a control which has a property that is calculated by using
I have made a new windows service which works fine using barebone code (just
I have made an application (for my self) for feeds reading, using SyndicationFeed ,
I have made a multiplayer game using the GameKit Framework where 2 iPhones/iPods can
Hy, I was thinking that all my website will have use of cells, using
I have a website that calculates a users involvement/activity using multiple MySQL queries. For
I have a code, that was not made by me. In this complex code,
I have made a lookup table that allows you to blend two single-byte channels
I have made an app that paints FFT to the screen realtime (from mic).
In a nutshell on page1.php I have a calculator that consists of an HTML

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.