import java.awt.*;
import java.awt.event.*;
public class TestInnerClass {
public static void main(String[] args) {
new TFFrame().launchFrame();
}
}
class TFFrame extends Frame {
public void launchFrame() {
TextField num1 = new TextField(10);
TextField num2 = new TextField(10);
TextField num3 = new TextField(15);
Label lbPlus = new Label("+");
Button btnEqual = new Button("=");
setLayout(new FlowLayout());
add(num1);
add(lbPlus);
add(num2);
add(btnEqual);
add(num3);
btnEqual.addActionListener(new MyMonitor(num1, num2, num3));
pack();
setVisible(true);
}
}
class MyMonitor implements ActionListener {
TextField num1,num2,num3;
MyMonitor(TextField num1, TextField num2, TextField num3) {//用构造函数实现
this.num1 = num1;
this.num2 = num2;
this.num3 = num3;
}
public void actionPerformed(ActionEvent e) {
int n1 = Integer.parseInt(num1.getText());
int n2 = Integer.parseInt(num2.getText());
int n3 = n1 + n2;
num3.setText("" + n3);
}
}
import java.awt.*;
import java.awt.event.*;
public class TestInnerClass {
public static void main(String[] args) {
new TFFrame().launchFrame();
}
}
class TFFrame extends Frame {
public void launchFrame() {
TextField num1 = new TextField(10);
TextField num2 = new TextField(10);
TextField num3 = new TextField(15);
Label lbPlus = new Label("+");
Button btnEqual = new Button("=");
setLayout(new FlowLayout());
add(num1);
add(lbPlus);
add(num2);
add(btnEqual);
add(num3);
btnEqual.addActionListener(new MyMonitor(this/*num1, num2, num3*/));
pack();
setVisible(true);
}
}
class MyMonitor implements ActionListener {
// TextField num1,num2,num3;
// MyMonitor(TextField num1, TextField num2, TextField num3) {
// this.num1 = num1;
// this.num2 = num2;
// this.num3 = num3;
// }
//持有对方的引用
TFFrame tf = null;
public MyMonitor(TFFrame tf) {
this.tf = tf;
}
public void actionPerformed(ActionEvent e) {
int n1 = Integer.parseInt(tf.num1.getText());
int n2 = Integer.parseInt(tf.num2.getText());
int n3 = n1 + n2;
tf.num3.setText("" + n3);
}
}
import java.awt.*;
import java.awt.event.*;
public class TestInnerClass {
public static void main(String[] args) {
new TFFrame().launchFrame();
}
}
class TFFrame extends Frame {
TextField num1, num2, num3;
public void launchFrame() {
num1 = new TextField(10);
num2 = new TextField(10);
num3 = new TextField(15);
Label lbPlus = new Label("+");
Button btnEqual = new Button("=");
setLayout(new FlowLayout());
add(num1);
add(lbPlus);
add(num2);
add(btnEqual);
add(num3);
btnEqual.addActionListener(new MyMonitor());
pack();
setVisible(true);
}
//内部类:1,方便的访问包装类的成员
// 2,该类不允许或不需要其他类进行访问
class MyMonitor implements ActionListener {
public void actionPerformed(ActionEvent e) {
int n1 = Integer.parseInt(num1.getText());
int n2 = Integer.parseInt(num2.getText());
int n3 = n1 + n2;
num3.setText("" + n3);
}
}
}