I'm currently working on a calculator using GUI.
This is what I mean and how it should work.
- After entering a sequence of number operator number the user can click: a. ‘=’, in which case the calculator must display: i. the ‘=’ symbol after the last digit of the second number ii. the result of the operation, on a new line iii. anything else entered after the ‘=’ symbol is part of a new calculation and must be displayed on a separate line
For example, the user clicks: 123.45+456.2=1”. The screen should look like this:
123.45+ entered by a user
456.2= entered by a user
579.65 calculated & displayed by your program
That is how I want the calculator to show the previous inputs and go to the new line once a mathematical operator has been clicked. Note, I've tried append as well but it didn't work.
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class CalculatorFrame extends JFrame {
/**
* All the buttons that will be used in the calculator have been initialized
*/
private JButton button1;
private JButton button2;
private JButton button3;
private JButton button4;
private JButton button5;
private JButton button6;
private JButton button7;
private JButton button8;
private JButton button9;
private JButton button0;
private JButton buttonEqual;
private JButton buttonDot;
private JButton buttonClearLast;
private JButton buttonClearAll;
private JButton buttonAdd;
private JButton buttonSub;
private JButton buttonMul;
private JButton buttonDiv;
private JTextArea textArea;
private JScrollPane scrollPane;
private JTextField textFieldResult;
String display = "";
private double TEMP;
private double equalTemp;
private int clearLastChar = 1;
Boolean additionBoolean = false;
Boolean subtractionBoolean = false;
Boolean multiplicationBoolean = false;
Boolean divisionBoolean = false;
public CalculatorFrame(){
JPanel panel2 = new JPanel();
panel2.setLayout(new GridLayout(1,1));
panel2.add(buttonClearLast = new JButton ("Clear Last"));
panel2.add(buttonClearAll = new JButton ("Clear All"));
add(panel2, BorderLayout.PAGE_START);
JPanel panel3 = new JPanel();
textArea = new JTextArea();
scrollPane = new JScrollPane(textArea);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
add(scrollPane);
add(panel3, BorderLayout.AFTER_LAST_LINE);
JPanel panel1 = new JPanel();
panel1.setLayout(new GridLayout(4,4));
panel1.add(button7 = new JButton ("7"));
panel1.add(button8 = new JButton ("8"));
panel1.add(button9 = new JButton ("9"));
panel1.add(buttonAdd = new JButton ("+"));
panel1.add(button4 = new JButton ("4"));
panel1.add(button5 = new JButton ("5"));
panel1.add(button6 = new JButton ("6"));
panel1.add(buttonSub = new JButton ("-"));
panel1.add(button1 = new JButton ("1"));
panel1.add(button2 = new JButton ("2"));
panel1.add(button3 = new JButton ("3"));
panel1.add(buttonMul = new JButton ("*"));
panel1.add(button0 = new JButton ("0"));
panel1.add(buttonDot = new JButton ("."));
panel1.add(buttonEqual = new JButton ("="));
panel1.add(buttonDiv = new JButton ("/"));
add(panel1, BorderLayout.PAGE_END);
pack();
buttonClearLast.addActionListener(new ListenToClearLast());
buttonClearAll.addActionListener(new ListenToClearAll());
button1.addActionListener(new ListenToOne());
button2.addActionListener(new ListenToTwo());
button3.addActionListener(new ListenToThree());
button4.addActionListener(new ListenToFour());
button5.addActionListener(new ListenToFive());
button6.addActionListener(new ListenToSix());
button7.addActionListener(new ListenToSeven());
button8.addActionListener(new ListenToEight());
button9.addActionListener(new ListenToNine());
button0.addActionListener(new ListenToZero());
buttonAdd.addActionListener(new ListenToAdd());
buttonSub.addActionListener(new ListenToSub());
buttonMul.addActionListener(new ListenToMul());
buttonDiv.addActionListener(new ListenToDiv());
buttonEqual.addActionListener(new ListenToEqual());
buttonDot.addActionListener(new ListenToDot());
}
/**
* This is where the action listener listens to all the button being pressed
* Once heard, it will show case it to the TextArea of the calculator.
*/
public class ListenToOne implements ActionListener{
public void actionPerformed(ActionEvent e){
// display = textArea.getText();
textArea.append("1");
}
}
public class ListenToTwo implements ActionListener{
public void actionPerformed(ActionEvent e){
// display = textArea.getText();
textArea.append("2");
}
}
public class ListenToThree implements ActionListener{
public void actionPerformed(ActionEvent e){
// display = textArea.getText();
textArea.append("3");
}
}
public class ListenToFour implements ActionListener{
public void actionPerformed(ActionEvent e){
// display = textArea.getText();
textArea.append("4");
}
}
public class ListenToFive implements ActionListener{
public void actionPerformed(ActionEvent e){
// display = textArea.getText();
textArea.append("5");
}
}
public class ListenToSix implements ActionListener{
public void actionPerformed(ActionEvent e){
// display = textArea.getText();
textArea.append("6");
}
}
public class ListenToSeven implements ActionListener{
public void actionPerformed(ActionEvent e){
// display = textArea.getText();
textArea.append("7");
}
}
public class ListenToEight implements ActionListener{
public void actionPerformed(ActionEvent e){
// display = textArea.getText();
textArea.append("8");
}
}
public class ListenToNine implements ActionListener{
public void actionPerformed(ActionEvent e){
// display = textArea.getText();
textArea.append("9");
}
}
public class ListenToZero implements ActionListener{
public void actionPerformed(ActionEvent e){
// display = textArea.getText();
textArea.append("0");
}
}
// This is used for decimal points.
// If the dot button is clicked, it will display "."
public class ListenToDot implements ActionListener{
public void actionPerformed(ActionEvent e){
// display = textArea.getText();
textArea.append(".");
}
}
// The next 4 methods are for the basic operators.
// If any of the operator button is clicked, it would set it's boolean value to true and
// tell the program which operation to perform
public class ListenToAdd implements ActionListener{
public void actionPerformed (ActionEvent e){
TEMP = Double.parseDouble(textArea.getText());
textArea.append("+\n");
additionBoolean = true;
}
}
public class ListenToSub implements ActionListener{
public void actionPerformed (ActionEvent e){
TEMP = Double.parseDouble(textArea.getText());
textArea.setText("- \n");
subtractionBoolean = true;
}
}
public class ListenToMul implements ActionListener{
public void actionPerformed (ActionEvent e){
TEMP = Double.parseDouble(textArea.getText());
textArea.setText("* \n");
multiplicationBoolean = true;
}
}
public class ListenToDiv implements ActionListener{
public void actionPerformed (ActionEvent e){
TEMP = Double.parseDouble(textArea.getText());
textArea.setText("/ \n");
divisionBoolean = true;
}
}
// This ListenToEqual method does all the calculation
// First, the program is checking what kind of calculation to perform by comparing it's boolean values.
// Once that is done, it will get the previous input from the user using the getText method and add/sub/mul/div with the new value
// The output will be displayed in the text area.
public class ListenToEqual implements ActionListener{
public void actionPerformed (ActionEvent e){
equalTemp = Double.parseDouble(textArea.getText());
if (additionBoolean == true)
equalTemp = equalTemp + TEMP;
else if (subtractionBoolean == true)
equalTemp = TEMP - equalTemp;
else if (multiplicationBoolean == true)
equalTemp = equalTemp * TEMP;
else if (divisionBoolean == true)
equalTemp = TEMP / equalTemp;
textArea.append(Double.toString(equalTemp));
// textArea.setText("1");
additionBoolean = false;
subtractionBoolean = false;
multiplicationBoolean = false;
divisionBoolean = false;
}
}
public class ListenToClearAll implements ActionListener{
public void actionPerformed (ActionEvent e){
textArea.setText("");
additionBoolean = false;
subtractionBoolean = false;
multiplicationBoolean = false;
divisionBoolean = false;
TEMP = 0;
equalTemp = 0;
}
}
public class ListenToClearLast implements ActionListener{
public void actionPerformed (ActionEvent e){
String currentChar = textArea.getText();
String currentCharMinus = currentChar.substring(0,currentChar.length()-clearLastChar);
textArea.setText(currentCharMinus);
}
}
}
This is how my calculator looks.
calculator:
Any help on how I can display the output as it is in the example above.
Thank you.