Ask Your Question
0

How can a calculator be created using JFrame, JTextField, or Swing?

asked 2023-01-03 11:00:00 +0000

david gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
3

answered 2021-05-16 11:00:00 +0000

lalupa gravatar image

To create a calculator using JFrame, JTextField, and Swing, follow these steps:

  1. Create a new Java project in your preferred IDE.

  2. Add a new JFrame to the project. The JFrame will be the main window of the calculator.

  3. Inside the JFrame, add a JTextField where the user inputs numbers and operations.

  4. Add buttons for each digit from 0 to 9, as well as buttons for mathematical operations such as +, -, *, /, and =.

  5. Attach action listeners to each button to handle user input.

  6. Create methods to handle mathematical operations, such as addition, subtraction, multiplication, and division.

  7. Use these methods to perform calculations based on the user’s input.

  8. Update the JTextField to display the result of the calculation.

  9. Test the calculator to ensure it works as expected.

Here is some sample code to get you started:

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

public class Calculator extends JFrame implements ActionListener {

    JTextField tf;
    int num1, num2, result;
    String operator;

    public Calculator() {
        setTitle("Calculator");
        setSize(300, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        tf = new JTextField();
        tf.setEditable(false);
        add(tf, BorderLayout.NORTH);

        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(4, 4));
        add(panel, BorderLayout.CENTER);

        JButton btn0 = new JButton("0");
        JButton btn1 = new JButton("1");
        JButton btn2 = new JButton("2");
        JButton btn3 = new JButton("3");
        JButton btn4 = new JButton("4");
        JButton btn5 = new JButton("5");
        JButton btn6 = new JButton("6");
        JButton btn7 = new JButton("7");
        JButton btn8 = new JButton("8");
        JButton btn9 = new JButton("9");
        JButton btnAdd = new JButton("+");
        JButton btnSub = new JButton("-");
        JButton btnMul = new JButton("*");
        JButton btnDiv = new JButton("/");
        JButton btnEquals = new JButton("=");

        panel.add(btn1);
        panel.add(btn2);
        panel.add(btn3);
        panel.add(btnAdd);
        panel.add(btn4);
        panel.add(btn5);
        panel.add(btn6);
        panel.add(btnSub);
        panel.add(btn7);
        panel.add(btn8);
        panel.add(btn9);
        panel.add(btnMul);
        panel.add(btn0);
        panel.add(btnEquals);
        panel.add(btnDiv);

        btn1.addActionListener(this);
        btn2.addActionListener(this);
        btn3.addActionListener(this);
        btn4.addActionListener(this);
        btn5.addActionListener(this);
        btn6.addActionListener(this);
        btn7.addActionListener(this);
        btn8.addActionListener(this);
        btn9.addActionListener(this);
        btn0.addActionListener(this);
        btnAdd.addActionListener(this);
        btnSub.addActionListener(this);
        btnMul.addActionListener(this);
        btnDiv.addActionListener(this);
        btnEquals.addActionListener(this);

        setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("1")) {
            tf.setText(tf.getText() + "1");
        } else if (e.getActionCommand().equals("2")) {
            tf.setText(tf.getText() + "2");
        } else if (e.getActionCommand().equals("3")) {
            tf.setText(tf.getText() + "3");
        } else if (e.getActionCommand().equals("4")) {
            tf.setText(tf.getText() + "4");
        } else if (e.getActionCommand().equals("5")) {
            tf.setText(tf.getText() + "5");
        } else if (e.getActionCommand().equals("6")) {
            tf.setText(tf.getText() + "6");
        } else if (e.getActionCommand().equals("7")) {
            tf.setText(tf.getText() + "7");
        } else if (e.getActionCommand().equals("8")) {
            tf.setText(tf.getText() + "8");
        } else if (e.getActionCommand().equals("9")) {
            tf.setText(tf.getText() + "9");
        } else if (e.getActionCommand().equals("0")) {
            tf.setText(tf.getText() + "0");
        } else if (e.getActionCommand().equals("+")) {
            num1 = Integer.parseInt(tf.getText());
            operator = "+";
            tf.setText("");
        } else if (e.getActionCommand().equals("-")) {
            num1 = Integer.parseInt(tf.getText());
            operator = "-";
            tf.setText("");
        } else if (e.getActionCommand().equals("*")) {
            num1 = Integer.parseInt(tf.getText());
            operator = "*";
            tf.setText("");
        } else if (e.getActionCommand().equals("/")) {
            num1 = Integer.parseInt(tf.getText());
            operator = "/";
            tf.setText("");
        } else if (e.getActionCommand().equals("=")) {
            num2 = Integer.parseInt(tf.getText());
            if (operator.equals("+")) {
                result = num1 + num2;
            } else if (operator.equals("-")) {
                result = num1 - num2;
            } else if (operator.equals("*")) {
                result = num1 * num2;
            } else if (operator.equals("/")) {
                result = num1 / num2;
            }
            tf.setText(String.valueOf(result));
        }
    }

    public static void main(String[] args) {
        new Calculator();
    }
}

In this code, we first create a JFrame and a JTextField. Then we add buttons for each digit and ... (more)

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-01-03 11:00:00 +0000

Seen: 12 times

Last updated: May 16 '21