67,518
社区成员
发帖
与我相关
我的任务
分享
package test;
import java.awt.*;
import java.awt.event.*;
public class TFMath {
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 lb1plus = new Label("+");
Button btnEqual = new Button("=");
btnEqual.addActionListener(new MyMoniter(this));
setLayout(new FlowLayout());
add(num1);
add(lb1plus);
add(num2);
add(btnEqual);
add(num3);
pack();
setVisible(true);
}
}
class MyMoniter implements ActionListener {
TFFrame tf = null;
public MyMoniter(TFFrame tf){
this.tf = tf;
}
public void actionPerformed(ActionEvent e) {
int n1 = Integer.parseInt(tf.num1.getText());
int n2 = Integer.parseInt(tf.num2.getText());
tf.num3.setText(""+(n1+n2));
}
}
class MyMoniter implements ActionListener {
TFFrame tf = newTFFrame();
public void actionPerformed(ActionEvent e) {
int n1 = Integer.parseInt(tf.num1.getText());
int n2 = Integer.parseInt(tf.num2.getText());
tf.num3.setText(""+(n1+n2));
}
}