Home > Blockchain >  Trying to create a calculator with Java. Button inputs won't print in the textfield
Trying to create a calculator with Java. Button inputs won't print in the textfield

Time:04-28

I'm trying to create a scientific calculator for class. I'm not sure where I'm going wrong, but it will not print any button inputs into the textfield. Does anybody know where I'm going wrong? Problems come from the 'calc' class at the bottom of the code. I've only coded the calculator to respond to number inputs, so no other buttons will work, but the number buttons should work and I don't know how to fix it. I'm still very new to programming

package scientificcalc;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ScientificCalc extends JFrame {
    JFrame Frame;
    JPanel Panel;
    JTextField Text;
    JButton Button[]= new JButton[25]; // Array holds all calculator buttons
    String val0, val1, val2; // Each val records a button pressed
    public ScientificCalc() {
        val0 = val1 = val2 = "";
        Frame = new JFrame("Scientific Calculator");
        Panel = new JPanel();
        Panel.setBackground(Color.RED);
        Text = new JTextField(20);
        Text.setEditable(false);
        Frame.setVisible(true);
        Frame.setSize(400,400);
        Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ActionListener a = null;
        // Following Lines create each button
        Button[0] = new JButton("0");
        Button[0].addActionListener(a);
        Button[1] = new JButton("1");
        Button[1].addActionListener(a);
        Button[2] = new JButton("2");
        Button[2].addActionListener(a);
        Button[3] = new JButton("3");
        Button[3].addActionListener(a);
        Button[4] = new JButton("4");
        Button[4].addActionListener(a);
        Button[5] = new JButton("5");
        Button[5].addActionListener(a);
        Button[6] = new JButton("6");
        Button[6].addActionListener(a);
        Button[7] = new JButton("7");
        Button[7].addActionListener(a);
        Button[8] = new JButton("8");
        Button[8].addActionListener(a);
        Button[9] = new JButton("9");
        Button[9].addActionListener(a);
        Button[10] = new JButton(" ");
        Button[10].addActionListener(a);
        Button[11] = new JButton("-");
        Button[11].addActionListener(a);
        Button[12] = new JButton("*");
        Button[12].addActionListener(a);
        Button[13] = new JButton("/");
        Button[13].addActionListener(a);
        Button[14] = new JButton("SQRT");
        Button[14].addActionListener(a);
        Button[15] = new JButton("Sin");
        Button[15].addActionListener(a);
        Button[16] = new JButton("Cos");
        Button[16].addActionListener(a);
        Button[17] = new JButton("Tan");
        Button[17].addActionListener(a);
        Button[18] = new JButton("1/x");
        Button[18].addActionListener(a);
        Button[19] = new JButton("x^2");
        Button[19].addActionListener(a);
        Button[20] = new JButton("log");
        Button[20].addActionListener(a);
        Button[21] = new JButton("!");
        Button[21].addActionListener(a);
        Button[22] = new JButton(".");
        Button[22].addActionListener(a);
        Button[23] = new JButton("=");
        Button[23].addActionListener(a);
        Button[24] = new JButton("Clear");
        Button[24].addActionListener(a);
        // Following Lines add everything to Panel and Frame
        Panel.add(Text);
        Panel.add(Button[0]);
        Panel.add(Button[1]);
        Panel.add(Button[2]);
        Panel.add(Button[3]);
        Panel.add(Button[4]);
        Panel.add(Button[5]);
        Panel.add(Button[6]);
        Panel.add(Button[7]);
        Panel.add(Button[8]);
        Panel.add(Button[9]);
        Panel.add(Button[10]);
        Panel.add(Button[11]);
        Panel.add(Button[12]);
        Panel.add(Button[13]);
        Panel.add(Button[14]);
        Panel.add(Button[15]);
        Panel.add(Button[16]);
        Panel.add(Button[17]);
        Panel.add(Button[18]);
        Panel.add(Button[19]);
        Panel.add(Button[20]);
        Panel.add(Button[21]);
        Panel.add(Button[22]);
        Panel.add(Button[23]);
        Panel.add(Button[24]);
        Frame.add(Panel);
    }
    
    public class calc implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            String i = e.getActionCommand();
            if ((i.charAt(0) >= '0' && i.charAt(0) <= '9') || i.charAt(0) == '.') {
                if (!val1.equals("")) {
                    val2 = val2   i;
                } else {
                    val0 = val0   i; // Sets location of each digit when button is pressed
                }
                Text.setText(val0 val1 val2);
            }
        }
    }
    public static void main(String[] args) {
        new ScientificCalc();
    }
}

CodePudding user response:

You're currently adding your ActionListener a which you set to null to all of your buttons. Change ActionListener a = null to ActionListener a = new calc();

CodePudding user response:

You forgot to initialize calc object by ActionListener a = new calc();

  1. Use field names starting with small letters!
  2. Use loops instead of repetitive code!
  3. Start class names with capital letters!
  4. Name calc class by CustomActionListener Below there is pretty code!

Question: why do you check it if it's always false? if (!val1.equals(""))

package scientificcalc;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ScientificCalc extends JFrame {
    JFrame frame;
    JPanel panel;
    JTextField textField;
    JButton[] buttonArray = new JButton[25]; // Array holds all calculator buttons
    String val0, val1, val2; // Each val records a button pressed

    public ScientificCalc() {
        val0 = val1 = val2 = "";
        frame = new JFrame("Scientific Calculator");
        panel = new JPanel();
        panel.setBackground(Color.RED);
        textField = new JTextField(20);
        textField.setEditable(false);
        frame.setVisible(true);
        frame.setSize(400, 400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        CustomActionListener actionListener = new CustomActionListener();
        // Following Lines create each button
        buttonArray[0] = new JButton("0");
        buttonArray[1] = new JButton("1");
        buttonArray[2] = new JButton("2");
        buttonArray[3] = new JButton("3");
        buttonArray[4] = new JButton("4");
        buttonArray[5] = new JButton("5");
        buttonArray[6] = new JButton("6");
        buttonArray[7] = new JButton("7");
        buttonArray[8] = new JButton("8");
        buttonArray[9] = new JButton("9");
        buttonArray[10] = new JButton(" ");
        buttonArray[11] = new JButton("-");
        buttonArray[12] = new JButton("*");
        buttonArray[13] = new JButton("/");
        buttonArray[14] = new JButton("SQRT");
        buttonArray[15] = new JButton("Sin");
        buttonArray[16] = new JButton("Cos");
        buttonArray[17] = new JButton("Tan");
        buttonArray[18] = new JButton("1/x");
        buttonArray[19] = new JButton("x^2");
        buttonArray[20] = new JButton("log");
        buttonArray[21] = new JButton("!");
        buttonArray[22] = new JButton(".");
        buttonArray[23] = new JButton("=");
        buttonArray[24] = new JButton("Clear");
        for (int i = 0; i < buttonArray.length; i  ) {
            buttonArray[i].addActionListener(actionListener);
        }
        // Following Lines add everything to Panel and Frame
        panel.add(textField);
        for (JButton jButton : buttonArray) {
            panel.add(jButton);
        }
        frame.add(panel);
    }

    class CustomActionListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            String i = e.getActionCommand();
            if ((i.charAt(0) >= '0' && i.charAt(0) <= '9') || i.charAt(0) == '.') {
                if (!val1.equals("")) {
                    val2 = val2   i;
                } else {
                    val0 = val0   i; // Sets location of each digit when button is pressed
                }
                textField.setText(val0   val1   val2);
            }
        }
    }

    public static void main(String[] args) {
        new ScientificCalc();
    }
}
  • Related