Question

any help please using Swing GUI, which simulates a calculator.

any help please

using Swing GUI, which simulates a calculator.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import javax.swing.*;

import java.awt.event.*;

class Calc implements ActionListener

{

    JFrame f;

    JTextField t;

    JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,bdiv,bmul,bsub,badd,bdec,beq,bdel,bclr;

    static double a=0,b=0,result=0;

    static int operator=0;

    Calc()

    {

        f=new JFrame("Calculator");

        t=new JTextField();

        b1=new JButton("1");

        b2=new JButton("2");

        b3=new JButton("3");

        b4=new JButton("4");

        b5=new JButton("5");

        b6=new JButton("6");

        b7=new JButton("7");

        b8=new JButton("8");

        b9=new JButton("9");

        b0=new JButton("0");

        bdiv=new JButton("/");

        bmul=new JButton("*");

        bsub=new JButton("-");

        badd=new JButton("+");

        bdec=new JButton(".");

        beq=new JButton("=");

        bdel=new JButton("Delete");

        bclr=new JButton("Clear");

        

        t.setBounds(30,40,280,30);

        b7.setBounds(40,100,50,40);

        b8.setBounds(110,100,50,40);

        b9.setBounds(180,100,50,40);

        bdiv.setBounds(250,100,50,40);

        

        b4.setBounds(40,170,50,40);

        b5.setBounds(110,170,50,40);

        b6.setBounds(180,170,50,40);

        bmul.setBounds(250,170,50,40);

        

        b1.setBounds(40,240,50,40);

        b2.setBounds(110,240,50,40);

        b3.setBounds(180,240,50,40);

        bsub.setBounds(250,240,50,40);

        

        bdec.setBounds(40,310,50,40);

        b0.setBounds(110,310,50,40);

        beq.setBounds(180,310,50,40);

        badd.setBounds(250,310,50,40);

        

        bdel.setBounds(60,380,100,40);

        bclr.setBounds(180,380,100,40);

        

        f.add(t);

        f.add(b7);

        f.add(b8);

        f.add(b9);

        f.add(bdiv);

        f.add(b4);

        f.add(b5);

        f.add(b6);

        f.add(bmul);

        f.add(b1);

        f.add(b2);

        f.add(b3);

        f.add(bsub);

        f.add(bdec);

        f.add(b0);

        f.add(beq);

        f.add(badd);

        f.add(bdel);

        f.add(bclr);

        

        f.setLayout(null);

        f.setVisible(true);

        f.setSize(350,500);

        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        f.setResizable(false);

        

        b1.addActionListener(this);

        b2.addActionListener(this);

        b3.addActionListener(this);

        b4.addActionListener(this);

        b5.addActionListener(this);

        b6.addActionListener(this);

        b7.addActionListener(this);

        b8.addActionListener(this);

        b9.addActionListener(this);

        b0.addActionListener(this);

        badd.addActionListener(this);

        bdiv.addActionListener(this);

        bmul.addActionListener(this);

        bsub.addActionListener(this);

        bdec.addActionListener(this);

        beq.addActionListener(this);

        bdel.addActionListener(this);

        bclr.addActionListener(this);

    }

    public void actionPerformed(ActionEvent e)

    {

        if(e.getSource()==b1)

            t.setText(t.getText().concat("1"));

        

        if(e.getSource()==b2)

            t.setText(t.getText().concat("2"));

        

        if(e.getSource()==b3)

            t.setText(t.getText().concat("3"));

        

        if(e.getSource()==b4)

            t.setText(t.getText().concat("4"));

        

        if(e.getSource()==b5)

            t.setText(t.getText().concat("5"));

        

        if(e.getSource()==b6)

            t.setText(t.getText().concat("6"));

        

        if(e.getSource()==b7)

            t.setText(t.getText().concat("7"));

        

        if(e.getSource()==b8)

            t.setText(t.getText().concat("8"));

        

        if(e.getSource()==b9)

            t.setText(t.getText().concat("9"));

        

        if(e.getSource()==b0)

            t.setText(t.getText().concat("0"));

        

        if(e.getSource()==bdec)

            t.setText(t.getText().concat("."));

        

        if(e.getSource()==badd)

        {

            a=Double.parseDouble(t.getText());

            operator=1;

            t.setText("");

        }

        

        if(e.getSource()==bsub)

        {

            a=Double.parseDouble(t.getText());

            operator=2;

            t.setText("");

        }

        

        if(e.getSource()==bmul)

        {

            a=Double.parseDouble(t.getText());

            operator=3;

            t.setText("");

        }

        

        if(e.getSource()==bdiv)

        {

            a=Double.parseDouble(t.getText());

            operator=4;

            t.setText("");

        }

        

        if(e.getSource()==beq)

        {

            b=Double.parseDouble(t.getText());

        

            switch(operator)

            {

                case 1: result=a+b;

                    break;

        

                case 2: result=a-b;

                    break;

        

                case 3: result=a*b;

                    break;

        

                case 4: result=a/b;

                    break;

        

                default: result=0;

            }

        

            t.setText(""+result);

        }

        

        if(e.getSource()==bclr)

            t.setText("");

        

        if(e.getSource()==bdel)

        {

            String s=t.getText();

            t.setText("");

            for(int i=0;i<s.length()-1;i++)

            t.setText(t.getText()+s.charAt(i));

        }        

    }

