Kyle McCormick |04/08/2024 | COP2250 | Project 2: Calculator with GUI
CODE:_____________________________________________________________________
package com.mycompany.calculatorgui;
/**
*
* @author kylemccormick start date 04/08
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
// CalculatorGUI, Jframe and listener
public class CalculatorGUI extends JFrame implements ActionListener {
private JTextField textField;
private double num1, num2, result;
private char operator;
private boolean decimalEntered = false;
// window parameters
public CalculatorGUI() {
setTitle("McCormick, K. JFrame Calculator");
// size for window
setPreferredSize(new Dimension(265, 350));
setDefaultCloseOperation(EXIT_ON_CLOSE);
// baglayout for control of buttons
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(3, 3, 3, 3);
textField = new JTextField();
textField.setColumns(20);
textField.setPreferredSize(new Dimension(20, 45));
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 4;
add(textField, gbc);
// establish button labels
String[] buttonLabels = {"1", "2", "3", "4", "5", "6", "7", "8",
"9", "0", "+", "-", "*", "/", "+/-", "."};
gbc.gridwidth = 1;
gbc.gridy = 1;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.ipadx = 5;
gbc.ipady = 5;
for (String label : buttonLabels) {
JButton button = new JButton(label);
button.addActionListener(this);
add(button, gbc);
gbc.gridx++;
if (gbc.gridx == 4) {
gbc.gridx = 0;
gbc.gridy++;
}
}
// clear and equals buttons
gbc.gridx = 0;
gbc.gridy = 6;
gbc.gridwidth = 2;
JButton clearButton = new JButton("C");
clearButton.addActionListener(this);
clearButton.setPreferredSize(new Dimension(100, 5));
add(clearButton, gbc);
gbc.gridx = 2;
gbc.gridwidth = 2;
JButton equalsButton = new JButton("=");
equalsButton.addActionListener(this);
equalsButton.setPreferredSize(new Dimension(100, 15));
add(equalsButton, gbc);
// pack jframe
pack();
}
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
switch (command) {
case "+/-":
if (!textField.getText().isEmpty()) {
double number =
Double.parseDouble(textField.getText());
number = -number; // inversion for numbers
textField.setText(String.valueOf(number));
}
break;
case "+":
case "-":
case "*":
case "/":
// set parameter to prevent double decimal input for
errors
num1 = Double.parseDouble(textField.getText());
operator = command.charAt(0);
textField.setText("");
decimalEntered = false;
break;
case "C":
textField.setText("");
decimalEntered = false;
break;
case "=":
num2 = Double.parseDouble(textField.getText());
calculate();
textField.setText(String.valueOf(result));
decimalEntered = false;
break;
case ".":
if (!decimalEntered) {
textField.setText(textField.getText() + ".");
decimalEntered = true;
}
break;
default:
textField.setText(textField.getText() + command);
}
}
private void calculate() {
switch (operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
if (num2 != 0) {
result = num1 / num2;
} else {
result = Double.NaN; // div by 0 protection
}
break;
}
}
public static void main(String[] args) {
CalculatorGUI calculator = new CalculatorGUI();
calculator.setVisible(true);
}
}
OUTPUT:___________________________________________________________________
OUTPUT CODE:______________________________________________________________
--------------------< com.mycompany:CalculatorGUI >---------------------
Building CalculatorGUI 1.0-SNAPSHOT
--------------------------------[ jar ]---------------------------------
--- exec-maven-plugin:3.0.0:exec (default-cli) @ CalculatorGUI ---
------------------------------------------------------------------------
BUILD SUCCESS
------------------------------------------------------------------------
Total time: 48.668 s
Finished at: 2024-04-14T14:56:43-04:00
------------------------------------------------------------------------
VIDEO DEMO:_______________________________________________________________
McCormickKyle_CO
P2250_Project2Demo