Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi, I have a Java project with the following specifications: Java GUI with events of a Simple Calculator as follows: The simple calculator should be

Hi,

I have a Java project with the following specifications:

Java GUI with events of a Simple Calculator as follows:

  1. The simple calculator should be able to display the computed result based on the operation.
  2. The Reset button should clear all values entered and displayed.
  3. When the Quit button is clicked, a JOptionPane window will display an information as shown before the program terminates.
  4. The program codes should be hard-coded.

This is what I came up with and it doesn't seem to work properly.

package mp004; import java.awt.EventQueue; import javax.swing.*; import java.awt.Color; import java.awt.Font; public class SimpleCalculator1 { private JFrame frame; private JTextField resultField; public String stemp1 = "", stemp2 = ""; public boolean ss = true, dec = false; public char operation; private JTextField num1; private JTextField num2; private JTextField OpField; public static void main(String[] args) { EventQueue.invokeLater(() -> { try { String piku = ".3"; System.out.println(Double.parseDouble(piku) + 1.0); SimpleCalculator1 window = new SimpleCalculator1(); window.frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } }); } /** * Create the application. */ public SimpleCalculator1() { initialize(); } /** * Initialize the contents of the frame. */ private void initialize() { frame = new JFrame(); frame.getContentPane().setBackground(Color.YELLOW); frame.setBounds(100, 100, 651, 416); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(null); JButton button1 = new JButton("1"); button1.addActionListener(e -> { if (ss) { stemp1 += "1"; resultField.setText(stemp1); } else { stemp2 += "1"; resultField.setText(stemp2); } }); button1.setBounds(45, 140, 115, 25); frame.getContentPane().add(button1); JButton button2 = new JButton("2"); button2.addActionListener(e -> { if (ss) { stemp1 += "2"; resultField.setText(stemp1); } else { stemp2 += "2"; resultField.setText(stemp2); } }); button2.setBounds(185, 140, 115, 25); frame.getContentPane().add(button2); JButton button3 = new JButton("3"); button3.addActionListener(e -> { if (ss) { stemp1 += "3"; resultField.setText(stemp1); } else { stemp2 += "3"; resultField.setText(stemp2); } }); button3.setBounds(320, 140, 115, 25); frame.getContentPane().add(button3); JButton button4 = new JButton("4"); button4.addActionListener(e -> { if (ss) { stemp1 += "4"; resultField.setText(stemp1); } else { stemp2 += "4"; resultField.setText(stemp2); } }); button4.setBounds(45, 180, 117, 25); frame.getContentPane().add(button4); JButton button5 = new JButton("5"); button5.addActionListener(e -> { if (ss) { stemp1 += "5"; resultField.setText(stemp1); } else { stemp2 += "5"; resultField.setText(stemp2); } }); button5.setBounds(185, 180, 115, 25); frame.getContentPane().add(button5); JButton button6 = new JButton("6"); button6.addActionListener(e -> { if (ss) { stemp1 += "6"; resultField.setText(stemp1); } else { stemp2 += "6"; resultField.setText(stemp2); } }); button6.setBounds(320, 180, 115, 25); frame.getContentPane().add(button6); JButton button7 = new JButton("7"); button7.addActionListener(e -> { if (ss) { stemp1 += "7"; resultField.setText(stemp1); } else { stemp2 += "7"; resultField.setText(stemp2); } }); button7.setBounds(45, 217, 117, 25); frame.getContentPane().add(button7); JButton button8 = new JButton("8"); button8.addActionListener(e -> { if (ss) { stemp1 += "8"; resultField.setText(stemp1); } else { stemp2 += "8"; resultField.setText(stemp2); } }); button8.setBounds(185, 217, 117, 25); frame.getContentPane().add(button8); JButton button9 = new JButton("9"); button9.addActionListener(e -> { if (ss) { stemp1 += "9"; resultField.setText(stemp1); } else { stemp2 += "9"; resultField.setText(stemp2); } }); button9.setBounds(320, 217, 117, 25); frame.getContentPane().add(button9); JButton button0 = new JButton("0"); button0.addActionListener(e -> { if (ss) { stemp1 += "0"; resultField.setText(stemp1); } else { stemp2 += "0"; resultField.setText(stemp2); } }); button0.setBounds(183, 254, 117, 25); frame.getContentPane().add(button0); JButton buttonDecimal = new JButton("."); buttonDecimal.addActionListener(e -> { if (ss && !dec) { stemp1 += "."; resultField.setText(stemp1); dec = true; } else if (!dec) { stemp2 += "."; resultField.setText(stemp2); dec = true; } }); buttonDecimal.setBounds(320, 254, 117, 25); frame.getContentPane().add(buttonDecimal); JButton buttonAdd = new JButton("+"); buttonAdd.addActionListener(e -> { if (ss && !stemp1.equals("")) { ss = false; resultField.setText(""); dec = false; } if (!ss) operation = '+'; }); buttonAdd.setBounds(45, 304, 117, 25); frame.getContentPane().add(buttonAdd); JButton buttonSub = new JButton("-"); buttonSub.addActionListener(e -> { if (ss && !stemp1.equals("")) { ss = false; resultField.setText(""); dec = false; } if (!ss) operation = '-'; }); buttonSub.setBounds(185, 304, 117, 25); frame.getContentPane().add(buttonSub); JButton buttonMul = new JButton("*"); buttonMul.addActionListener(arg0 -> { if (ss && !stemp1.equals("")) { ss = false; resultField.setText(""); dec = false; } if (!ss) operation = '*'; }); buttonMul.setBounds(318, 304, 117, 25); frame.getContentPane().add(buttonMul); JButton buttonDiv = new JButton("/"); buttonDiv.addActionListener(arg0 -> { if (ss && !stemp1.equals("")) { ss = false; resultField.setText(""); dec = false; } if (!ss) operation = '/'; }); buttonDiv.setBounds(469, 304, 117, 25); frame.getContentPane().add(buttonDiv); JButton buttonEq = new JButton("="); buttonEq.addActionListener(e -> { if (!ss && !stemp1.equals("") && !stemp2.equals("")) { ss = true; dec = false; if (stemp1.lastIndexOf(".") == stemp1.length() - 1) { stemp1 = stemp1.substring(0, stemp1.length() - 1); if (stemp1.length() <= 0) stemp1 = "0"; } if (stemp2.lastIndexOf(".") == stemp2.length() - 1) { stemp2 = stemp2.substring(0, stemp2.length() - 1); if (stemp2.length() <= 0) stemp2 = "0"; } stemp1.replaceAll("^0+", ""); stemp2.replaceAll("^0+", ""); num1.setText("num1 : " + stemp1); num2.setText("num2 : " + stemp2); OpField.setText(" " + Character.toString(operation)); if (operation == '+') resultField.setText(Double.toString (Double.parseDouble(stemp1) + Double.parseDouble(stemp2))); else if (operation == '-') resultField.setText(Double.toString (Double.parseDouble(stemp1) - Double.parseDouble(stemp2))); else if (operation == '*') resultField.setText(Double.toString (Double.parseDouble(stemp1) * Double.parseDouble(stemp2))); else if (operation == '/') { if (Double.parseDouble(stemp2) == 0.0) { JOptionPane.showMessageDialog(null, "Can't divide by zero, can you?"); } else { resultField.setText(Double.toString (Double.parseDouble(stemp1) / Double.parseDouble(stemp2))); } } stemp1 = ""; stemp2 = ""; operation = 'a'; } }); buttonEq.setBounds(497, 180, 117, 25); frame.getContentPane().add(buttonEq); resultField = new JTextField(); resultField.setFont(new Font("Accanthis ADF Std", Font.BOLD, 15)); resultField.setEditable(false); resultField.setBounds(46, 36, 389, 66); frame.getContentPane().add(resultField); resultField.setColumns(10); JButton buttonX = new JButton("X"); buttonX.addActionListener(e -> { if (ss) { if (!stemp1.equals("")) { stemp1 = stemp1.substring(0, stemp1.length() - 1); dec = stemp1.lastIndexOf(".") != -1; } if (stemp1.length() <= 0) { stemp1 = ""; dec = false; } resultField.setText(stemp1); if (stemp1.equals("") && stemp2.equals("")) { resultField.setText("0.0"); } } else { if (stemp1.length() <= 0) ss = true; else { if (!stemp2.equals("")) { stemp2 = stemp2.substring(0, stemp2.length() - 1); dec = stemp2.lastIndexOf(".") != -1; } if (stemp2.length() <= 0) { stemp2 = ""; dec = false; } resultField.setText(stemp2); } } }); buttonX.setBounds(497, 56, 117, 25); frame.getContentPane().add(buttonX); num1 = new JTextField(); num1.setEditable(false); num1.setText("num1 : "); num1.setBounds(46, 13, 114, 19); frame.getContentPane().add(num1); num1.setColumns(10); num2 = new JTextField(); num2.setEditable(false); num2.setText("num2 : "); num2.setBounds(321, 13, 114, 19); frame.getContentPane().add(num2); num2.setColumns(10); OpField = new JTextField(); OpField.setForeground(Color.BLUE); OpField.setFont(new Font("Dialog", Font.BOLD, 17)); OpField.setEditable(false); OpField.setBounds(186, 13, 114, 19); frame.getContentPane().add(OpField); OpField.setColumns(10); } }

Please help.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

More Books

Students also viewed these Programming questions