0% found this document useful (0 votes)
15 views5 pages

Import Javax HAS

This Java code defines a Calculator class that creates a graphical user interface (GUI) for a basic calculator application. It initializes the frame and GUI components like buttons and text fields. It adds action listeners to the buttons to handle number entry, arithmetic operations, clear, and equals functions. The calculate method performs the calculations based on numbers and operators passed in. The main method creates an instance of the Calculator class to display and run the application.

Uploaded by

Suleman Qureshi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views5 pages

Import Javax HAS

This Java code defines a Calculator class that creates a graphical user interface (GUI) for a basic calculator application. It initializes the frame and GUI components like buttons and text fields. It adds action listeners to the buttons to handle number entry, arithmetic operations, clear, and equals functions. The calculate method performs the calculations based on numbers and operators passed in. The main method creates an instance of the Calculator class to display and run the application.

Uploaded by

Suleman Qureshi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

import javax.swing.

*;

import java.awt.*;

import java.awt.event.*;

import java.rmi.NotBoundException;

public class Calculator {

protected JFrame JF;

protected JTextField JTF;

protected JButton[] JNB;

protected JButton[] JOB;

protected JButton AB, SB, MB, DB, EB, CB;

protected JPanel JP;

protected double N1;

protected String op;

public Calculator () {

JF = new JFrame(" Calculator");

JF.setBackground(Color.BLACK);

JTF = new JTextField();

JNB = new JButton[10];

JOB = new JButton[5];

AB = new JButton("+");

SB = new JButton("-");

MB = new JButton("*");

DB = new JButton("/");

EB = new JButton("=");

CB = new JButton("C");
JP = new JPanel();

JF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JF.setSize(300, 450);

JF.setLayout(null);

JF.setVisible(true);

JTF.setBounds(30, 40, 240, 30);

JTF.setEditable(true);

AB.setBounds(50, 100, 50, 30);

SB.setBounds(110, 100, 50, 30);

MB.setBounds(170, 100, 50, 30);

DB.setBounds(230, 100, 50, 30);

EB.setBounds(50, 170, 100, 30);

CB.setBounds(170, 170, 100, 30);

JP.setBounds(50, 220, 230, 200);

JP.setLayout(new GridLayout(4, 3));

JP.setBackground(Color.BLUE);

for (int i = 0; i < 10; i++) {

JNB[i] = new JButton(String.valueOf(i));

JP.add(JNB[i]);

JF.add(JTF);

JF.add(AB);

JF.add(SB);
JF.add(MB);

JF.add(DB);

JF.add(EB);

JF.add(CB);

JF.add(JP);

for (int i = 0; i < 10; i++) {

int number = i;

JNB[i].addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

JTF.setText(JTF.getText() + number);

});

AB.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

N1 = Double.parseDouble(JTF.getText());

op = "+";

JTF.setText("");

});

SB.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

N1 = Double.parseDouble(JTF.getText());

op = "-";

JTF.setText("");

}
});

MB.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

N1 = Double.parseDouble(JTF.getText());

op = "*";

JTF.setText("");

});

DB.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

N1 = Double.parseDouble(JTF.getText());

op = "/";

JTF.setText("");

});

EB.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

double secondNumber = Double.parseDouble(JTF.getText());

double result = calculate(N1, secondNumber, op);

JTF.setText(String.valueOf(result));

});

CB.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

JTF.setText("");
}

});

private double calculate(double num1, double num2, String operator) {

switch (operator) {

case "+":

return num1 + num2;

case "-":

return num1 - num2;

case "*":

return num1 * num2;

case "/":

return num1 / num2;

default:

return 0;

public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable() {

public void run() {

new Calculator ();

});

You might also like