My Java GUI calculator keeps returing 0.0 instead of the correct answer to the o
ID: 3829013 • Letter: M
Question
My Java GUI calculator keeps returing 0.0 instead of the correct answer to the operation. Please help, here is my source code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* Sample application using Frame.
*
* @author
* @version
*/
public class WidgetDemoFrame extends JFrame implements ActionListener{
JTextField jb = new JTextField();
private double numbers1 = 0;
private double numbers2 = 0 ;
private byte function = -1;
public static void main (String [] args)
{
WidgetDemoFrame aWidgetDemoFrame = new WidgetDemoFrame();
aWidgetDemoFrame.setVisible(true);
}
/**
* The constructor.
*/
public WidgetDemoFrame() {
// this.numbers1 = 0;
// this.numbers2 = 0;
JMenuBar menuBar = new JMenuBar();
JMenu menuFile = new JMenu();
JMenuItem menuFileExit = new JMenuItem();
Container c = getContentPane();
c.setBackground(Color.YELLOW);
JButton b1 = new JButton("1");
JButton b2 = new JButton("2");
JButton b3 = new JButton("3");
JButton b4 = new JButton("4");
JButton b5 = new JButton("5");
JButton b6 = new JButton("6");
JButton b7 = new JButton("7");
JButton b8 = new JButton("8");
JButton b9 = new JButton("9");
JButton b0 = new JButton("0");
JButton plus = new JButton("+");
JButton minus = new JButton("-");
JButton divide = new JButton("/");
JButton multiply = new JButton("*");
JButton clear = new JButton("C");
JButton equals = new JButton("=");
setLayout(new GridLayout(5,1));
JPanel r1 = new JPanel();
r1.setBackground(Color.RED);
JPanel r2 = new JPanel();
r2.setBackground(Color.WHITE);
JPanel r3 = new JPanel();
r3.setBackground(Color.BLUE);
JPanel r4 = new JPanel();
r4.setBackground(Color.YELLOW);
JPanel r5 = new JPanel();
r5.setBackground(Color.CYAN);
add(r1);
add(r2);
add(r3);
add(r4);
add(r5);
r1.setLayout(new GridLayout(1,1));
r1.add(jb);
r2.setLayout(new GridLayout(1,4));
r2.add(b1);
r2.add(b2);
r2.add(b3);
r2.add(plus);
r3.setLayout(new GridLayout(1,4));
r3.add(b4);
r3.add(b5);
r3.add(b6);
r3.add(minus);
r4.setLayout(new GridLayout(1,4));
r4.add(b7);
r4.add(b8);
r4.add(b9);
r4.add(divide);
r5.setLayout(new GridLayout(1,4));
r5.add(clear);
r5.add(b0);
r5.add(equals);
r5.add(multiply);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b0.addActionListener(this);
clear.addActionListener(this);
equals.addActionListener(this);
minus.addActionListener(this);
divide.addActionListener(this);
multiply.addActionListener(this);
plus.addActionListener(this);
EnterButton enter = new EnterButton();
equals.addActionListener(enter);
menuFile.setLabel("File");
menuFileExit.setLabel("Exit");
// Add action listener.for the menu button
menuFileExit.addActionListener
(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
WidgetDemoFrame.this.windowClosed();
}
}
);
menuFile.add(menuFileExit);
menuBar.add(menuFile);
setTitle("WidgetDemo");
setJMenuBar(menuBar);
setSize(new Dimension(400, 400));
// Add window listener.
this.addWindowListener
(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
WidgetDemoFrame.this.windowClosed();
}
}
);
}
@Override
public void actionPerformed(ActionEvent e){
String bs = e.getActionCommand();
if(bs.equals("1")){
String x = jb.getText();
jb.setText(x+"1");
}
if(bs.equals("2")){
String x = jb.getText();
jb.setText(x+"2");
}
if(bs.equals("3")){
String x = jb.getText();
jb.setText(x+"3");
}
if(bs.equals("4")){
String x = jb.getText();
jb.setText(x+"4");
}
if(bs.equals("5")){
String x = jb.getText();
jb.setText(x+"5");
}
if(bs.equals("6")){
String x = jb.getText();
jb.setText(x+"6");
}
if(bs.equals("7")){
String x = jb.getText();
jb.setText(x+"7");
}
if(bs.equals("8")){
String x = jb.getText();
jb.setText(x+"8");
}
if(bs.equals("9")){
String x = jb.getText();
jb.setText(x+"9");
}
if(bs.equals("C")){
jb.setText(" ");
}
if(bs.equals("-")){
jb.setText(" ");
}
if(bs.equals("+")){
jb.setText(" ");
}
if(bs.equals("/")){
jb.setText(" ");
}
if(bs.equals("*")){
jb.setText(" ");
}
}
private void windowClosed() {
System.exit(0);
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
//divide listener
private class DivideButton implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (numbers1 == 0) {
numbers1 = Double.parseDouble(jb.getText());
jb.setText("");
} else {
numbers2 = Double.parseDouble(jb.getText());
jb.setText("");
}
function = 0;
}
}
private class MultiplyButton implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (numbers1 == 0) {
numbers1 = Double.parseDouble(jb.getText());
jb.setText("");
} else {
numbers2 = Double.parseDouble(jb.getText());
jb.setText("");
}
function = 1;
}
}
private class AddButton implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (numbers1 == 0) {
numbers1 = Double.parseDouble(jb.getText());
jb.setText(" ");
} else {
numbers2 = Double.parseDouble(jb.getText());
jb.setText(" ");
}
function = 2;
}
}
private class SubtractButton implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (numbers1 == 0) {
numbers1 = Double.parseDouble(jb.getText());
jb.setText("");
} else {
numbers2 = Double.parseDouble(jb.getText());
jb.setText("");
}
function = 3;
}
}
private class EnterButton implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
numbers2 = Double.parseDouble(jb.getText());
if (function == 0) {
jb.setText(Double.toString((Math.round((numbers1 / numbers2) * 100)) / 100));
} else if (function == 1) {
jb.setText(Double.toString(numbers1 * numbers2));
} else if (function == 2) {
jb.setText(Double.toString(numbers2 + numbers1));
} else if (function == 3) {
jb.setText(Double.toString(numbers1 - numbers2));
} else {
jb.setText(String.valueOf(numbers1));
}
numbers1 = Double.parseDouble(jb.getText());
}
}
}
Explanation / Answer
//There was small mistake
//you was not adding corresponding lisner classes to + ,- ,/ and * button
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* Sample application using Frame.
*
* @author
* @version
*/
public class WidgetDemoFrame extends JFrame implements ActionListener{
JTextField jb = new JTextField();
private double numbers1 = 0;
private double numbers2 = 0 ;
private byte function = -1;
public static void main (String [] args)
{
WidgetDemoFrame aWidgetDemoFrame = new WidgetDemoFrame();
aWidgetDemoFrame.setVisible(true);
}
/**
* The constructor.
*/
public WidgetDemoFrame() {
// this.numbers1 = 0;
// this.numbers2 = 0;
JMenuBar menuBar = new JMenuBar();
JMenu menuFile = new JMenu();
JMenuItem menuFileExit = new JMenuItem();
Container c = getContentPane();
c.setBackground(Color.YELLOW);
JButton b1 = new JButton("1");
JButton b2 = new JButton("2");
JButton b3 = new JButton("3");
JButton b4 = new JButton("4");
JButton b5 = new JButton("5");
JButton b6 = new JButton("6");
JButton b7 = new JButton("7");
JButton b8 = new JButton("8");
JButton b9 = new JButton("9");
JButton b0 = new JButton("0");
JButton plus = new JButton("+");
JButton minus = new JButton("-");
JButton divide = new JButton("/");
JButton multiply = new JButton("*");
JButton clear = new JButton("C");
JButton equals = new JButton("=");
setLayout(new GridLayout(5,1));
JPanel r1 = new JPanel();
r1.setBackground(Color.RED);
JPanel r2 = new JPanel();
r2.setBackground(Color.WHITE);
JPanel r3 = new JPanel();
r3.setBackground(Color.BLUE);
JPanel r4 = new JPanel();
r4.setBackground(Color.YELLOW);
JPanel r5 = new JPanel();
r5.setBackground(Color.CYAN);
add(r1);
add(r2);
add(r3);
add(r4);
add(r5);
r1.setLayout(new GridLayout(1,1));
r1.add(jb);
r2.setLayout(new GridLayout(1,4));
r2.add(b1);
r2.add(b2);
r2.add(b3);
r2.add(plus);
r3.setLayout(new GridLayout(1,4));
r3.add(b4);
r3.add(b5);
r3.add(b6);
r3.add(minus);
r4.setLayout(new GridLayout(1,4));
r4.add(b7);
r4.add(b8);
r4.add(b9);
r4.add(divide);
r5.setLayout(new GridLayout(1,4));
r5.add(clear);
r5.add(b0);
r5.add(equals);
r5.add(multiply);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b0.addActionListener(this);
clear.addActionListener(this);
equals.addActionListener(this);
minus.addActionListener(new SubtractButton());
divide.addActionListener(new DivideButton());
multiply.addActionListener(new MultiplyButton());
plus.addActionListener(new AddButton());
EnterButton enter = new EnterButton();
equals.addActionListener(enter);
menuFile.setLabel("File");
menuFileExit.setLabel("Exit");
// Add action listener.for the menu button
menuFileExit.addActionListener
(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
WidgetDemoFrame.this.windowClosed();
}
}
);
menuFile.add(menuFileExit);
menuBar.add(menuFile);
setTitle("WidgetDemo");
setJMenuBar(menuBar);
setSize(new Dimension(400, 400));
// Add window listener.
this.addWindowListener
(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
WidgetDemoFrame.this.windowClosed();
}
}
);
}
@Override
public void actionPerformed(ActionEvent e){
String bs = e.getActionCommand();
if(bs.equals("1")){
String x = jb.getText();
jb.setText(x+"1");
}
if(bs.equals("2")){
String x = jb.getText();
jb.setText(x+"2");
}
if(bs.equals("3")){
String x = jb.getText();
jb.setText(x+"3");
}
if(bs.equals("4")){
String x = jb.getText();
jb.setText(x+"4");
}
if(bs.equals("5")){
String x = jb.getText();
jb.setText(x+"5");
}
if(bs.equals("6")){
String x = jb.getText();
jb.setText(x+"6");
}
if(bs.equals("7")){
String x = jb.getText();
jb.setText(x+"7");
}
if(bs.equals("8")){
String x = jb.getText();
jb.setText(x+"8");
}
if(bs.equals("9")){
String x = jb.getText();
jb.setText(x+"9");
}
if(bs.equals("C")){
jb.setText(" ");
}
if(bs.equals("-")){
jb.setText(" ");
}
if(bs.equals("+")){
jb.setText(" ");
}
if(bs.equals("/")){
jb.setText(" ");
}
if(bs.equals("*")){
jb.setText(" ");
}
}
private void windowClosed() {
System.exit(0);
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
//divide listener
private class DivideButton implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (numbers1 == 0) {
numbers1 = Double.parseDouble(jb.getText());
jb.setText("");
} else {
numbers2 = Double.parseDouble(jb.getText());
jb.setText("");
}
function = 0;
}
}
private class MultiplyButton implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (numbers1 == 0) {
numbers1 = Double.parseDouble(jb.getText());
jb.setText("");
} else {
numbers2 = Double.parseDouble(jb.getText());
jb.setText("");
}
function = 1;
}
}
private class AddButton implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("numbers1 "+numbers1);
numbers1 = Double.parseDouble(jb.getText());
jb.setText("");
function = 2;
}
}
private class SubtractButton implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (numbers1 == 0) {
numbers1 = Double.parseDouble(jb.getText());
jb.setText("");
} else {
numbers2 = Double.parseDouble(jb.getText());
jb.setText("");
}
// numbers1 = Double.parseDouble(jb.getText());
function = 3;
}
}
private class EnterButton implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
numbers2 = Double.parseDouble(jb.getText());
System.out.println("number2 "+numbers2+" number1 "+numbers1);
if (function == 0) {
jb.setText(Double.toString((Math.round((numbers1 / numbers2) * 100)) / 100));
} else if (function == 1) {
jb.setText(Double.toString(numbers1 * numbers2));
} else if (function == 2) {
jb.setText(Double.toString(numbers2 + numbers1));
} else if (function == 3) {
jb.setText(Double.toString(numbers1 - numbers2));
} else {
jb.setText(String.valueOf(numbers1));
}
numbers1 = Double.parseDouble(jb.getText());
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.