求和与清除

package b;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class c implements ActionListener{
JFrame jf;
JPanel p;
JButton b1,b2;
JTextField t1,t2,t3;
JLabel l1,l2,u1,u2;
public c(){
jf = new JFrame();
//jf.setLayout(new GridLayout(3,3));
p = new JPanel();
p.setLayout(new GridLayout(3,3));
b1 = new JButton("求和");
b1.addActionListener(this);
b2 = new JButton("清除");
b2.addActionListener(this);
t1 = new JTextField(5);
t2 = new JTextField(5);
t3 = new JTextField(5);
l1 = new JLabel("加数1");
l2 = new JLabel("加数2");
u1 = new JLabel();
u2 = new JLabel();
jf.add(p);
p.add(l1);
p.add(t1);
p.add(u1);
p.add(l2);
p.add(t2);
p.add(u2);
p.add(b1);
p.add(t3);
p.add(b2);
jf.setVisible(true);
jf.setSize(200,200);
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("求和")) {
String a=t1.getText();
String b=t2.getText();
int a1=Integer.parseInt(a);
int b1=Integer.parseInt(b);
String c =String.valueOf(a1+b1);
t3.setText(c);
}
else {
t1.setText(null);
t2.setText(null);
t3.setText(null);
}

}
public static void main(String[] agrs){
new c();
}

}

posted @ 2019-05-29 11:54  杨垚1  阅读(147)  评论(0编辑  收藏  举报