编程语言
首页 > 编程语言> > javaSwing加法

javaSwing加法

作者:互联网

package swing;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class ADD {
    JButton b1,b2;
    JTextField t1,t2,t3;
    JLabel l1,l2,l3,l4;
    GridLayout gl;
    public ADD(){
        JPanel p=new JPanel();
        JFrame f=new JFrame();
        gl=new GridLayout(3,3);
        b1=new JButton("求和");
        b1.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                int sum = Integer.parseInt(t1.getText()) + Integer.parseInt(t2.getText());
                t3.setText(sum+"");
            }
        });
        b2=new JButton("清除");
        b2.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                t1.setText(null);
                t2.setText(null);
                t3.setText(null);
            }
        });
        t1=new JTextField();
        t2=new JTextField();
        t3=new JTextField();
        l1=new JLabel("加数1");
        l2=new JLabel("加数2");
        l3=new JLabel("");
        l4=new JLabel("");
        f.setSize(500, 500);
        f.add(p);
        p.setLayout(gl);
        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);
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {    
        ADD a=new ADD();
        
        
        

}
}

 

标签:加法,t2,add,import,new,JLabel,javaSwing,public
来源: https://www.cnblogs.com/LYY1084702511/p/11022867.html