import java.awt.Button; import java.awt.TextField; import java.awt.Label; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.applet.Applet; public class sqrt_c extends Applet { // Inre klass för att lyssna på knapp private class sqrt_btn_listener_c implements ActionListener { public void actionPerformed( ActionEvent aev ) { handle_sqrt_btn(); } } // Hantera klick på roten-ur-knapp private void handle_sqrt_btn() { String numbuf = this.num_edt.getText(); boolean okay = false; float num = 0; try { num = new Float( numbuf ).floatValue(); // !!! Float.parseFloat( numbuf ); gick inte i IE !!! num = (float)Math.sqrt( num ); okay = !Float.isNaN( num ); } catch ( Exception exc ) { } if ( okay ) { this.sqrt_lbl.setText( "Roten ur " + numbuf + " = " + Float.toString( num ) ); } else { this.sqrt_lbl.setText( "Det måste vara ett positivt tal." ); } } // handle_sqrt_btn public void init() { super.init(); super.setLayout( null ); this.num_edt = new TextField( "SKRIV TAL HÄR" ); super.add( this.num_edt ); this.num_edt.setBounds( 20, 20, 120, 20 ); this.sqrt_lbl = new Label( "HÄR VISAS ROTEN" ); super.add( this.sqrt_lbl ); this.sqrt_lbl.setBounds( 20, 50, 200, 20 ); Button sqrt_btn; sqrt_btn_listener_c sqrt_btn_listener = new sqrt_btn_listener_c(); sqrt_btn = new Button( "VISA ROT" ); sqrt_btn.addActionListener( sqrt_btn_listener ); super.add( sqrt_btn ); sqrt_btn.setBounds( 150, 20, 100, 20 ); } // init private Label sqrt_lbl; private TextField num_edt; } // sqrt_c