// SumApplet.java (c) 2005 Kari Laitinen // 2005-09-27 File created. // 2005-10-03 Latest modification. /* This applet demonstrates the following things: - an applet made with Swing classes JApplet, JPanel, JTextField, JLabel - DocumentListener interface - GridLayout - Scanner class - using null layout in a panel and placing a component into an absolute numerical position within a Panel object */ import java.awt.* ; import java.awt.event.* ; import javax.swing.* ; import javax.swing.event.* ; import java.util.Scanner ; class SumPanel extends JPanel implements DocumentListener { JTextField first_text_field, second_text_field ; JLabel result_label ; SumPanel() { setLayout( null ) ; Font font_for_numbers = new Font( "Courier", Font.BOLD, 20 ) ; first_text_field = new JTextField( "" ) ; first_text_field.setHorizontalAlignment( JTextField.RIGHT ) ; first_text_field.setFont( font_for_numbers ) ; first_text_field.getDocument().addDocumentListener( this ) ; second_text_field = new JTextField( "" ) ; second_text_field.setHorizontalAlignment( JTextField.RIGHT ) ; second_text_field.setFont( font_for_numbers ) ; second_text_field.getDocument().addDocumentListener( this ) ; result_label = new JLabel( "0", JLabel.RIGHT ) ; result_label.setFont( font_for_numbers ) ; JPanel calculation_panel = new JPanel( new GridLayout( 3, 2, 10, 10 ) ) ; // The following statement specifies that the position // of the panel is ( 100, 50 ), and its width is 300 pixels // and its height is 150 pixels. calculation_panel.setBounds( 100, 50, 300, 150 ) ; calculation_panel.add( new JLabel( "First integer:" ) ) ; calculation_panel.add( first_text_field ) ; calculation_panel.add( new JLabel( "Second integer:" ) ) ; calculation_panel.add( second_text_field ) ; calculation_panel.add( new JLabel( "Calculated sum:" ) ) ; calculation_panel.add( result_label ) ; add( calculation_panel ) ; } void calculate_sum() { // The texts from the text fields are converted to Scanner objects // from which it is easy to scan data. The Scanner class has // the hasNextInt() method which returns false if no valid // int value is present in the text being scanned. Scanner first_text_to_scan = new Scanner( first_text_field.getText() ) ; Scanner second_text_to_scan = new Scanner( second_text_field.getText()); // The following two if constructs ensure that empty text fields // are treated as if they contained a zero. if ( ! first_text_to_scan.hasNext() ) { first_text_to_scan = new Scanner( "0" ) ; } if ( ! second_text_to_scan.hasNext() ) { second_text_to_scan = new Scanner( "0" ) ; } if ( first_text_to_scan.hasNextInt() && second_text_to_scan.hasNextInt() ) { int first_integer = first_text_to_scan.nextInt() ; int second_integer = second_text_to_scan.nextInt() ; int sum_of_two_integers = first_integer + second_integer ; result_label.setText( "" + sum_of_two_integers ) ; } else { result_label.setText( "INPUT ERROR!" ) ; } } public void insertUpdate( DocumentEvent event ) { calculate_sum() ; } public void removeUpdate( DocumentEvent event ) { calculate_sum() ; } public void changedUpdate( DocumentEvent event ) { } // This kind of update is not possible in the case of JTextField } public class SumApplet extends JApplet { public void init() { getContentPane().add( new SumPanel() ) ; } }