package vn.viettuts.awt;
import java.awt.Button;
import java.awt.Frame;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TextFieldExample2 extends Frame implements ActionListener {
TextField textField1, textField2, textField3;
Button button1, button2;
TextFieldExample2() {
textField1 = new TextField();
textField1.setBounds( 50 , 50 , 150 , 20 );
textField2 = new TextField();
textField2.setBounds( 50 , 100 , 150 , 20 );
textField3 = new TextField();
textField3.setBounds( 50 , 150 , 150 , 20 );
textField3.setEditable( false );
button1 = new Button( "+" );
button1.setBounds( 50 , 200 , 50 , 50 );
button2 = new Button( "-" );
button2.setBounds( 120 , 200 , 50 , 50 );
button1.addActionListener( this );
button2.addActionListener( this );
add(textField1);
add(textField2);
add(textField3);
add(button1);
add(button2);
setSize( 300 , 300 );
setLayout( null );
setVisible( true );
}
public void actionPerformed(ActionEvent e) {
String s1 = textField1.getText();
String s2 = textField2.getText();
int a = Integer.parseInt(s1);
int b = Integer.parseInt(s2);
int c = 0 ;
if (e.getSource() == button1) {
c = a + b;
} else if (e.getSource() == button2) {
c = a - b;
}
String result = String.valueOf(c);
textField3.setText(result);
}
public static void main(String[] args) {
new TextFieldExample2();
}
}
|