极简计算器
import java. awt. *;
import java. awt. event. ActionEvent;
import java. awt. event. ActionListener;
import java. awt. event. WindowAdapter;
import java. awt. event. WindowEvent;
public class CalculatorDemo02 {
public static void main ( String[ ] args) {
new Calculator2 ( ) ;
}
}
class Calculator2 {
public Calculator2 ( ) {
TextField num1 = new TextField ( 10 ) ;
TextField num2 = new TextField ( 10 ) ;
TextField num3 = new TextField ( 20 ) ;
Button button = new Button ( "=" ) ;
Label label = new Label ( "+" ) ;
Frame frame = new Frame ( "Calculator" ) ;
frame. setLayout ( new FlowLayout ( ) ) ;
frame. add ( num1) ;
frame. add ( label) ;
frame. add ( num2) ;
frame. add ( button) ;
frame. add ( num3) ;
frame. setVisible ( true ) ;
button. addActionListener ( new MyCalculatorListen2 ( num1, num2, num3) ) ;
frame. addWindowListener ( new WindowAdapter ( ) {
@Override
public void windowClosing ( WindowEvent e) {
super . windowClosing ( e) ;
System. exit ( 0 ) ;
}
} ) ;
}
}
class MyCalculatorListen2 implements ActionListener {
private TextField num1;
private TextField num2;
private TextField num3;
int a;
public MyCalculatorListen2 ( TextField num1, TextField num2, TextField num3) {
this . num1 = num1;
this . num2 = num2;
this . num3 = num3;
}
@Override
public void actionPerformed ( ActionEvent e) {
a= Integer. parseInt ( num1. getText ( ) ) + Integer. parseInt ( num2. getText ( ) ) ;
num3. setText ( String. valueOf ( a) ) ;
num1. setText ( "" ) ;
num2. setText ( "" ) ;
}
}
import java. awt. *;
import java. awt. event. ActionEvent;
import java. awt. event. ActionListener;
import java. awt. event. WindowAdapter;
import java. awt. event. WindowEvent;
public class CalculatorDemo02 {
public static void main ( String[ ] args) {
new Calculator2 ( ) . loadFrame ( ) ;
}
}
class Calculator2 {
TextField num1;
TextField num2;
TextField num3;
public void loadFrame ( ) {
num1 = new TextField ( 10 ) ;
num2 = new TextField ( 10 ) ;
num3 = new TextField ( 20 ) ;
Button button = new Button ( "=" ) ;
Label label = new Label ( "+" ) ;
Frame frame = new Frame ( "Calculator" ) ;
frame. setLayout ( new FlowLayout ( ) ) ;
frame. add ( num1) ;
frame. add ( label) ;
frame. add ( num2) ;
frame. add ( button) ;
frame. add ( num3) ;
frame. setVisible ( true ) ;
frame. pack ( ) ;
button. addActionListener ( new MyCalculatorListen2 ( this ) ) ;
frame. addWindowListener ( new WindowAdapter ( ) {
@Override
public void windowClosing ( WindowEvent e) {
super . windowClosing ( e) ;
System. exit ( 0 ) ;
}
} ) ;
}
}
class MyCalculatorListen2 implements ActionListener {
Calculator2 calculator = null;
public MyCalculatorListen2 ( Calculator2 calculator) {
this . calculator = calculator;
}
@Override
public void actionPerformed ( ActionEvent e) {
int a= Integer. parseInt ( calculator. num1. getText ( ) ) + Integer. parseInt ( calculator. num2. getText ( ) ) ;
calculator. num3. setText ( String. valueOf ( a) ) ;
calculator. num1. setText ( "" ) ;
calculator. num2. setText ( "" ) ;
}
}
import java. awt. *;
import java. awt. event. ActionEvent;
import java. awt. event. ActionListener;
import java. awt. event. WindowAdapter;
import java. awt. event. WindowEvent;
public class CalculatorDemo02 {
public static void main ( String[ ] args) {
new Calculator2 ( ) . loadFrame ( ) ;
}
}
class Calculator2 {
TextField num1;
TextField num2;
TextField num3;
public void loadFrame ( ) {
num1 = new TextField ( 10 ) ;
num2 = new TextField ( 10 ) ;
num3 = new TextField ( 20 ) ;
Button button = new Button ( "=" ) ;
Label label = new Label ( "+" ) ;
Frame frame = new Frame ( "Calculator" ) ;
frame. setLayout ( new FlowLayout ( ) ) ;
frame. add ( num1) ;
frame. add ( label) ;
frame. add ( num2) ;
frame. add ( button) ;
frame. add ( num3) ;
frame. setVisible ( true ) ;
frame. pack ( ) ;
button. addActionListener ( new MyCalculatorListen2 ( ) ) ;
frame. addWindowListener ( new WindowAdapter ( ) {
@Override
public void windowClosing ( WindowEvent e) {
super . windowClosing ( e) ;
System. exit ( 0 ) ;
}
} ) ;
}
private class MyCalculatorListen2 implements ActionListener {
@Override
public void actionPerformed ( ActionEvent e) {
int a= Integer. parseInt ( num1. getText ( ) ) + Integer. parseInt ( num2. getText ( ) ) ;
num3. setText ( String. valueOf ( a) ) ;
num1. setText ( "" ) ;
num2. setText ( "" ) ;
}
}
}