求和计算器

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

import javax.swing.*;
public class windows implements ActionListener {
    JFrame jf;
    JPanel jp,jp1;
    JButton jb1,jb2;
    JTextField  jt1,jt2,jt3;
    JLabel  jl,jl1;
    public windows() {
         jf=new JFrame("求和");
         jp=new JPanel(new GridLayout(2,2,10,10));
         jp1=new JPanel();
         jl=new JLabel("加数1");
         jl1=new JLabel("加数2");
         jt1=new JTextField(10);
         jt2=new JTextField(10);
         jp.add(jl);
         jp.add(jt1);
         jp.add(jl1);
         jp.add(jt2);
         jb1=new JButton("求和");
         jt3=new JTextField(10);
         jb2=new JButton("清除");
         jp1.add(jb1);
         jb1.addActionListener(this);
         jp1.add(jt3);
         jp1.add(jb2);
         jb2.addActionListener(this);
         
        
        jf.add(BorderLayout.SOUTH,jp1);
        jf.add(BorderLayout.CENTER,jp);
        jf.setSize(300,200);
        jf.setVisible(true);
        
    }
    public void actionPerformed(ActionEvent e) {
        if(e.getActionCommand().equals("求和")) {
            String a=jt1.getText();
            String b=jt2.getText();
            int a1=Integer.parseInt(a);
            int b1=Integer.parseInt(b);
             String c =String.valueOf(a1+b1);
            jt3.setText(c);
        }
        else {
            jt1.setText(null);
            jt2.setText(null);
            jt3.setText(null);
        }
        
        
    }
    public static void main(String[] args) {
        new windows();
        
    }
    
    
    

}

求和计算器_第1张图片

你可能感兴趣的:(求和计算器)