Welcome to Dream.In.Code
Become a Java Expert!

Join 150,050 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,717 people online right now. Registration is fast and FREE... Join Now!




Java Calculator questions

 
Reply to this topicStart new topic

Java Calculator questions, more Gui stuff

jakkels
7 Jun, 2008 - 03:12 AM
Post #1

New D.I.C Head
*

Joined: 21 May, 2008
Posts: 21


My Contributions
1st: I'm trying to restrict my Textfiled to 8 integers.
2nd: I want split the operation and keys with some sort of horizontal line.

See what I have tried below with no luck.

CODE


//VERSION 0.2 with Engine 0.2
import java.awt.*;
import java.awt.event.*;

//=============
// Calculator
//=============

//Declaring that we are interested in using the action listener
//Extends frame - inherits
public class Calculator extends Frame implements ActionListener, WindowListener
{
    //--------
    // member
    //--------
    private TextField display;
    private TextField status;
    private Engine engine = new Engine();

    public static void main( String[] args )
    {
        Frame f = new Calculator();
        f.setSize( 200, 300 );
        f.setLocation( 300, 150 );
        f.setTitle( "Calculator" );
        f.show();
    }
    //-------------
    // constructor
    //-------------
    public Calculator()
    {
        addWindowListener( this );
      
        display = new TextField( "0", 8 ); //THIS IS NOT WORKING
        display.setEditable( false );
        add( display, "North" );
              
      //New panel specifying grid for all the numbers and functions
        Panel p = new Panel();
      p.setLayout( new GridLayout( 5, 4, 5, 4 ) );
      //p.setHgap ( 1, 1 ); THIS IS NOT WORKING

      addButton( p, "" );      
      addButton( p, "" );
      addButton( p, "" );  
      addButton( p, "CE" );
      addButton( p, "CA" );
      addButton ( p, "7" );
      addButton ( p, "8" );
      addButton ( p, "9" );
      addButton ( p, "/" );
      addButton( p, "M+" );
      addButton ( p, "4" );
      addButton ( p, "5" );
      addButton ( p, "6" );
      addButton ( p, "*" );
      addButton ( p, "MC" );
      addButton ( p, "1" );      
      addButton ( p, "2" );
      addButton ( p, "3" );
      addButton( p, "-" );
      addButton( p, "MRC" );
      addButton ( p, "0" );
      addButton ( p, "%" );
        addButton( p, "." );
        addButton( p, "+" );
        addButton( p, "=" );

        add( p, "Center" );
      
      //this is to add a textfield panel at the bottom
        status = new TextField();
        status.setEditable( false );
        status.setText("Welcome to my calculator");
        add( status, "South" );
   }

    public void addButton( Container c, String s )
    {
        Button b = new Button( s );
        c.add( b );
        b.addActionListener( this );
    }

    public void actionPerformed( ActionEvent evt )
       //Handling graphic user interface events - will be adding more here
      {
        String s = evt.getActionCommand();
        int result;

        if ( '0' <= s.charAt( 0 ) && s.charAt( 0 ) <= '9' )
        {
        
      if ( engine.started() )
                display.setText( s );
            else
                display.setText( display.getText() + s );

            engine.stop();
        }
        else
        {
            result = engine.calculate( Integer.parseInt( display.getText() ) );
            display.setText( "" + result );
            engine.operation( s );
            engine.start();
        }
    }

    public void windowActivated(WindowEvent e)
    {
        return;
    }

    public void windowClosed(WindowEvent e)
    {
        return;
    }

    //Close the window using windows listener to close the calculator
   public void windowClosing(WindowEvent e)
    {
        dispose();
    }

    public void windowDeactivated(WindowEvent e)
    {
        return;
    }

    public void windowDeiconified(WindowEvent e)
    {
        return;
    }

    public void windowIconified(WindowEvent e)
    {
        return;
    }

    public void windowOpened(WindowEvent e)
    {
        return;
    }
}


User is offlineProfile CardPM
+Quote Post

herefishyfishy
RE: Java Calculator Questions
7 Jun, 2008 - 04:58 AM
Post #2

D.I.C Head
Group Icon

Joined: 1 May, 2008
Posts: 60


Dream Kudos: 100
My Contributions
When you use the constructor to set the maximum length to 8, that means that when the USER edits it, it cannot go beyond 8. The text field is not editable, though, and you are using setText instead of allowing user input. The length is not limited. Write an if statement that will check whether the length is already 8, and then if it is, don't add another digit.

