Hey guys So i am just having one little issue with my GUI that I need help with!
ID: 3854822 • Letter: H
Question
Hey guys So i am just having one little issue with my GUI that I need help with! Not sure why but I am getting a runtime error when I store and read the numbers back! Anyone that could fix this and point out what I need to do so i understand it better would be greatly appreciated!! :)
Here is my code and I need this to be done in Java Eclipse!
package week07_Ben_Calculator;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class AdvancedCalculatorGUI implements ActionListener {
JFrame frame;
JPanel butPanel;
JTextField res;
JTextField res2;
int oper = 0;
double currentCalc;
double last;
double memory = 0;
int temp = 0;
boolean hit = false;
public static void main(String[] args) {
// Invocation
EventQueue.invokeLater(new Runnable() {
// Override run
@Override
public void run() {
// Call constructor
new AdvancedCalculatorGUI();
}
});
}
// Create GUI
public AdvancedCalculatorGUI() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Calculator");
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
// New text field
res = new JTextField();
res.setHorizontalAlignment(JTextField.RIGHT);
res.setEditable(false);
frame.add(res, BorderLayout.NORTH);
butPanel = new JPanel();
// 2nd text field
res2 = new JTextField();
res2.setHorizontalAlignment(JTextField.LEFT);
res2.setEditable(false);
frame.add(res2, BorderLayout.SOUTH);
// Create grid
butPanel.setLayout(new GridLayout(6, 3));
frame.add(butPanel, BorderLayout.CENTER);
// Add button
for (int i = 0; i < 10; i++) {
addButton(butPanel, String.valueOf(i));
}
// read button
JButton readButton = new JButton("Read");
readButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
memory = Double.parseDouble(res.getText());
res.setText("");
}
});
// store button
JButton storeButton = new JButton("Store");
storeButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
res.setText(memory + "");
}
});
JButton clearButton = new JButton("Clear");
clearButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
res.setText("");
res2.setText("");
}
});
// Add button +
JButton additionButton = new JButton("+");
// additionButton.setActionCommand("+");
additionButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ev) {
oper = 1;
currentCalc = Double.parseDouble(res.getText());
res.setText("");
res2.setText(res2.getText() + "+");
}
});
operAct additionAction = new operAct(1);
//additionButton.addActionListener(additionAction);
// Subtract button
JButton subtractionButton = new JButton("-");
subtractionButton.setActionCommand("-");
subtractionButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ev) {
oper = 2;
currentCalc = Double.parseDouble(res.getText());
res.setText("");
res2.setText(res2.getText() + "-");
}
});
operAct subtractionAction = new operAct(2);
subtractionButton.addActionListener(subtractionAction);
// Equal button
JButton eqButton = new JButton("=");
eqButton.setActionCommand("=");
eqButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ev) {
System.out.println(oper);
if (!res.getText().isEmpty()) {
double number = Double.parseDouble(res.getText());
if (oper == 1) {
double value = currentCalc + number;
last = value;
res.setText(Double.toString(value));
res2.setText(res2.getText() + "=" + Double.toString(value));
} else if (oper == 2) {
double value = currentCalc - number;
last = value;
res.setText(Double.toString(value));
res2.setText(res2.getText() + "=" + Double.toString(value));
} else if (oper == 3) {
double value = currentCalc * number;
last = value;
res.setText(Double.toString(value));
res2.setText(res2.getText() + "=" + Double.toString(value));
} else if (oper == 4) {
if (number == 0)
res.setText("ERR");
double value = currentCalc / number;
last = value;
res.setText(Double.toString(value));
res2.setText(res2.getText() + "=" + Double.toString(value));
}
}
hit = true;
}
});
// multiplication button
JButton mulButton = new JButton("*");
mulButton.setActionCommand("*");
mulButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ev) {
oper = 3;
currentCalc = Double.parseDouble(res.getText());
res.setText("");
res2.setText(res2.getText() + "*");
}
});
operAct mulAction = new operAct(3);
//mulButton.addActionListener(mulAction);
// division button
JButton divButton = new JButton("/");
divButton.setActionCommand("/");
divButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ev) {
oper = 4;
currentCalc = Double.parseDouble(res.getText());
res.setText("");
res2.setText(res2.getText() + "/");
}
});
operAct divAction = new operAct(4);
//divButton.addActionListener(divAction);
butPanel.add(additionButton);
butPanel.add(subtractionButton);
butPanel.add(eqButton);
butPanel.add(mulButton);
butPanel.add(divButton);
butPanel.add(readButton);
butPanel.add(clearButton);
butPanel.add(storeButton);
frame.setVisible(true);
}
private void addButton(Container par, String nam) {
JButton b = new JButton(nam);
b.setActionCommand(nam);
b.addActionListener(this);
par.add(b);
}
@Override
public void actionPerformed(ActionEvent ev) {
String act = ev.getActionCommand();
if (hit) {
res.setText(act);
res2.setText(res2.getText() + "" + act);
hit = false;
}
else {
//Append the new number
res.setText(res.getText() + act);
System.out.println("res: " + res.getText());
res2.setText(res2.getText() + "" + act);
}
}
private class operAct implements ActionListener {
private int opt;
public operAct(int oper) {
opt = oper;
}
public void actionPerformed(ActionEvent ev) {
currentCalc = Double.parseDouble(res.getText());
oper = opt;
System.out.println(oper + " " + opt + " " + currentCalc);
}
}
}
Explanation / Answer
Problems with your code
You had exchanged the codes of store button and read button. Whenever you were pressing the store button, your program was reading from memory instead of storing and whenever you were pressing read button, your program was doing vice-versa. Hence it was generating an error of unknown source.
Corrected Java Code
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class AdvancedCalculatorGUI implements ActionListener {
JFrame frame;
JPanel butPanel;
JTextField res;
JTextField res2;
int oper = 0;
double currentCalc;
double last;
double memory = 0;
int temp = 0;
boolean hit = false;
public static void main(String[] args) {
// Invocation
EventQueue.invokeLater(new Runnable() {
// Override run
@Override
public void run() {
// Call constructor
new AdvancedCalculatorGUI();
}
});
}
// Create GUI
public AdvancedCalculatorGUI() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Calculator");
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
// New text field
res = new JTextField();
res.setHorizontalAlignment(JTextField.RIGHT);
res.setEditable(false);
frame.add(res, BorderLayout.NORTH);
butPanel = new JPanel();
// 2nd text field
res2 = new JTextField();
res2.setHorizontalAlignment(JTextField.LEFT);
res2.setEditable(false);
frame.add(res2, BorderLayout.SOUTH);
// Create grid
butPanel.setLayout(new GridLayout(6, 3));
frame.add(butPanel, BorderLayout.CENTER);
// Add button
for (int i = 0; i < 10; i++) {
addButton(butPanel, String.valueOf(i));
}
// read button
JButton readButton = new JButton("Read");
readButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
res.setText(res.getText()+memory + "");
}
});
// store button
JButton storeButton = new JButton("Store");
storeButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
memory = Double.parseDouble(res.getText());
}
});
JButton clearButton = new JButton("Clear");
clearButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
res.setText("");
res2.setText("");
}
});
// Add button +
JButton additionButton = new JButton("+");
// additionButton.setActionCommand("+");
additionButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ev) {
oper = 1;
currentCalc = Double.parseDouble(res.getText());
res.setText("");
res2.setText(res2.getText() + "+");
}
});
operAct additionAction = new operAct(1);
//additionButton.addActionListener(additionAction);
// Subtract button
JButton subtractionButton = new JButton("-");
subtractionButton.setActionCommand("-");
subtractionButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ev) {
oper = 2;
currentCalc = Double.parseDouble(res.getText());
res.setText("");
res2.setText(res2.getText() + "-");
}
});
operAct subtractionAction = new operAct(2);
subtractionButton.addActionListener(subtractionAction);
// Equal button
JButton eqButton = new JButton("=");
eqButton.setActionCommand("=");
eqButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ev) {
System.out.println(oper);
if (!res.getText().isEmpty()) {
double number = Double.parseDouble(res.getText());
if (oper == 1) {
double value = currentCalc + number;
last = value;
res.setText(Double.toString(value));
res2.setText(res2.getText() + "=" + Double.toString(value));
} else if (oper == 2) {
double value = currentCalc - number;
last = value;
res.setText(Double.toString(value));
res2.setText(res2.getText() + "=" + Double.toString(value));
} else if (oper == 3) {
double value = currentCalc * number;
last = value;
res.setText(Double.toString(value));
res2.setText(res2.getText() + "=" + Double.toString(value));
} else if (oper == 4) {
if (number == 0)
res.setText("ERR");
double value = currentCalc / number;
last = value;
res.setText(Double.toString(value));
res2.setText(res2.getText() + "=" + Double.toString(value));
}
}
hit = true;
}
});
// multiplication button
JButton mulButton = new JButton("*");
mulButton.setActionCommand("*");
mulButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ev) {
oper = 3;
currentCalc = Double.parseDouble(res.getText());
res.setText("");
res2.setText(res2.getText() + "*");
}
});
operAct mulAction = new operAct(3);
//mulButton.addActionListener(mulAction);
// division button
JButton divButton = new JButton("/");
divButton.setActionCommand("/");
divButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ev) {
oper = 4;
currentCalc = Double.parseDouble(res.getText());
res.setText("");
res2.setText(res2.getText() + "/");
}
});
operAct divAction = new operAct(4);
//divButton.addActionListener(divAction);
butPanel.add(additionButton);
butPanel.add(subtractionButton);
butPanel.add(eqButton);
butPanel.add(mulButton);
butPanel.add(divButton);
butPanel.add(readButton);
butPanel.add(clearButton);
butPanel.add(storeButton);
frame.setVisible(true);
}
private void addButton(Container par, String nam) {
JButton b = new JButton(nam);
b.setActionCommand(nam);
b.addActionListener(this);
par.add(b);
}
@Override
public void actionPerformed(ActionEvent ev) {
String act = ev.getActionCommand();
if (hit) {
res.setText(act);
res2.setText(res2.getText() + "" + act);
hit = false;
}
else {
//Append the new number
res.setText(res.getText() + act);
System.out.println("res: " + res.getText());
res2.setText(res2.getText() + "" + act);
}
}
private class operAct implements ActionListener {
private int opt;
public operAct(int oper) {
opt = oper;
}
public void actionPerformed(ActionEvent ev) {
currentCalc = Double.parseDouble(res.getText());
oper = opt;
System.out.println(oper + " " + opt + " " + currentCalc);
}
}
}
Please let me know in the comments if you have any doubt. And if you liked it then you can give a thumbs up.