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!
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
//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;
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
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.
//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
//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(); // }