package First; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class summation implements ActionListener { JFrame f; JPanel p; JButton b1,b2; JLabel l1,l2,l3,l4; JTextField t1,t2,t3; public summation() { f = new JFrame(); p = new JPanel(new GridLayout(3,3)); b1 = new JButton("求和"); b1.addActionListener(this); b2 = new JButton("清除"); b2.addActionListener(this); l1=new JLabel("加数1"); l2=new JLabel("加数2"); l3=new JLabel(" "); l4=new JLabel(" "); t1=new JTextField(); t2=new JTextField(); t3=new JTextField(); f.setSize(300,200); f.setVisible(true); f.add(p); p.add(l1); p.add(t1); p.add(l3); p.add(l2); p.add(t2); p.add(l4); p.add(b1); p.add(t3); p.add(b2); } public static void main(String[] args){ new summation(); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(e.getSource()==b1) { int sum; int a=Integer.parseInt(t1.getText()); int b=Integer.parseInt(t2.getText()); sum=a+b; t3.setText(sum+""); } if(e.getSource()==b2) { t1.setText(null); t2.setText(null); t3.setText(null); } } }
原文地址:https://www.cnblogs.com/wannengc/p/10962713.html
时间: 2024-10-07 18:59:07