0% found this document useful (0 votes)
70 views9 pages

McCormickKyle COP2250 Week11Submission

The document contains code for three exercises to create GUI calculators in Java using Swing. The first exercise creates a basic calculator GUI with number buttons. The second creates a temperature converter. The third expands on this to create an arithmetic calculator that can perform addition, subtraction, multiplication and division.

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)
70 views9 pages

McCormickKyle COP2250 Week11Submission

The document contains code for three exercises to create GUI calculators in Java using Swing. The first exercise creates a basic calculator GUI with number buttons. The second creates a temperature converter. The third expands on this to create an arithmetic calculator that can perform addition, subtraction, multiplication and division.

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/ 9

Kyle McCormick | 03/20/2024 | COP2250 | Week 11 Submission

EXERCISE ONE:___________________________________________________
CODE:_________________________________
package com.mycompany.calculatorgui;
import javax.swing.*;
public class CalculatorGUI {
public static void main(String[] args) {
JFrame frame = new JFrame("Calculator");
frame.setSize(300, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel panel = new JPanel();


frame.add(panel);

JButton button1 = new JButton("1");


JButton button2 = new JButton("2");
JButton button3 = new JButton("3");
// setup jbutton structure for buttons

panel.add(button1);
panel.add(button2);
panel.add(button3);
// panel buttons

frame.setVisible(true);
}
}
OUTPUT:_______________________________

EXERCISE TWO:___________________________________________________
CODE:_________________________________
package com.mycompany.temperatureconvertergui;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TemperatureConverterGUI {
public static void main(String[] args) {
JFrame frame = new JFrame("Temperature Conversion for
Calculator Program");
frame.setSize(300, 150);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// set panel with jframe title
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3, 2));
frame.add(panel);
// identify jlabels for celsius and fahrenheit
JLabel celsiusLabel = new JLabel("Enter Celsius:");
JTextField celsiusField = new JTextField();
JLabel fahrenheitLabel = new JLabel("Fahrenheit:");
JTextField fahrenheitField = new JTextField();
JButton calculateButton = new JButton("Calculate");
JButton clearButton = new JButton("Clear");
// panels for celcius, F, clear, and field buttons
panel.add(celsiusLabel);
panel.add(celsiusField);
panel.add(fahrenheitLabel);
panel.add(fahrenheitField);
panel.add(calculateButton);
panel.add(clearButton);
// calculations for conversions
calculateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
double celsius =
Double.parseDouble(celsiusField.getText());
double fahrenheit = (celsius * 9/5) + 32;

fahrenheitField.setText(String.valueOf(fahrenheit));
}
});
// CHECK BUTTON, issues with listener so double check
clearButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
celsiusField.setText("");
fahrenheitField.setText("");
}
});
// visible
frame.setVisible(true);
}
}
OUTPUT:_______________________________

EXERCISE THREE:_________________________________________________
CODE:_________________________________
package com.mycompany.arithmeticcalculatorgui;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ArithmeticCalculatorGUI {
public static void main(String[] args) {
JFrame frame = new JFrame("Exercise 3 Arith. Calc");
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// same title setup as exercise 2
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(4, 2));
frame.add(panel);

JLabel num1Label = new JLabel("Enter first number:");


JTextField num1Field = new JTextField();
JLabel num2Label = new JLabel("Enter second number:");
JTextField num2Field = new JTextField();
JLabel resultLabel = new JLabel("Result:");
JTextField resultField = new JTextField();
resultField.setEditable(false);
// setup fields for labels for 1st, 2nd, and result ( +,
-, Div, *)
ButtonGroup operatorGroup = new ButtonGroup();
JRadioButton addRadio = new JRadioButton("Addition
(+)");
JRadioButton subtractRadio = new
JRadioButton("Subtraction (-)");
JRadioButton multiplyRadio = new
JRadioButton("Multiplication (*)");
JRadioButton divideRadio = new JRadioButton("Division
(/)");
// radio buttons setup
operatorGroup.add(addRadio);
operatorGroup.add(subtractRadio);
operatorGroup.add(multiplyRadio);
operatorGroup.add(divideRadio);
// operators
panel.add(num1Label);
panel.add(num1Field);
panel.add(num2Label);
panel.add(num2Field);
panel.add(addRadio);
panel.add(subtractRadio);
panel.add(multiplyRadio);
panel.add(divideRadio);
panel.add(resultLabel);
panel.add(resultField);
// lotsa panels, remember to adjust label and fields in
case of errors
JButton calculateButton = new JButton("Calculate
inputs!");
JButton clearButton = new JButton("Clear");
// calculate panels and buttons
panel.add(calculateButton);
panel.add(clearButton);
// same with listener as exercise 2, add parse and use
else if after result
calculateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
double num1 =
Double.parseDouble(num1Field.getText());
double num2 =
Double.parseDouble(num2Field.getText());
double result = 0;

if (addRadio.isSelected()) {
result = num1 + num2;
} else if (subtractRadio.isSelected()) {
result = num1 - num2;
} else if (multiplyRadio.isSelected()) {
result = num1 * num2;
} else if (divideRadio.isSelected()) {
if (num2 != 0) {
result = num1 / num2;
} else {
JOptionPane.showMessageDialog(frame,
"Cannot divide by zero.");
return;
}
}
// string value
resultField.setText(String.valueOf(result));
}
});
// second listener with clear
clearButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
num1Field.setText("");
num2Field.setText("");
resultField.setText("");
operatorGroup.clearSelection();
}
});

frame.setVisible(true);
}
}
OUTPUT:_______________________________

You might also like