package cys;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import javax.swing.*;

public class AddOperator {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        WindowButton win = new WindowButton("加法器");
    }

}

class WindowButton extends JFrame implements ActionListener{
    int num1,num2;
    JLabel answer,tishi,jiahao;
    JTextField txt1,txt2;
    JButton buttonSure;
    
    WindowButton(String s){
        Container con = getContentPane();
        con.setLayout(new FlowLayout());
        buttonSure = new JButton("确定");
        txt1 = new JTextField("0",10);
        txt2 = new JTextField("0",10);
        answer = new JLabel("0",JLabel.CENTER);
        answer.setSize(20,20);
        tishi = new JLabel("请输入你想要加的两个数",JLabel.CENTER);
        jiahao = new JLabel("+",JLabel.CENTER);
        con.add(tishi);
        con.add(txt1);
        con.add(jiahao);
        con.add(txt2);
        con.add(answer);
        con.add(buttonSure);
        buttonSure.addActionListener(this);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(600,150);
        this.setVisible(true);
        validate();    
    }
    public void actionPerformed(ActionEvent e){
        
        if(e.getSource()==buttonSure){
            num1=Integer.parseInt(txt1.getText());
            num2=Integer.parseInt(txt2.getText());
            answer.setText(num1+num2+"");
            
        }
        
    }
}

Java学习——加法器_java