import java.awt.*;
import java.awt.event.*;
public class TFMath {
public static void main(String[] args) {
new TFrame();
}
}
class TFrame extends Frame{
TextField tf1, tf2, tf3;
TFrame(){
tf1 = new TextField("", 10);
tf2 = new TextField("", 10);
tf3 = new TextField("", 12);
Label lbladd = new Label("+");
Button btneq = new Button("=");
Monitor mnt = new Monitor(this);
btneq.addActionListener(mnt);
super.add(tf1);
super.add(lbladd);
super.add(tf2);
super.add(btneq);
super.add(tf3);
super.setLayout(new FlowLayout(FlowLayout.CENTER));
super.pack();
super.setBounds(400, 400, 500, 300);
super.setBackground(Color.DARK_GRAY);
super.setVisible(true);
}
}
class Monitor implements ActionListener{
TFrame tf = null;
public Monitor(TFrame tf){
this.tf = tf;
}
public void actionPerformed(ActionEvent e){
int num1 = Integer.parseInt(tf.tf1.getText());
int num2 = Integer.parseInt(tf.tf2.getText());
int num3 = num1 + num2;
tf.tf3.setText(String.valueOf(num3));
}
}
GUI中的事件绑定
最新推荐文章于 2023-05-16 09:46:47 发布