This post has been edited by herefishyfishy: 7 Jun, 2008 - 05:00 AM
User is offlineProfile CardPM
+Quote Post

jakkels
RE: Java Calculator Questions
7 Jun, 2008 - 06:44 AM
Post #3

New D.I.C Head
*

Joined: 21 May, 2008
Posts: 21


My Contributions
Are you looking at the first TextField commented //NOT WORKING? Sorry there are two.

My aim is to limit the digits which may be entered onto the calculator display (textfield) to 8 maximum.

Is this where you suggest writing an IF statement?

QUOTE(herefishyfishy @ 7 Jun, 2008 - 05:58 AM) *

When you use the constructor to set the maximum length to 8, that means that when the USER edits it, it cannot go beyond 8. The text field is not editable, though, and you are using setText instead of allowing user input. The length is not limited. Write an if statement that will check whether the length is already 8, and then if it is, don't add another digit.


User is offlineProfile CardPM
+Quote Post

mensahero
RE: Java Calculator Questions
7 Jun, 2008 - 06:52 AM
Post #4

c0mput3rz Are Only Human
Group Icon

Joined: 26 May, 2008
Posts: 664



Thanked: 17 times
Dream Kudos: 75
My Contributions

Limit Text in JTextField Snippet

Hope thats the one your looking for.. well thats a good example.. blink.gif
User is offlineProfile CardPM
+Quote Post

pbl
RE: Java Calculator Questions
7 Jun, 2008 - 09:44 AM
Post #5

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
It is not an editable JTextField so that will suffice

CODE

     if ( engine.started() )
                display.setText( s );
            else {
                String str = display.getText();
                if(str.length() < 8)
                     display.setText( str + s );
            }

            engine.stop();
        }

User is online!Profile CardPM
+Quote Post

mensahero
RE: Java Calculator Questions
7 Jun, 2008 - 09:54 AM
Post #6

c0mput3rz Are Only Human
Group Icon

Joined: 26 May, 2008
Posts: 664



Thanked: 17 times
Dream Kudos: 75
My Contributions
QUOTE(pbl @ 7 Jun, 2008 - 10:44 AM) *

It is not an editable JTextField so that will suffice

CODE

     if ( engine.started() )
                display.setText( s );
            else {
                String str = display.getText();
                if(str.length() < 8)
                     display.setText( str + s );
            }

            engine.stop();
        }



Hmm.. why would someone use a JTextField that is not for user input.. why not use JLabel instead.. maybe its the border or something.. probably.. blink.gif blink.gif
User is offlineProfile CardPM
+Quote Post

pbl
RE: Java Calculator Questions
7 Jun, 2008 - 10:06 AM
Post #7

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
QUOTE(mensahero @ 7 Jun, 2008 - 10:54 AM) *

Hmm.. why would someone use a JTextField that is not for user input.. why not use JLabel instead.. maybe its the border or something.. probably.. blink.gif blink.gif


Good question Mensa. Thanks for the thanks it is enough icon_up.gif

User is online!Profile CardPM
+Quote Post

herefishyfishy
RE: Java Calculator Questions
7 Jun, 2008 - 01:58 PM
Post #8

D.I.C Head
Group Icon

Joined: 1 May, 2008
Posts: 60


Dream Kudos: 100
My Contributions
It's not even a JTextField at all. It's a java.awt.TextField.
User is offlineProfile CardPM
+Quote Post

jakkels
RE: Java Calculator Questions
12 Jun, 2008 - 12:04 PM
Post #9

New D.I.C Head
*

Joined: 21 May, 2008
Posts: 21


My Contributions
Actually I didn't quite get this working yet.

I'll give you guys another shot. tongue.gif If you don't mind.

