3 Replies - 26609 Views - Last Post: 10 March 2008 - 09:58 AM Rate Topic: -----

#1 hallizh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 09-March 08

Creating a GUI calculator in Java

Posted 09 March 2008 - 07:20 PM

I have a school assignment to create a GUI calculator. Im having a slight problem how to refresh the label each time a button is clicked.
Also im pretty sure ill run into troubles on how i can make the calculator understand Operator Precedence.
I know i could have made an num[i] array to create the number buttons and assign a listener etc, but i didnt at that time, so .. :rolleyes:

English is not my native language but im doing my best :P


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

public class Reiknari extends JPanel
	{

		JButton t1, t2, t3, t4, t5, t6, t7,t8,t9,t0;
		JButton plus, minus, margf, deiling, plusminus, komma, hreinsa, samasem;
		JTextField slattur;
		JLabel empty;

	public Reiknari()
	{

		ButtonListener listener = new ButtonListener();

		t1 = new JButton ("1");
		t1.addActionListener(listener);
		t2 = new JButton ("2");
		t2.addActionListener(listener);
		t3 = new JButton ("3");
		t3.addActionListener(listener);
		t4 = new JButton ("4");
		t4.addActionListener(listener);
		t5 = new JButton ("5");
		t5.addActionListener(listener);
		t6 = new JButton ("6");
		t6.addActionListener(listener);
		t7 = new JButton ("7");
		t7.addActionListener(listener);
		t8 = new JButton ("8");
		t8.addActionListener(listener);
		t9 = new JButton ("9");
		t9.addActionListener(listener);
		t0 = new JButton ("0");
		t0.addActionListener(listener);
		plus = new JButton ("+");
		plus.addActionListener(listener);
		minus = new JButton ("-");
		minus.addActionListener(listener);
		margf = new JButton ("*");
		margf.addActionListener(listener);
		deiling = new JButton ("/");
		deiling.addActionListener(listener);
		hreinsa = new JButton ("C");
		hreinsa.addActionListener(listener);
		samasem = new JButton ("=");
		samasem.addActionListener(listener);
		slattur = new JTextField ("");
		empty = new JLabel ("");

		plusminus = new JButton ("+/-");
		plusminus.addActionListener(listener);
		komma = new JButton (",");
		komma.addActionListener(listener);


		// Plus, minus, devide, multiply
		JPanel skipanir = new JPanel();
		skipanir.setLayout (new GridLayout(5, 1));
		skipanir.add(plus);
		skipanir.add(minus);
		skipanir.add(margf);
		skipanir.add(deiling);
		skipanir.add(samasem);

		// numbers, comma, clear and an empty space.
		JPanel tolur = new JPanel();
		tolur.setLayout (new GridLayout(5, 3));
		tolur.add(t7);
		tolur.add(t8);
		tolur.add(t9);
		tolur.add(t4);
		tolur.add(t5);
		tolur.add(t6);
		tolur.add(t1);
		tolur.add(t2);
		tolur.add(t3);
		tolur.add(t0);
		tolur.add(plusminus);
		tolur.add(komma);
		tolur.add(empty);
		tolur.add(hreinsa);

		JPanel look = new JPanel();

		look.setLayout (new BorderLayout());

		look.add(tolur, BorderLayout.CENTER);
		look.add(skipanir, BorderLayout.EAST);
		look.add(slattur, BorderLayout.NORTH);


		setPreferredSize (new Dimension(400, 400));
		setBackground(Color.green);
		add (look);
	}


	public class ButtonListener implements ActionListener
	{
		public void actionPerformed (ActionEvent event)
		{
			// Here's mostly my problem ... was trying to use switch sentence, didnt work.
			// I know this line down here is nowhere close i guess.
			slattur.setText(event.getString());
		}
	}

}



Thanks in Advance! =)

------------
EDIT:
I know this is possible with tons of if statements, but im also sure there is a much better way. A smarter solution ;)

This post has been edited by hallizh: 10 March 2008 - 08:27 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Creating a GUI calculator in Java

#2 hallizh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 09-March 08

Re: Creating a GUI calculator in Java

Posted 10 March 2008 - 09:11 AM

I don't need code for this, just for someone to point me in the right direction, i can handle the coding myself...
(maybe this is easy, i might just be mindblocked)
If someone could help, it would be greatly appreciated.

Thanks :)

This post has been edited by hallizh: 10 March 2008 - 09:12 AM

Was This Post Helpful? 0
  • +
  • -

#3 GWatt  Icon User is online

  • member icon

Reputation: 278
  • View blog
  • Posts: 3,079
  • Joined: 01-December 05

Re: Creating a GUI calculator in Java

Posted 10 March 2008 - 09:42 AM

Is operator precedence required in your assignment? If it isn't, I would leave that alone until everything else worked.

@ line 107, you need to append the button number to the textfield, not simply set the button text.
Was This Post Helpful? 0
  • +
  • -

#4 hallizh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 09-March 08

Re: Creating a GUI calculator in Java

Posted 10 March 2008 - 09:58 AM

Operator precedence is not required, however, thats a "bonus". The comma is also a "bonus" as he stated.

Quote

@ line 107, you need to append the button number to the textfield, not simply set the button text.


    public class ButtonListener implements ActionListener  
    {  
        public void actionPerformed (ActionEvent event)  
        {  
            // Here's mostly my problem ... was trying to use switch sentence, didnt work.  
            // I know this line down here is nowhere close i guess.  
            slattur.setText(event.getString());  
        }  
    }  



Im the end, i handled it like this:

public class ButtonListener implements ActionListener
	{
		String tilStadar;
		public void actionPerformed (ActionEvent event)
		{
			if (event.getSource() == t1){
				tilStadar = slattur.getText();
				slattur.setText(tilStadar + "1"); }
			else if (event.getSource() == t2){
				tilStadar = slattur.getText();
				slattur.setText(tilStadar + "2"); }
			else if (event.getSource() == t3){
				tilStadar = slattur.getText();
				slattur.setText(tilStadar + "3"); }
			else if (event.getSource() == t4){
				tilStadar = slattur.getText();
				slattur.setText(tilStadar + "4"); }
			else if (event.getSource() == t5){
				tilStadar = slattur.getText();
				slattur.setText(tilStadar + "5"); }
			else if (event.getSource() == t6){
				tilStadar = slattur.getText();
				slattur.setText(tilStadar + "6"); }
			else if (event.getSource() == t7){
				tilStadar = slattur.getText();
				slattur.setText(tilStadar + "7"); }
			else if (event.getSource() == t8){
				tilStadar = slattur.getText();
				slattur.setText(tilStadar + "8"); }
			else if (event.getSource() == t9){
				tilStadar = slattur.getText();
				slattur.setText(tilStadar + "9"); }
			else if (event.getSource() == t0){
				tilStadar = slattur.getText();
				slattur.setText(tilStadar + "0"); }
		}
	}


Not exactly as "pretty" as i intended, but it works.

Thanks for your help. And i will indeed leave the precedence part alone untill everything else is working as intended ;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1