    public static void main(String...s)

    {

        new Calc();

    }

}

Add a comment
Know the answer?
Add Answer to:
any help please using Swing GUI, which simulates a calculator.
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please, build calculator to exact image as below using Python. Write a GUI that implements the...

    Please, build calculator to exact image as below using Python. Write a GUI that implements the calculator shown in the following image: Calculator х Plus Equals: Add Clear Quit - User enters two integers into the text fields. - When Add button is pressed, the sum of the values in the text fields are shown after the Equals: as a label. - The Clear button clears the values in the text fields. The cleared values can be blank or zero....

  • please use c programme Write a program which simulates a calculator using the following specifications. (50...

    please use c programme Write a program which simulates a calculator using the following specifications. (50 points - 10pts for syntax, 10pts for commenting and 30pts for successful execution) • User input must have the following format - value1 operator value2 • Compute value1 operator value2 • Use switch statement • Operators must be ’+’ ,’-’, ’*’, ’/’ and ’%’ • Division by 0 in C leads to an error. Terminate the program before divison occurs in such cases. •...

  • Write a C++ Program that simulates a basic calculator using functions which performs the operations of...

    Write a C++ Program that simulates a basic calculator using functions which performs the operations of Addition, Subtraction, multiplication, and Division. ( make sure you cover the case to avoid division by a zero) Display a menu for list of operations that can be calculated and get the input from user about his choice of calculation. Based on user choice of operation, take the input of number/numbers from user. Assume all input values are of type double. Calculations must be...

  • Write a C++ Program that simulates a basic calculator using functions which performs the operations of...

    Write a C++ Program that simulates a basic calculator using functions which performs the operations of Addition, Subtraction, multiplication, and Division. ( make sure you cover the case to avoid division by a zero) Display a menu for the list of operations that can be calculated and get the input from the user about his choice of calculation. Based on user choice of operation, take the input of number/numbers from user. Assume all input values are of type double. Calculations...

  • Simple Java Notepad application using Swing GUI Create a help menu with shortcut ctrl+H: -Help me...

    Simple Java Notepad application using Swing GUI Create a help menu with shortcut ctrl+H: -Help menu includes two menu items: 1. About 2. Visit Homepage. Add a separator between these menu items. 1. Create a menu item which is About. Add a short cut for the menu item. It is ctrl+A. -When a user clicks it (an action event occurs), display a show message dialog box. Display the message shown in the figure. Display information icon. 2. Create a menu...

  • IN PYTHON! Create a calculator GUI. The GUI should let the user add, subtract, multiply, and...

    IN PYTHON! Create a calculator GUI. The GUI should let the user add, subtract, multiply, and divide two numbers they input. All user input and the result displayed to the user should be seen in the GUI itself (i.e. the terminal should not show anything). Thank you very much! PLEASE PASTE IN HERE THE RIGHT INDENTS

  • Use of swing or javaFX is optional in any of the questions... 3) Create the following...

    Use of swing or javaFX is optional in any of the questions... 3) Create the following GUI. You do not have to provide any functionality: Sign Up Log in Sign Up for Free First Nmae Last Nina." Email Address

  • I need help writing the code for a GUI calculator that uses 4 Radio Buttons (+....

    I need help writing the code for a GUI calculator that uses 4 Radio Buttons (+. -, * , /). Only one button can be selected at a time. I also have to group the buttons for this i am importing the ToggleGroup and using that. The user also need to select one function to begin with or an "Error" message will show if they select the calculate button. I would really like to know what needs to be used...

  • Java GUI Help! Hi, I have a Java GUI exercise that I need to complete and...

    Java GUI Help! Hi, I have a Java GUI exercise that I need to complete and was wondering if I could have a little bit of help... you can see it below. Create a simple graphical application that will enable a user to perform push, pop and peek operations on a stack and display the resulting stack (using toString) in a text area. Any help would be much appreciated! Thank you.

  • Please code the following Java App using Swing No additional classes are required. With that in...

    Please code the following Java App using Swing No additional classes are required. With that in mind ... Write a program that emulates a calculator. Create a Label with a title of "Super Calculator", a textbox, where the numbers are displayed, a series of buttons labeled as shown, and a “Clear” button. As the user enters the numbers, pressing the number buttons, display the numbers right justified to the textbox. When the user press any one of the operators, +,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT