package com.zeng.GUI;
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 Calculate3 {
public static void main(String[] args) {
new MyCalculate2().LoadFrame();
}
}
class MyCalculate3 extends Frame {
TextField textField1, textField2, textField3;//三个文本框
private class MyCalcuAction3Listener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {//获取文本上的内容并做加法运算
int n1 = Integer.parseInt(textField1.getText());
int n2 = Integer.parseInt(textField2.getText());
textField3.setText(" " + (n1 + n2));
// textField1.setText("");
// textField2.setText("");
}
}
public void LoadFrame() {
//组件
textField1 = new TextField(10);
textField2 = new TextField(10);
textField3 = new TextField(20);
Label label = new Label("+");
Button button = new Button("=");
//监控事件
button.addActionListener(new MyCalcuAction3Listener());
addWindowListener(new FrameClose2());
//布局
setLayout(new FlowLayout());
//组装
add(textField1);
add(label);
add(textField2);
add(button);
add(textField3);
//收尾
pack();
setVisible(true);
}
}
//窗口关闭类
class FrameClose3 extends WindowAdapter {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
文本框加法计算器
最新推荐文章于 2022-08-24 14:06:05 发布