Results 1 to 4 of 4
  1. #1
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Default Stack Calculator

    Hey Guys just beginning to learn Gui.I have to create a stack calculator, I have wrote the code for all the format and its fine but i need to know how to make the "Buttons" to work.ps i dont really know how a stack cal works..
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.math.*;
    
    public class Calc extends JApplet
     {
     private JTextField num1Field;
     private JTextField num2Field;
     private JTextField num3Field;
     private JTextField num4Field;
     private JTextField num5Field;
     private JButton enterButton;
     private JButton plusButton;
     private JButton minusButton;
     private JButton multiButton;
     private JButton divisButton;
     
     public void init()
      {
      	Container c = getContentPane();
    	c.setLayout(new GridLayout (5, 2));
    	
    	num1Field = new JTextField();
    	c.add(num1Field);
    	enterButton = new JButton("Enter");
    	c.add(enterButton);
    	
    
    	
    	num2Field = new JTextField();
    	c.add(num2Field);
    	plusButton = new JButton("+");
    	c.add(plusButton);
    	
    	num3Field = new JTextField();
    	c.add(num3Field);
    	minusButton = new JButton("-");
    	c.add(minusButton);
    	
    	num4Field = new JTextField();
    	c.add(num4Field);
    	multiButton = new JButton("*");
    	c.add(multiButton);
    	
    	num5Field = new JTextField();
    	c.add(num5Field);
    	divisButton = new JButton("/");
    	c.add(divisButton);
    	
    	}
    	public static void main(String args[])
    	{
    		JFrame app = new JFrame("Calc");
    		JApplet applet = new Calc();
    		applet.init();
    		app.setContentPane(applet.getContentPane());
    		app.setSize(240, 100);
    		app.setVisible(true);
    	 }
    	 }
    
    
    	
    	// ti.setText(t0.getText()); //copy t0 to t1)
    	// to do + 
    	//getFloat from t0 and t1 . add the numbers
    	//setFloat into t0,

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    how to make the "Buttons" to work
    Add ActionListeners to them.

  3. #3
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Default

    UPDATED CODE WITH ACTION LISTENERS INSERTED ..DOESN'T SEEM TO CALCULATE PROPERLY ..
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.math.*;
    
    public class Calc extends JApplet  {
     private JTextField num1field, num2field,num3field, num4field,num5field;
     private JButton enterButton,plusButton,minusButton, multiButton,divisButton;
     
     
     // *******************BUILD GUI ENVIRO*****
     public void init()
      {
      	Container c = getContentPane();
    	c.setLayout(new GridLayout (5, 2));
    	
    	num1field = new JTextField();
    	c.add(num1field);
    	// **************CREATE ENTER BUTTON******
    	enterButton = new JButton("Enter");
    	c.add(enterButton);
    	enterButton.addActionListener(new ActionListener(){
    		public void actionPerformed(ActionEvent e){
    	num5field.setText((num4field.getText() == null) ? "" : num4field.getText());
    	num4field.setText((num3field.getText() == null) ? "" : num3field.getText());
    	num3field.setText((num2field.getText() == null) ? "" : num2field.getText());
    	num2field.setText((num1field.getText() == null) ? "" : num1field.getText());
    	num1field.setText((num3field.getText() == null) ? "" : num3field.getText());
    }
    });
    	num2field = new JTextField();
    	c.add(num2field);
    	//******************CREATES PLUS BUTTON *****
    	
    	plusButton = new JButton("+");
    	c.add(plusButton);
    	plusButton.addActionListener(new ActionListener() {
    	public void actionPerformed(ActionEvent e){
    	setFloat(num1field, getFloat(num2field)+ getFloat(num1field));
    	setPopStack();
    	}
    	});
    	num3field = new JTextField();
    	c.add(num3field);
    	//******************Creates MINUS BUTTON*******
    	minusButton = new JButton("-");
    	c.add(minusButton);
    	minusButton.addActionListener(new ActionListener() {
    	public void actionPerformed(ActionEvent e) {
    	setFloat(num1field, getFloat(num2field)- getFloat(num1field));
    	setPopStack();
    	}
    	});
    	num4field = new JTextField();
    	c.add(num4field);
    	//******************* Creates Multi Button**********
    	multiButton = new JButton("*");
    	c.add(multiButton);
    	multiButton.addActionListener(new ActionListener() {
    	public void actionPerformed(ActionEvent e){
    	setFloat(num1field, getFloat(num2field)* getFloat(num1field));
    	setPopStack();
    	}
    	});
    	num5field = new JTextField();
    	c.add(num5field);
    	//**************** Creates Divis Button
    	divisButton = new JButton("/");
    	c.add(divisButton);
    	divisButton.addActionListener(new ActionListener() {
    	public void actionPerformed(ActionEvent e) {
    	setFloat(num1field, getFloat(num2field)/ getFloat(num1field));
    	setPopStack();
    	}
    	});
    
    	}// end init()
    	
    	//******************DO COMPUTATION************
    	
    	
    	// RETURNS A FLOAT FROM A TEXT FIELD
    	
    	public float getFloat(JTextField t)
    	{
    	return Float.parseFloat(t.getText());
    	}
    	// SET TEXT FIELD TO FLOATING POINT NUMBER
    	
    	public void setFloat(JTextField t, float p)
    	{
    	t.setText((new Float(p)).toString());
    	}
    	
      //*******POPS STACK UP***********
      public void setPopStack(){
    num1field.setText((num2field.getText() == null || num2field.getText().equals("")) ? "" : num2field.getText());
    num2field.setText((num3field.getText() == null || num3field.getText().equals("")) ? "" : num3field.getText());
    num3field.setText((num4field.getText() == null || num4field.getText().equals("")) ? "" : num4field.getText());
    num4field.setText(null);
    }
    
    
    
    
    	public static void main(String args[])
    	{
    		JFrame app = new JFrame("Bilals Calculator");
    		JApplet applet = new Calc();
    		applet.init();
    		app.setContentPane(applet.getContentPane());
    		app.setSize(240, 100);
    		app.setVisible(true);
    	 }
    	 }
    
    
    	
    	// ti.setText(t0.getText()); //copy t0 to t1)
    	// to do + 
    	//getFloat from t0 and t1 . add the numbers
    	//setFloat into t0,

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Bgreen7887 View Post
    UPDATED CODE WITH ACTION LISTENERS INSERTED ..DOESN'T SEEM TO CALCULATE PROPERLY ..
    Please read the link in my signature below on how to ask smart questions. "doesn't seem to calculate properly" is not an answerable question, nor contains any useful information that would help others help you.

Similar Threads

  1. Calculator
    By water in forum AWT / Swing
    Replies: 4
    Last Post: 09-23-2009, 06:00 AM
  2. help with calculator
    By kalibballer in forum New To Java
    Replies: 8
    Last Post: 04-01-2009, 12:57 PM
  3. Problem in Calculator implementation using Stack
    By realahmed8 in forum New To Java
    Replies: 1
    Last Post: 12-19-2008, 11:58 PM
  4. Calculator help.
    By madkidd02 in forum New To Java
    Replies: 2
    Last Post: 10-25-2008, 07:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •