Java GUI Calculator I need the minimize, maximize and close standard icons to di
ID: 3918888 • Letter: J
Question
Java GUI Calculator
I need the minimize, maximize and close standard icons to display as circles (red, yellow, green) in the upper left hand corner of the calculator. The word calculator should be centered and the same color as the rest of the background calculator. The java icon should not display either.
Here is link to what the final product should look like.
https://elearn.sinclair.edu/content/enforced/110920-261581/content/Assignment12_homework_video.swf
Here is the code so far.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
// the CalcGUI class defines a GUI for a calculator *
public class CalcGUI extends JFrame {
// create sentinal values that are triggered when an operation is being performed
boolean addS = false;
boolean subtractS = false;
boolean divideS = false;
boolean multiplyS = false;
boolean decimalS = false;
boolean newNumberEntered = false; // true if the last event was a digit being entered
// Create a new MemoryCalc Object
MemoryCalc calc = new MemoryCalc();
// Create an = button to the bottom of the GUI
private JButton equals = new JButton("=");
// Create a text field for the top of the GUI
private JTextField topText = new JTextField("0.0");
// create an array of buttons for the center panel
private JButton[] buttons = new JButton[12];
// create an array of text for the center button panel
private String[] buttonNames = { "1", "2", "3",
"4", "5", "6",
"7", "8", "9",
"0", ".", "C",
};
// create buttons for the operators and add them to the east panel
private JButton plus = new JButton("+");
private JButton minus = new JButton("-");
private JButton multiply = new JButton("*");
private JButton divide = new JButton("/");
private String topNumber = ""; // The string that is displayed on the calculator
public CalcGUI() {
// Set layout with 10 px horizontal gap and 10 px vertical gap
setLayout(new BorderLayout(10, 10));
// define White (for topText)
Color white = new Color(255, 255, 255);
// set the top text field so that it cannot be changed by the user
topText.setEditable(false);
// Change topText color to white
topText.setBackground(white);
// change the top text field font to a bold 20pt mono font
Font topTextFont = new Font("monospace", Font.BOLD, 20);
topText.setFont(topTextFont);
// add the top text field to the top of the GUI
add(topText, BorderLayout.NORTH);
// add equals to the bottom of the calculator
add(equals, BorderLayout.SOUTH);
// register equals listener
equals.addActionListener(new EqualsListener());
// Create a 3X3 panel for the center of the GUI
// with 10px horizontal gap and 5px vertical gap
JPanel pCenter = new JPanel();
pCenter.setLayout(new GridLayout(4, 3, 10, 10));
// create the numerical buttons and add them to the center panel
// and add action listeners
for (int i = 0; i < 10; i++)
{
buttons[i] = new JButton(buttonNames[i]);
buttons[i].setEnabled(false);
pCenter.add(buttons[i]);
buttons[i].addActionListener(new DigitListener());
}
// Create . and C buttons and add them to the center panel
// and add action listeners
buttons[10] = new JButton(buttonNames[10]);
pCenter.add(buttons[10]);
buttons[10].addActionListener(new DecimalListener());
buttons[11] = new JButton(buttonNames[11]);
pCenter.add(buttons[11]);
buttons[11].addActionListener(new CancelListener());
// Add the center panel to the frame
add(pCenter, BorderLayout.CENTER);
JPanel pEast = new JPanel();
pEast.setLayout(new GridLayout(4, 1, 10, 10));
pEast.add(plus);
pEast.add(minus);
pEast.add(multiply);
pEast.add(divide);
// register operator listeners
plus.addActionListener(new OperatorListener());
minus.addActionListener(new OperatorListener());
multiply.addActionListener(new OperatorListener());
divide.addActionListener(new OperatorListener());
// Add the east panel to the frame
add(pEast, BorderLayout.EAST);
}
// Add the event listener for digits 0 - 9
class DigitListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
// add the number the user pushed to topText
topNumber += ((AbstractButton) e.getSource()).getText();
topText.setText(topNumber);
newNumberEntered = true;
}
}
// Event listener for operators - +, -, *, /
class OperatorListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
// Set the currentValue to whatever is in the topText unless there
// is a pending operation
if (!addS && !subtractS && !divideS && !multiplyS) {
calc.setCurrentValue(Double.parseDouble(topText.getText()));
topNumber = "";
decimalS = false;
newNumberEntered = false;
} else {
if (newNumberEntered == true)
{
// do ever calculation the user wanted
equals.doClick();
newNumberEntered = false;
}
}
for(int i = 0;i<10;i++){
buttons[i].setEnabled(true);
}
if (e.getSource() == plus)
{
addS = true;
subtractS = false; divideS = false; multiplyS = false;
}
else if (e.getSource() == minus)
{
subtractS = true;
addS = false; divideS = false; multiplyS = false;
}
else if (e.getSource() == divide)
{
divideS = true;
addS = false; subtractS = false; multiplyS = false;
}
else if (e.getSource() == multiply)
{
multiplyS = true;
addS = false; subtractS = false; divideS = false;
}
}
}
// Event listener for =
class EqualsListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
double answer; // holds the result of the calculation
calc.setOp2Value(Double.parseDouble(topText.getText()));
if (addS)
calc.add(calc.getOp2Value());
else if (subtractS)
calc.subtract(calc.getOp2Value());
else if (multiplyS)
calc.multiply(calc.getOp2Value());
else if (divideS)
calc.divide(calc.getOp2Value());
// update topText
topText.setText(String.valueOf(calc.getCurrentValue()));
// reset sentinal values
decimalS = false;
addS = false;
subtractS = false;
multiplyS = false;
divideS = false;
for(int i = 0;i<10;i++){
buttons[i].setEnabled(false);
}
// reset the topNumber
topNumber = "";
}
}
// Event listener for .
class DecimalListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
// don't let the user enter more than one decimal
if (!decimalS) {
topNumber += ".";
topText.setText(topNumber);
decimalS = true;
}
}
}
// Event listener for C
class CancelListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
// set all operator sentinal values to false
addS = false;
subtractS = false;
multiplyS = false;
divideS = false;
decimalS = false;
// reset number
calc.setCurrentValue(0);
topNumber = "";
topText.setText("0.0");
}
}
/* main method to drive the calculator */
public static void main(String[] args) {
// Create a new frame
JFrame CalcGUI = new CalcGUI();
// set Title to Calculator
CalcGUI.setTitle("Calculator");
// set size of Calculator and prevent it from being resized
CalcGUI.setSize(400, 250);
CalcGUI.setResizable(false);
// Center frame
CalcGUI.setLocationRelativeTo(null);
// when the window is closed the program ends
CalcGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// display the frame
CalcGUI.setVisible(true);
}
}
Explanation / Answer
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class JavaCalculator extends JFrame {
private JButton jbtNum1;
private JButton jbtNum2;
private JButton jbtNum3;
private JButton jbtNum4;
private JButton jbtNum5;
private JButton jbtNum6;
private JButton jbtNum7;
private JButton jbtNum8;
private JButton jbtNum9;
private JButton jbtNum0;
private JButton jbtEqual;
private JButton jbtAdd;
private JButton jbtSubtract;
private JButton jbtMultiply;
private JButton jbtDivide;
private JButton jbtSolve;
private JButton jbtClear;
private double TEMP;
private double SolveTEMP;
private JTextField jtfResult;
String display = "";
public JavaCalculator() {
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(4, 3));
p1.add(jbtNum1 = new JButton("1"));
p1.add(jbtNum2 = new JButton("2"));
p1.add(jbtNum3 = new JButton("3"));
p1.add(jbtNum4 = new JButton("4"));
p1.add(jbtNum5 = new JButton("5"));
p1.add(jbtNum6 = new JButton("6"));
p1.add(jbtNum7 = new JButton("7"));
p1.add(jbtNum8 = new JButton("8"));
p1.add(jbtNum9 = new JButton("9"));
p1.add(jbtNum0 = new JButton("0"));
p1.add(jbtClear = new JButton("C"));
JPanel p2 = new JPanel();
p2.setLayout(new FlowLayout());
p2.add(jtfResult = new JTextField(20));
jtfResult.setHorizontalAlignment(JTextField.RIGHT);
jtfResult.setEditable(false);
JPanel p3 = new JPanel();
p3.setLayout(new GridLayout(5, 1));
p3.add(jbtAdd = new JButton("+"));
p3.add(jbtSubtract = new JButton("-"));
p3.add(jbtMultiply = new JButton("*"));
p3.add(jbtDivide = new JButton("/"));
p3.add(jbtSolve = new JButton("="));
JPanel p = new JPanel();
p.setLayout(new GridLayout());
p.add(p2, BorderLayout.NORTH);
p.add(p1, BorderLayout.SOUTH);
p.add(p3, BorderLayout.EAST);
add(p);
jbtNum1.addActionListener(new ListenToOne());
jbtNum2.addActionListener(new ListenToTwo());
jbtNum3.addActionListener(new ListenToThree());
jbtNum4.addActionListener(new ListenToFour());
jbtNum5.addActionListener(new ListenToFive());
jbtNum6.addActionListener(new ListenToSix());
jbtNum7.addActionListener(new ListenToSeven());
jbtNum8.addActionListener(new ListenToEight());
jbtNum9.addActionListener(new ListenToNine());
jbtNum0.addActionListener(new ListenToZero());
jbtAdd.addActionListener(new ListenToAdd());
jbtSubtract.addActionListener(new ListenToSubtract());
jbtMultiply.addActionListener(new ListenToMultiply());
jbtDivide.addActionListener(new ListenToDivide());
jbtSolve.addActionListener(new ListenToSolve());
} //JavaCaluclator()
class ListenToOne implements ActionListener {
public void actionPerformed(ActionEvent e) {
display = jtfResult.getText();
jtfResult.setText(display + "1");
}
}
class ListenToTwo implements ActionListener {
public void actionPerformed(ActionEvent e) {
display = jtfResult.getText();
jtfResult.setText(display + "2");
}
}
class ListenToThree implements ActionListener {
public void actionPerformed(ActionEvent e) {
display = jtfResult.getText();
jtfResult.setText(display + "3");
}
}
class ListenToFour implements ActionListener {
public void actionPerformed(ActionEvent e) {
display = jtfResult.getText();
jtfResult.setText(display + "4");
}
}
class ListenToFive implements ActionListener {
public void actionPerformed(ActionEvent e) {
display = jtfResult.getText();
jtfResult.setText(display + "5");
}
}
class ListenToSix implements ActionListener {
public void actionPerformed(ActionEvent e) {
display = jtfResult.getText();
jtfResult.setText(display + "6");
}
}
class ListenToSeven implements ActionListener {
public void actionPerformed(ActionEvent e) {
display = jtfResult.getText();
jtfResult.setText(display + "7");
}
}
class ListenToEight implements ActionListener {
public void actionPerformed(ActionEvent e) {
display = jtfResult.getText();
jtfResult.setText(display + "8");
}
}
class ListenToNine implements ActionListener {
public void actionPerformed(ActionEvent e) {
display = jtfResult.getText();
jtfResult.setText(display + "9");
}
}
class ListenToZero implements ActionListener {
public void actionPerformed(ActionEvent e) {
display = jtfResult.getText();
jtfResult.setText(display + "0");
}
}
class ListenToAdd implements ActionListener {
public void actionPerformed(ActionEvent e) {
TEMP = Double.parseDouble(jtfResult.getText());
jtfResult.setText("");
}
}
class ListenToSubtract implements ActionListener {
public void actionPerformed(ActionEvent e) {
display = jtfResult.getText();
jtfResult.setText(display + "0");
}
}
class ListenToMultiply implements ActionListener {
public void actionPerformed(ActionEvent e) {
display = jtfResult.getText();
jtfResult.setText(display + "0");
}
}
class ListenToDivide implements ActionListener {
public void actionPerformed(ActionEvent e) {
display = jtfResult.getText();
jtfResult.setText(display + "0");
}
}
class ListenToSolve implements ActionListener {
public void actionPerformed(ActionEvent e) {
SolveTEMP = jtfResult.getText();
jtfResult.setText(TEMP + Double.parseDouble(jtfResult);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
JavaCalculator calc = new JavaCalculator();
calc.pack();
calc.setLocationRelativeTo(null);
calc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
calc.setVisible(true);
}
} //JavaCalculator
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.