Java GUI calculator
Closed this issue · 0 comments
i am having issues with a java gui calculator i am trying to work want on as a beginner. I want my digits to keep displaying on the screen of my java gui calculator until an equal to sign is clicked.
enter code here
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.text.JTextComponent;
import javax.swing.text.TextAction;
public class calculatorWorkspace extends javax.swing.JFrame {
private double Num1;
private double solvedNum1;
String display = "";
Boolean addBool = false;
Boolean subBool = false;
Boolean mulBool = false;
Boolean divBool = false;
public calculatorWorkspace() {
initComponents();
jButton1.addActionListener(new ListenToOne());
jButton2.addActionListener(new ListenToTwo());
jButton3.addActionListener(new ListenToThree());
jButton4.addActionListener(new ListenToFour());
jButton5.addActionListener(new ListenToFive());
jButton6.addActionListener(new ListenToSix());
jButton7.addActionListener(new ListenToSeven());
jButton8.addActionListener(new ListenToEight());
jButton9.addActionListener(new ListenToNine());
jButton0.addActionListener(new ListenToZero());
jButtonadd.addActionListener(new ListenToAdd());
jButtonClear.addActionListener(new ListenToClear());
jButtonbackspace.addActionListener(new ListenToBackspace());
jButtonsub.addActionListener(new ListenToSub());
jButtondivide.addActionListener(new ListenToDivide());
jButtonmultiply.addActionListener(new ListenToMultiply());
jButtondot.addActionListener(new ListenToDot());
jButtonequal.addActionListener(new ListenToEqual());
}
class ListenToOne implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
solutionField.getText();
solutionField.setText(display + "1");
}
}
class ListenToTwo implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
solutionField.getText();
solutionField.setText(display + "2");
}
}
class ListenToThree implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
solutionField.getText();
solutionField.setText(display + "3");
}
}
class ListenToFour implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
solutionField.getText();
solutionField.setText(display + "4");
}
}
class ListenToFive implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
solutionField.getText();
solutionField.setText(display + "5");
}
}
class ListenToSix implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
solutionField.getText();
solutionField.setText(display + "6");
}
}
class ListenToSeven implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
solutionField.getText();
solutionField.setText(display + "7");
}
}
class ListenToEight implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
solutionField.getText();
solutionField.setText(display + "8");
}
}
class ListenToNine implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
solutionField.getText();
solutionField.setText(display + "9");
}
}
class ListenToZero implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
solutionField.getText();
solutionField.setText(display + "0");
}
}
class ListenToDot implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
solutionField.getText();
solutionField.setText(display + ".");
}
}
class ListenToAdd implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
Num1 = Double.parseDouble(solutionField.getText());
solutionField.setText("");
addBool = true;
}
}
class ListenToSub implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
Num1 = Double.parseDouble(solutionField.getText());
solutionField.setText("");
subBool = true;
}
}
class ListenToMultiply implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
Num1 = Double.parseDouble(solutionField.getText());
solutionField.setText("");
mulBool = true;
}
}
class ListenToDivide implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
Num1 = Double.parseDouble(solutionField.getText());
solutionField.setText("");
divBool = true;
}
}
class ListenToClear implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
solutionField.setText("");
addBool = false;
subBool = false;
mulBool = false;
divBool = false;
Num1 = 0;
solvedNum1 = 0;
}
}
class ListenToBackspace implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
solutionField.getText();
solutionField.setText("");
}
}
class ListenToEqual implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
solvedNum1 = Double.parseDouble(solutionField.getText());
if (addBool == true)
{
solvedNum1 = solvedNum1 + Num1;
}
else if (subBool == true)
{
solvedNum1 = solvedNum1 - Num1;
}
else if (mulBool == true)
{
solvedNum1 = solvedNum1 * Num1;
}
else if (divBool == true)
{
solvedNum1 = solvedNum1 / Num1;
}
solutionField.setText(Double.toString(solvedNum1));
addBool = false;
subBool = false;
mulBool = false;
divBool = false;
}
}
private void initComponents() {
buttonGroup1 = new javax.swing.ButtonGroup();
buttonGroup2 = new javax.swing.ButtonGroup();
figurePanel = new javax.swing.JPanel();
screenPanel = new javax.swing.JPanel();
solutionField = new javax.swing.JTextField();
figurePanel1 = new javax.swing.JPanel();
jButton6 = new javax.swing.JButton();
jButton9 = new javax.swing.JButton();
jButtonasterik = new javax.swing.JButton();
jButton7 = new javax.swing.JButton();
jButton8 = new javax.swing.JButton();
jButtonClear = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButtonmod = new javax.swing.JButton();
jButtonsub = new javax.swing.JButton();
jButtonmultiply = new javax.swing.JButton();
jButtondivide = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButtonroot = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jButton0 = new javax.swing.JButton();
jButtonequal = new javax.swing.JButton();
jButtondot = new javax.swing.JButton();
jButton1 = new javax.swing.JButton();
jButtonbackspace = new javax.swing.JButton();
jButtonadd = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setBackground(new java.awt.Color(153, 153, 153));
pack();
setLocationRelativeTo(null);
}
public static void main(String args[]) {
// Variables declaration
private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.ButtonGroup buttonGroup2;
private javax.swing.JPanel figurePanel;
private javax.swing.JPanel figurePanel1;
private javax.swing.JButton jButton0;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JButton jButton6;
private javax.swing.JButton jButton7;
private javax.swing.JButton jButton8;
private javax.swing.JButton jButton9;
private javax.swing.JButton jButtonClear;
private javax.swing.JButton jButtonadd;
private javax.swing.JButton jButtonasterik;
private javax.swing.JButton jButtonbackspace;
private javax.swing.JButton jButtondivide;
private javax.swing.JButton jButtondot;
private javax.swing.JButton jButtonequal;
private javax.swing.JButton jButtonmod;
private javax.swing.JButton jButtonmultiply;
private javax.swing.JButton jButtonroot;
private javax.swing.JButton jButtonsub;
private javax.swing.JPanel screenPanel;
private javax.swing.JTextField solutionField;
// End of variables declaration
}