0% found this document useful (0 votes)
18 views19 pages

Simple Calculator - Sabulao

Simple Calculator Program Sample

Uploaded by

juboysabulao
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)
18 views19 pages

Simple Calculator - Sabulao

Simple Calculator Program Sample

Uploaded by

juboysabulao
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/ 19

Simple Calculator

An output Submitted to
PT Joleco Agullo

Faculty of the Information Technology Department


College of Engineering
Eastern Visayas State University

In Partial Fulfilment
of the Requirement for the Course
IT 373A (Event Driven Programming)

Kent Edward Sabulao


BSIT – 3A

January 6, 2023
Screenshots:
(When you clicked a number example here is 5, and then you clicked the percentage sign, it will convert
the decimal format of the whole number)

(Equating decimal numbers, and to validate that the


dot is working)
Source Code:

package calculatorGUI;

import java.awt.EventQueue;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.border.EmptyBorder;

import javax.swing.JTextField;

import javax.swing.JButton;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import java.awt.Color;

import java.awt.Font;

import javax.swing.border.LineBorder;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

public class mainframe extends JFrame {

double currentOperand1;

double currentOperand2;

double currentResult;

String currentOperation;

private static final long serialVersionUID = 1L;

private JPanel contentPane;

private JTextField textField;


private JButton btnBackspace;

private JButton btnNewButton_2;

private JButton btnNewButton_3;

private JButton btnNewButton_4;

private JButton btnNewButton_5;

private JButton btnNewButton_6;

private JButton btnNewButton_7;

private JButton btnNewButton_8;

private JButton btnNewButton_9;

private JButton btnNewButton_10;

private JButton btnNewButton_11;

private JButton btnNewButton_12;

private JButton btnNewButton_13;

private JButton btnNewButton_14;

private JButton btnNewButton_15;

private JButton btnNewButton_16;

private JButton btnNewButton_17;

private JButton btnNewButton_18;

private JButton btnNewButton_19;

/**

* Launch the application.

*/

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

public void run() {

try {

mainframe frame = new mainframe();

frame.setVisible(true);
} catch (Exception e) {

e.printStackTrace();

});

private void addHoverEffect(final JButton button) {

button.addMouseListener(new MouseAdapter() {

@Override

public void mouseEntered(MouseEvent e) {

if (!button.getText().equals("=")) {

button.setBackground(Color.GRAY);

@Override

public void mouseExited(MouseEvent e) {

if (!button.getText().equals("=")) {

button.setBackground(new Color(100, 100, 100));

});

/**

* Create the frame.

*/
public mainframe() {

setResizable(false);//prevent frame from being resized

setTitle("Kalkyo ni papajubs");// Sets title of the frame

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//Exit of Application

setBounds(750, 150, 378, 410);

contentPane = new JPanel();

contentPane.setBackground(new Color(211, 211, 211));

contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

setContentPane(contentPane);

contentPane.setLayout(null);

textField = new JTextField();

textField.setBounds(30, 15, 305, 53);

textField.setBackground(Color.BLACK);

textField.setForeground(Color.GREEN);

contentPane.add(textField);

textField.setColumns(10);

JButton btnNewButton = new JButton("7");

btnNewButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

String number=textField.getText()+btnNewButton.getText();

textField.setText(number);

});

btnNewButton.setBounds(30, 146, 78, 50);

btnNewButton.setFocusPainted(false);

btnNewButton.setContentAreaFilled(false);

contentPane.add(btnNewButton);
btnBackspace = new JButton("Back");

btnBackspace.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

String backSpace=null;

if(textField.getText().length()>0){

StringBuilder str=new StringBuilder(textField.getText());

str.deleteCharAt(textField.getText().length()-1);

backSpace=str.toString();

textField.setText(backSpace);

});

//Anther sample of back button codes

//JButton btnbackSpace = new JButton("back")

//btnbackSpace.addActionListener(new ActionListener() {

//public void actionPerformed(ActionEvent e) {

//textField_Output.setText("");

btnBackspace.setBounds(30, 96, 78, 50);

btnBackspace.setFocusPainted(false);

btnBackspace.setContentAreaFilled(false);

contentPane.add(btnBackspace);

btnNewButton_2 = new JButton("4");

btnNewButton_2.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

String number = textField.getText() + btnNewButton_2.getText();


textField.setText(number);

});

btnNewButton_2.setBounds(30, 196, 78, 50);

btnNewButton_2.setFocusPainted(false);

btnNewButton_2.setContentAreaFilled(false);

contentPane.add(btnNewButton_2);

btnNewButton_3 = new JButton("1");

btnNewButton_3.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

String number = textField.getText() + btnNewButton_3.getText();

textField.setText(number);

});

