0

I'm a Java Noob and working on the GUI Calculator and I just ended up here.. I've got the buttons and I need to bind those numbers and store somewhere between the operators ( + - * / ) to show on my JTextArea .. How do I get the values from the buttons and apply the formula?

thanks you for your time... :)

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class Compute implements ActionListener{

    public String firstNumber;
    public String secondNumber;
    public String resultNumber;
    public double result = 0.0;

    JFrame frame;
    JPanel panel;
    JButton btn_0,btn_1,btn_2,btn_3,btn_4,btn_5,btn_6,btn_7,btn_8,btn_9,btn_C,btn_Add,btn_Sub,btn_Mul,btn_Div,btn_E;
    JTextArea area;

    public Compute() {
        area = new JTextArea();
        frame = new JFrame();
        panel = new JPanel();
        btn_0 = new JButton("0");
        btn_0.addActionListener(this);
        btn_1 = new JButton("1");
        btn_1.addActionListener(this);
        btn_2 = new JButton("2");
        btn_2.addActionListener(this);
        btn_3 = new JButton("3");
        btn_3.addActionListener(this);
        btn_4 = new JButton("4");
        btn_4.addActionListener(this);
        btn_5 = new JButton("5");
        btn_5.addActionListener(this);
        btn_6 = new JButton("6");
        btn_6.addActionListener(this);
        btn_7 = new JButton("7");
        btn_7.addActionListener(this);
        btn_8 = new JButton("8");
        btn_8.addActionListener(this);
        btn_9 = new JButton("9");
        btn_9.addActionListener(this);
        btn_C = new JButton("C");
        btn_C.addActionListener(this);
        btn_Add = new JButton("+");
        btn_Add.addActionListener(this);
        btn_Sub = new JButton("-");
        btn_Sub.addActionListener(this);
        btn_Mul = new JButton("x");
        btn_Mul.addActionListener(this);
        btn_Div = new JButton("/");
        btn_Div.addActionListener(this);
        btn_E = new JButton("=");
        btn_E.addActionListener(this);
        frame.setLayout(null);
        frame.setSize(300,400);
        frame.setTitle("GUI_Calculator");
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.add(btn_C).setBounds(90, 50, 40, 40);
        frame.add(btn_E).setBounds(130, 50, 40, 40);
        frame.add(btn_0).setBounds(50, 50, 40, 40);
        frame.add(btn_1).setBounds(50, 90, 40, 40);
        frame.add(btn_2).setBounds(90, 90, 40, 40);
        frame.add(btn_3).setBounds(130, 90, 40, 40);
        frame.add(btn_4).setBounds(50, 130, 40, 40);
        frame.add(btn_5).setBounds(90, 130, 40, 40);
        frame.add(btn_6).setBounds(130,130, 40, 40);
        frame.add(btn_7).setBounds(50, 170, 40, 40);
        frame.add(btn_8).setBounds(90, 170, 40, 40);
        frame.add(btn_9).setBounds(130, 170, 40, 40);
        frame.add(btn_Add).setBounds(170, 170, 40, 40);
        frame.add(btn_Sub).setBounds(170, 130, 40, 40);
        frame.add(btn_Mul).setBounds(170, 90, 40, 40);
        frame.add(btn_Div).setBounds(170, 50, 40, 40);

        frame.add(area).setBounds(50, 20, 160, 25);


        frame.setVisible(true);
    }

    public static void main(String[] args) {

        new Compute();
    }


    @Override
    public void actionPerformed(ActionEvent e) {


        if(e.getSource() == btn_0)
        {
            area.append("0");
        }
        if(e.getSource() == btn_C)
        {
            area.setText(null);
        }
        if(e.getSource() == btn_1)
        {
            area.append("1");
        }
        if(e.getSource() == btn_2)
        {
            area.append("2");
        }
        if(e.getSource() == btn_3)
        {
            area.append("3");
        }
        if(e.getSource() == btn_4)
        {
            area.append("4");
        }
        if(e.getSource() == btn_5)
        {
            area.append("5");
        }
        if(e.getSource() == btn_6)
        {
            area.append("6");
        }
        if(e.getSource() == btn_7)
        {
            area.append("7");
        }
        if(e.getSource() == btn_8)
        {
            area.append("8");
        }
        if(e.getSource() == btn_9)
        {
            area.append("9");
        }
        if(e.getSource() == btn_E)
        {
        }


    }






}
kyle lee
  • 23
  • 8
  • 2
    Please, for God's sake, use an array instead of declaring a ton of different button variables. – ajb Jan 06 '15 at 23:49
  • 1
    I'm not clear on what the problem is. You're already trying to have your listener compare the button to all the different variables, one by one, which is really clunky. But does it work? Is the problem that it doesn't work and you need help figuring out why? Or is the problem that it works but you want a better way to write it? – ajb Jan 06 '15 at 23:50
  • yup agree with the messy button codes.. it's a learning curve i'm totally noob..and the problem is I get the buttons showing up but when it's pressed I wanted to make it gets the values as I go and store the values to somewhere so that I can apply the formulas – kyle lee Jan 06 '15 at 23:57
  • Avoid using `null` layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify – MadProgrammer Jan 07 '15 at 00:09
  • See also this [calculator example](http://stackoverflow.com/a/7441804/418556). It uses `ScriptEngine` to evaluate the expression in the text area. – Andrew Thompson Jan 07 '15 at 02:56
  • `if(e.getSource() == btn_C)` (and the rest after that) should be `else if(e.getSource() == btn_C)`.. – Andrew Thompson Jan 07 '15 at 04:11
  • Better still, instead of `if(e.getSource() == btn_0) { area.append("0"); //.. etcetera` use `area.append(e.getActionCommand());` .. – Andrew Thompson Jan 07 '15 at 04:19

1 Answers1

1

Since you are a Java beginner, here are some tips that will make your life easier on the long run

  • Initialize your buttons via a private function:

    Private void initializeButtons(){
        //Initialize your buttons...
    }
    

    Call that function from your constructor.

  • Do the same for setting the listeneres

  • In your actionPerformed function, make all other if-cases except for the first one to be else if.. it will safe much more computation time since you don't force your program to run through all if-cases even though it might find the first case to be plausible.

Back to your question.. simply call:

String text = theButton.getText();

Then, take that text and make computations on it by parsing it as an "int".

<=>

int tempNum = Integer.parseInt(text);

Make the necessary calculations and save the result wherever you want.

Good luck!

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Ahmed Elmir
  • 163
  • 6