kyle lee
kyle lee

Reputation: 23

Java Swing calculator

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)
        {
        }


    }






}

Upvotes: 0

Views: 2264

Answers (1)

Ahmed Elmir
Ahmed Elmir

Reputation: 163

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!

Upvotes: 1

Related Questions