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

Import Import Import Import Public Class Extends Private Double Private Double Private Byte

This document defines a Java class called Frame that creates a basic calculator GUI. It initializes number buttons, function buttons like add and multiply, and a text field to display results. Button click listeners perform the appropriate calculation based on the operation selected and display the output. The class stores the current operation and two number values to perform multi-step calculations.

Uploaded by

Avi Garg
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)
44 views5 pages

Import Import Import Import Public Class Extends Private Double Private Double Private Byte

This document defines a Java class called Frame that creates a basic calculator GUI. It initializes number buttons, function buttons like add and multiply, and a text field to display results. Button click listeners perform the appropriate calculation based on the operation selected and display the output. The class stores the current operation and two number values to perform multi-step calculations.

Uploaded by

Avi Garg
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

import
import
import

javax.swing.*;
java.awt.*;
java.awt.event.ActionEvent;
java.awt.event.ActionListener;

public class Frame extends JFrame {


private double tempNumbers1 = 0;
private double tempNumbers2 = 0;
private byte function = -1;
private JTextField resultJText;
public Frame() {
JButton[] numberButtons = new JButton[10];
for ( int i = 9; i >= 0; i--) {
numberButtons[i] = new JButton(Integer.toString(i));
}
JButton
JButton
JButton
JButton
JButton
JButton

enterButton
= new JButton("Enter");
cButton
= new JButton("C");
multiplyButton = new JButton("*");
divideButton = new JButton("/");
addButton
= new JButton("+");
substractButton = new JButton("-");

resultJText = new JTextField();


resultJText.setPreferredSize(new Dimension(160, 20));
resultJText.setBackground(Color.WHITE);
resultJText.setEnabled(false);
resultJText.setHorizontalAlignment(4);
resultJText.setDisabledTextColor(Color.BLACK);
JPanel motherPanel = new JPanel();
motherPanel.setLayout(new BoxLayout(motherPanel,
BoxLayout.Y_AXIS));
JPanel textPanel = new JPanel();
textPanel.setPreferredSize(new Dimension(160, 20));
textPanel.add(resultJText);

JPanel numberButtonsPanel = new JPanel();


numberButtonsPanel.setPreferredSize(new Dimension(160, 100));
for(int i = 9; i>=0; i--) {
numberButtonsPanel.add(numberButtons[i]);
}
JPanel functionButtonPanel = new JPanel();
functionButtonPanel.setPreferredSize(new Dimension(160, 35));
functionButtonPanel.add(enterButton);
functionButtonPanel.add(cButton);
functionButtonPanel.add(multiplyButton);
functionButtonPanel.add(divideButton);
functionButtonPanel.add(addButton);
functionButtonPanel.add(substractButton);
numberButtonsAction[] numberButtonActions = new
numberButtonsAction[10];
for ( int i = 0; i < 10; i++ ) {
numberButtonActions[i] = new
numberButtonsAction(numberButtons[i]);
numberButtons[i].addActionListener(numberButtonActions[i]);
}
EnterButton enter = new EnterButton();
enterButton.addActionListener(enter);
CButton c = new CButton();
cButton.addActionListener(c);
MultiplyButton multiply = new MultiplyButton();
multiplyButton.addActionListener(multiply);
DivideButton divide = new DivideButton();
divideButton.addActionListener(divide);
AddButton add = new AddButton();
addButton.addActionListener(add);
SubtractButton subtract = new SubtractButton();
substractButton.addActionListener(subtract);
motherPanel.add(textPanel);
motherPanel.add(numberButtonsPanel);
motherPanel.add(functionButtonPanel);

add(motherPanel);
setTitle("ButtonTest");
setSize(180, 290);
setLocationByPlatform(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
}
private class numberButtonsAction implements ActionListener {
private String c;
public numberButtonsAction(JButton a) {
this.c = a.getText();
}
public void actionPerformed(ActionEvent e) {
if (!resultJText.getText().equals("0.0")) {
resultJText.setText(resultJText.getText() + c);
} else {
resultJText.setText("");
actionPerformed(e);
}
}
}
private class EnterButton implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
tempNumbers2 = Double.parseDouble(resultJText.getText());
if (function == 0) {
resultJText.setText(Double.toString((Math.round((tempNumbers1 /
tempNumbers2) * 100)) / 100));
} else if (function == 1) {
resultJText.setText(Double.toString(tempNumbers1 *
tempNumbers2));
} else if (function == 2) {
resultJText.setText(Double.toString(tempNumbers2 +
tempNumbers1));
} else if (function == 3) {
resultJText.setText(Double.toString(tempNumbers1 tempNumbers2));

} else {
resultJText.setText(String.valueOf(tempNumbers1));
}
tempNumbers1 = Double.parseDouble(resultJText.getText());
}
}
private class CButton implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
resultJText.setText("");
tempNumbers1 = 0;
tempNumbers2 = 0;
function = -1;
}
}
private class DivideButton implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (tempNumbers1 == 0) {
tempNumbers1 = Double.parseDouble(resultJText.getText());
resultJText.setText("");
} else {
tempNumbers2 = Double.parseDouble(resultJText.getText());
resultJText.setText("");
}
function = 0;
}
}
private class MultiplyButton implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (tempNumbers1 == 0) {
tempNumbers1 = Double.parseDouble(resultJText.getText());
resultJText.setText("");

} else {
tempNumbers2 = Double.parseDouble(resultJText.getText());
resultJText.setText("");
}
function = 1;
}
}
private class AddButton implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (tempNumbers1 == 0) {
tempNumbers1 = Double.parseDouble(resultJText.getText());
resultJText.setText("");
} else {
tempNumbers2 = Double.parseDouble(resultJText.getText());
resultJText.setText("");
}
function = 2;
}
}
private class SubtractButton implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (tempNumbers1 == 0) {
tempNumbers1 = Double.parseDouble(resultJText.getText());
resultJText.setText("");
} else {
tempNumbers2 = Double.parseDouble(resultJText.getText());
resultJText.setText("");
}
function = 3;
}
}
}

You might also like