To create a calculator using JFrame, JTextField, and Swing, follow these steps:
Create a new Java project in your preferred IDE.
Add a new JFrame to the project. The JFrame will be the main window of the calculator.
Inside the JFrame, add a JTextField where the user inputs numbers and operations.
Add buttons for each digit from 0 to 9, as well as buttons for mathematical operations such as +, -, *, /, and =.
Attach action listeners to each button to handle user input.
Create methods to handle mathematical operations, such as addition, subtraction, multiplication, and division.
Use these methods to perform calculations based on the user’s input.
Update the JTextField to display the result of the calculation.
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)
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
Asked: 2023-01-03 11:00:00 +0000
Seen: 12 times
Last updated: May 16 '21
How can the rejection of the class text_plain from JavaMail API due to a VerifyError be confirmed?
What is the process for generating a dynamic subdomain/URL using vue.js?
How can the style of the loader be modified while the form submission is being processed?
I'm attempting to develop a Javascript-based comments section for my website.
What are some feasible methods to enable MIDI file playback on a web browser?
How can I resolve the issue of being unable to use Fetch to POST an array of Selected Checkboxes?
What is the method to hide the scroll button when reaching the bottom?
What is the process of using a custom nunjucks filter to filter collections in an eleventy template?