See below: //To try and limit the length of the textfield to 8
(It's the 'Panel', i think) - need to limit the digits on the calculator panel

CODE


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


//=============
// Calculator
//=============

//Declaring that we are interested in using the action listener
//Extends frame - inherits
public class Calculator extends Frame implements ActionListener, WindowListener
{
    private TextField display;
    private TextField status;
    private Engine engine = new Engine();

    public static void main( String[] args )
    {
        Frame f = new Calculator();
        f.setSize( 200, 300 );
        f.setLocation( 300, 150 );
        f.setTitle( "Calculator" );
        f.show();
    }
  
    public Calculator()
    {
        addWindowListener( this );
      
      // Adding the display for the calculator
      display = new TextField( "" );
        display.setEditable( false );    
      add( display, "North" );
              
      //New panel specifying grid for all the numbers and functions
        Panel p = new Panel();
      p.setLayout( new GridLayout( 5, 4, 5, 4 ) );
      //p.setVgap ( 1, 1 ); THIS IS NOT WORKING

      //Adding the buttons
      p.add(new JLabel(""));
      p.add(new JLabel(""));
      p.add(new JLabel(""));
      addButton( p, "CE" );
      addButton( p, "CA" );
      addButton ( p, "7" );
      addButton ( p, "8" );
      addButton ( p, "9" );
      addButton ( p, "/" );
      addButton( p, "M+" );
      addButton ( p, "4" );
      addButton ( p, "5" );
      addButton ( p, "6" );
      addButton ( p, "*" );
      addButton ( p, "MC" );
      addButton ( p, "1" );      
      addButton ( p, "2" );
      addButton ( p, "3" );
        addButton( p, "-" );
      addButton( p, "MRC" );
      addButton ( p, "0" );
      addButton ( p, "%" );
        addButton( p, "." );
        addButton( p, "+" );
        addButton( p, "=" );

        add( p, "Center" );
      
      //this is to add a textfield panel at the bottom
        status = new TextField();
        status.setEditable( false );
        status.setText("Welcome to my calculator");
        add( status, "South" );
   }

    public void addButton( Container c, String s )
    {
        Button b = new Button( s );
        c.add( b );
        b.addActionListener( this );
    }

    public void actionPerformed( ActionEvent evt )
       //Handling graphic user interface events
      {
        String s = evt.getActionCommand();
        int result;

//To try and limit the length of the textfield to 8      
//      if ( engine.started() )
//      display.setText( s );
//    else {
//          String str = display.getText();
//          if(str.length() < 8)
//                 display.setText( str + s );
//          }
//        engine.stop();
//        }

           if ( '0' <= s.charAt( 0 ) && s.charAt( 0 ) <= '9' )
           {
           
         if ( engine.started() )
                   display.setText( s );
               else
                   display.setText( display.getText() + s );
               engine.stop();
           }
        
         else
           {
               result = engine.calculate( Integer.parseInt( display.getText() ) );
               display.setText( "" + result );
               engine.operation( s );
               engine.start();
           }
        
         if (s == " CA ")
         {
            display.setText( "0" );
            }
           
         //else if
         //{
         //(s == " M+ ")
         //{
         //  
         //}
         //
         //}
      }  

    public void windowActivated(WindowEvent e)
    {
        return;
    }

    public void windowClosed(WindowEvent e)
    {
        return;
    }

    //Close the window using windows listener to close the calculator
   public void windowClosing(WindowEvent e)
    {
        dispose();
    }

    public void windowDeactivated(WindowEvent e)
    {
        return;
    }

    public void windowDeiconified(WindowEvent e)
    {
        return;
    }

    public void windowIconified(WindowEvent e)
    {
        return;
    }

    public void windowOpened(WindowEvent e)
    {
        return;
    }
}




Engine class too if you want to compile.

CODE



public class Engine
{
    private int acc = 0;
    private String op = "=";
    public boolean start = true;

    public Engine()
    {
    }

    public void start()
    {
        start = true;
    }

    public void stop()
    {
        start = false;
    }

    public boolean started()
    {
        return start;
    }

    public void operation( String op )
    {
        this.op = op;
    }

    public int calculate( int n )
    {
        if ( op.equals( "+" ) )
            acc += n;
        else if ( op.equals( "-" ) )
            acc -= n;
        else if ( op.equals( "*" ) )
            acc *= n;
        else if ( op.equals( "/" ) )
            acc /= n;
        else if ( op.equals( "%" ) )
            acc %= n;
        else if ( op.equals( "=" ) )
            acc = n;
      else if ( op.equals( "CE" ) )
         acc = 0;
      else if ( op.equals( "CA" ) )
          acc = 0;
      //else if ( op.equals( "M+" ) )
      //   acc = n;
      //else if ( op.equals( "MC" ) )
      //   acc = n;
      //else if ( op.equals( "MRC" ) )
      //   acc = n;

        return acc;
    }
}



This post has been edited by jakkels: 12 Jun, 2008 - 12:08 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 10:13PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month