0% found this document useful (0 votes)
81 views7 pages

McCormickKyle COP2250 Week12Project2Submission

The document describes a Java project to create a graphical user interface (GUI) calculator application. It includes code for a JFrame window layout with buttons and text fields. Methods handle button clicks and perform calculations. The code demonstrates using Swing components to build the GUI calculator.

Uploaded by

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

McCormickKyle COP2250 Week12Project2Submission

The document describes a Java project to create a graphical user interface (GUI) calculator application. It includes code for a JFrame window layout with buttons and text fields. Methods handle button clicks and perform calculations. The code demonstrates using Swing components to build the GUI calculator.

Uploaded by

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

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy