// SumMIDlet.java (c) Kari Laitinen // http://www.naturalprogramming.com // 2003-11-?? File created. // 2007-09-02 Last modification. // This program can calculate the sum of two integers which the user // inputs. TextField objects are used to receive data from the user. // The result of the calculation, i.e., the sum of the two integers, // is shown in an uneditable TextField. Another possibility to show the // result would be to use a StringItem object. However, it seems that // with an uneditable TextField it is easier to create a "nice" user // interface. // An old, equivalent version of this program is at the end of this file. import javax.microedition.lcdui.*; import javax.microedition.midlet.*; public class SumMIDlet extends MIDlet implements CommandListener, ItemStateListener { Display display_of_this_midlet = Display.getDisplay( this ) ; TextField first_integer_text_field = new TextField( "First integer: ", "", 8, TextField.NUMERIC ) ; TextField second_integer_text_field = new TextField( "Second integer:", "", 8, TextField.NUMERIC ) ; TextField result_text_field = new TextField( "Calculated sum:", "0", 8, TextField.NUMERIC | TextField.UNEDITABLE ) ; Form form_of_this_midlet = new Form( "SumMIDlet" ) ; Command exit_command = new Command( "EXIT", Command.EXIT, 1 ) ; public SumMIDlet() { first_integer_text_field.setLayout( Item.LAYOUT_CENTER ) ; form_of_this_midlet.append( first_integer_text_field ) ; form_of_this_midlet.append( second_integer_text_field ) ; form_of_this_midlet.append( result_text_field ) ; form_of_this_midlet.setItemStateListener( this ) ; form_of_this_midlet.addCommand( exit_command ) ; form_of_this_midlet.setCommandListener( this ) ; } protected void startApp() throws MIDletStateChangeException { display_of_this_midlet.setCurrent( form_of_this_midlet ) ; } protected void pauseApp() { } protected void destroyApp( boolean unconditional_destruction_required ) { } public void commandAction( Command given_command, Displayable display_content ) { if ( given_command == exit_command ) { destroyApp( false ) ; notifyDestroyed() ; } } public void itemStateChanged( Item item_which_changed_state ) { String first_integer_text = first_integer_text_field.getString() ; String second_integer_text = second_integer_text_field.getString() ; if ( first_integer_text.length() == 0 ) { first_integer_text = "0" ; } if ( second_integer_text.length() == 0 ) { second_integer_text = "0" ; } int first_integer = Integer.parseInt( first_integer_text ) ; int second_integer = Integer.parseInt( second_integer_text ) ; int sum_of_two_integers = first_integer + second_integer ; String sum_text = "" + sum_of_two_integers ; result_text_field.setString( sum_text ) ; } } /* The following is the old version of this program. The old version used an immutable TextField to show the calculation result. import javax.microedition.lcdui.*; import javax.microedition.midlet.*; public class SumMIDlet extends MIDlet implements CommandListener, ItemStateListener { private Display display_of_this_midlet ; private TextField first_integer_text_field ; private TextField second_integer_text_field ; private TextField result_text_field ; private Form form_of_this_midlet = new Form( "SumMIDlet" ) ; private Command exit_command = new Command( "EXIT", Command.EXIT, 1 ) ; public SumMIDlet() { display_of_this_midlet = Display.getDisplay( this ) ; first_integer_text_field = new TextField( "First integer: ", "", 8, TextField.NUMERIC ) ; first_integer_text_field.setLayout( Item.LAYOUT_CENTER ) ; second_integer_text_field = new TextField( "Second integer:", "", 8, TextField.NUMERIC ) ; result_text_field = new TextField( "Calculated sum:", "0", 8, TextField.NUMERIC | TextField.UNEDITABLE ) ; form_of_this_midlet.append( first_integer_text_field ) ; form_of_this_midlet.append( second_integer_text_field ) ; form_of_this_midlet.append( result_text_field ) ; form_of_this_midlet.setItemStateListener( this ) ; form_of_this_midlet.addCommand( exit_command ) ; form_of_this_midlet.setCommandListener( this ) ; } protected void destroyApp( boolean conditional_destruction_disabled ) { } protected void pauseApp() { } protected void startApp() throws MIDletStateChangeException { display_of_this_midlet.setCurrent( form_of_this_midlet ) ; } public void commandAction( Command given_command, Displayable display_content ) { if ( given_command == exit_command ) { destroyApp( false ) ; notifyDestroyed() ; } } public void itemStateChanged( Item item_which_changed_state ) { String first_integer_text = first_integer_text_field.getString() ; String second_integer_text = second_integer_text_field.getString() ; if ( first_integer_text.length() == 0 ) { first_integer_text = "0" ; } if ( second_integer_text.length() == 0 ) { second_integer_text = "0" ; } int first_integer = Integer.parseInt( first_integer_text ) ; int second_integer = Integer.parseInt( second_integer_text ) ; int sum_of_two_integers = first_integer + second_integer ; String sum_text = "" + sum_of_two_integers ; result_text_field.setString( sum_text ) ; } } ******/