btnNewButton_3.setBounds(30, 246, 78, 50);

btnNewButton_3.setFocusPainted(false);

btnNewButton_3.setContentAreaFilled(false);

contentPane.add(btnNewButton_3);

btnNewButton_4 = new JButton("0");

btnNewButton_4.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

String number = textField.getText() + btnNewButton_4.getText();

textField.setText(number);

});

btnNewButton_4.setBounds(30, 296, 78, 50);

btnNewButton_4.setFocusPainted(false);
btnNewButton_4.setContentAreaFilled(false);

contentPane.add(btnNewButton_4);

btnNewButton_5 = new JButton(".");

btnNewButton_5.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

String currentText = textField.getText();

if (currentText.isEmpty() || !currentText.endsWith(".")) {

textField.setText(currentText + ".");

});

btnNewButton_5.setBounds(108, 296, 78, 50);

btnNewButton_5.setFocusPainted(false);

btnNewButton_5.setContentAreaFilled(false);

contentPane.add(btnNewButton_5);

btnNewButton_6 = new JButton("=");

btnNewButton_6.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

try {

currentOperand2 =
Double.parseDouble(textField.getText().substring(textField.getText().indexOf(currentOperation) + 1));

if (currentOperation.equals("+")) {

currentResult = currentOperand1 + currentOperand2;

} else if (currentOperation.equals("-")) {

currentResult = currentOperand1 - currentOperand2;


} else if (currentOperation.equals("x")) {

currentResult = currentOperand1 * currentOperand2;

} else if (currentOperation.equals("/")) {

currentResult = currentOperand1 / currentOperand2;

} else if (currentOperation.equals("%")) {

double percentage = currentOperand2 / 100;

currentResult = currentOperand1 * percentage;

textField.setText(textField.getText() + " = " + currentResult);

} catch (NumberFormatException | StringIndexOutOfBoundsException ex) {

// Handle exceptions appropriately (e.g., display an error message)

ex.printStackTrace();

});

btnNewButton_6.setBounds(186, 296, 78, 50);

btnNewButton_6.setFocusPainted(false);

btnNewButton_6.setContentAreaFilled(false);

btnNewButton_6.setOpaque(true);

contentPane.add(btnNewButton_6);

btnNewButton_7 = new JButton("%");

btnNewButton_7.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

currentOperand1 = Double.parseDouble(textField.getText());

double percentage = currentOperand1 / 100;


textField.setText(String.valueOf(percentage));

currentOperation = "%";

});

btnNewButton_7.setBounds(264, 296, 69, 50);

btnNewButton_7.setFocusPainted(false);

btnNewButton_7.setContentAreaFilled(false);

contentPane.add(btnNewButton_7);

btnNewButton_8 = new JButton("C");

btnNewButton_8.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

textField.setText("");

String op1, op2;

op1 = String.valueOf(currentOperand1);

op2 = String.valueOf(currentOperand2);

op1="";

op2="";

});

btnNewButton_8.setBounds(108, 96, 78, 50);

btnNewButton_8.setFocusPainted(false);

btnNewButton_8.setContentAreaFilled(false);

contentPane.add(btnNewButton_8);

btnNewButton_9 = new JButton("8");


btnNewButton_9.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

String number = textField.getText() + btnNewButton_9.getText();

textField.setText(number);

});

btnNewButton_9.setBounds(108, 146, 78, 50);

btnNewButton_9.setFocusPainted(false);

btnNewButton_9.setContentAreaFilled(false);

contentPane.add(btnNewButton_9);

btnNewButton_10 = new JButton("5");

btnNewButton_10.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

String number = textField.getText() + btnNewButton_10.getText();

textField.setText(number);

});

btnNewButton_10.setBounds(108, 196, 78, 50);

btnNewButton_10.setFocusPainted(false);

btnNewButton_10.setContentAreaFilled(false);

contentPane.add(btnNewButton_10);

btnNewButton_11 = new JButton("2");

btnNewButton_11.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

String number = textField.getText() + btnNewButton_11.getText();

textField.setText(number);

}
});

btnNewButton_11.setBounds(108, 246, 78, 50);

btnNewButton_11.setFocusPainted(false);

btnNewButton_11.setContentAreaFilled(false);

contentPane.add(btnNewButton_11);

btnNewButton_12 = new JButton("00");

btnNewButton_12.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

String number = textField.getText() + btnNewButton_12.getText();

textField.setText(number);

});

btnNewButton_12.setBounds(186, 96, 78, 50);

btnNewButton_12.setFocusPainted(false);

btnNewButton_12.setContentAreaFilled(false);

contentPane.add(btnNewButton_12);

btnNewButton_13 = new JButton("9");

btnNewButton_13.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

String number = textField.getText() + btnNewButton_13.getText();

textField.setText(number);

});

btnNewButton_13.setBounds(186, 146, 78, 50);

btnNewButton_13.setFocusPainted(false);

btnNewButton_13.setContentAreaFilled(false);

contentPane.add(btnNewButton_13);
btnNewButton_14 = new JButton("6");

btnNewButton_14.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

String number = textField.getText() + btnNewButton_14.getText();

textField.setText(number);

});

btnNewButton_14.setBounds(186, 196, 78, 50);

btnNewButton_14.setFocusPainted(false);

btnNewButton_14.setContentAreaFilled(false);

contentPane.add(btnNewButton_14);

btnNewButton_15 = new JButton("3");

btnNewButton_15.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

String number = textField.getText() + btnNewButton_15.getText();

textField.setText(number);

});

btnNewButton_15.setBounds(186, 246, 78, 50);

btnNewButton_15.setFocusPainted(false);

btnNewButton_15.setContentAreaFilled(false);

contentPane.add(btnNewButton_15);

btnNewButton_16 = new JButton("+");

btnNewButton_16.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

currentOperand1 = Double.parseDouble(textField.getText());
textField.setText(textField.getText() + "+");

currentOperation = "+";

});

btnNewButton_16.setBounds(264, 96, 69, 50);

btnNewButton_16.setFocusPainted(false);

btnNewButton_16.setContentAreaFilled(false);

contentPane.add(btnNewButton_16);

btnNewButton_17 = new JButton("<html><b>-</b></html>");

btnNewButton_17.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

currentOperand1 = Double.parseDouble(textField.getText());

textField.setText(textField.getText() + "-");

currentOperation = "-";

});

btnNewButton_17.setBounds(264, 146, 69, 50);

btnNewButton_17.setFocusPainted(false);

btnNewButton_17.setContentAreaFilled(false);

contentPane.add(btnNewButton_17);

btnNewButton_18 = new JButton("x");

btnNewButton_18.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

currentOperand1 = Double.parseDouble(textField.getText());

textField.setText(textField.getText() + "x");

currentOperation = "x";

}
});

btnNewButton_18.setBounds(264, 196, 69, 50);

btnNewButton_18.setFocusPainted(false);

btnNewButton_18.setContentAreaFilled(false);

contentPane.add(btnNewButton_18);

btnNewButton_19 = new JButton("/");

btnNewButton_19.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

currentOperand1 = Double.parseDouble(textField.getText());

textField.setText(textField.getText() + "/");

currentOperation = "/";

});

btnNewButton_19.setBounds(264, 246, 69, 50);

btnNewButton_19.setFocusPainted(false);

btnNewButton_19.setContentAreaFilled(false);

contentPane.add(btnNewButton_19);

JButton[] buttons = { btnNewButton, btnBackspace, btnNewButton_2, btnNewButton_3,


btnNewButton_4, btnNewButton_5,

btnNewButton_6, btnNewButton_7, btnNewButton_8, btnNewButton_9, btnNewButton_10,


btnNewButton_11,

btnNewButton_12, btnNewButton_13, btnNewButton_14, btnNewButton_15,


btnNewButton_16, btnNewButton_17,

btnNewButton_18, btnNewButton_19 };

Color darkerGray = new Color(100, 100, 100);

for (JButton button : buttons) {


button.setBackground(darkerGray);

button.setOpaque(true);

button.setForeground(Color.WHITE);

btnBackspace.setForeground(new Color(255, 165, 0));

btnNewButton_7.setForeground(new Color(255, 165, 0));

btnNewButton_8.setForeground(new Color(255, 165, 0));

btnNewButton_16.setForeground(new Color(255, 165, 0));

btnNewButton_17.setForeground(new Color(255, 165, 0));

btnNewButton_18.setForeground(new Color(255, 165, 0));

btnNewButton_19.setForeground(new Color(255, 165, 0));

button.setBorder(new LineBorder(Color.WHITE));

button.setFont(new Font("Arial", Font.PLAIN, 12));

addHoverEffect(button);

contentPane.add(button);

btnNewButton_6.setBackground(new Color(255, 127, 0));

You might also like