代码是啥代码咋写代码咋写

 

用java画出窗口,写上标题,3个文本框,一个按钮,然后获取文本框的输入值相加。加我好友,

import java.awt.Button;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class Calculator extends JFrame implements ActionListener {
    //创建按钮、文本框
    Button b1, b2, b3, b4;
    JTextField text, text1, res;

    public Calculator() {
        super("一个简单的加法器");

        // 设置布局(设置没有布局,才能自己定位)
        setLayout(null);

        ///操作数1标签
        Label uname = new Label("操作数1:");
        // 加入窗口中
        add(uname);
        // 设置组件位置 (组件的x坐标,组件的y坐标,组件的长,组件的高)
        uname.setBounds(20, 50, 40, 20);

        // 操作数1文本框
        text = new JTextField();
        add(text);
        text.setBounds(65, 50, 200, 20);

        // 操作数2标签
        Label pwd = new Label("操作数2:");
        add(pwd);
        pwd.setBounds(20, 80, 40, 20);

        // 操作数2文本框
        text1 = new JTextField();
        add(text1);
        text1.setBounds(65, 80, 200, 20);

        // 结果文字标签
        Label rest = new Label("计算结果:");
        add(rest);
        rest.setBounds(20, 110, 40, 20);

        // 结果文本框
        res = new JTextField();
        add(res);
        res.setBounds(65, 110, 200, 20);

        // 按钮1
        b1 = new Button("加+");
        add(b1);
        b1.setBounds(300, 80, 80, 30);

        b1.addActionListener(this);

        //设置窗口大小
        setSize(450, 250);

        //设置窗口是否可见,设为false的话看不到窗体
        setVisible(true);

        //设置窗体居中
        setLocationRelativeTo(null);

        //点击关闭按钮解除程序
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    //main方法,new Calculator对象执行程序
    public static void main(String[] args) {
        new Calculator();
    }

    //执行加减乘除方法
    public void actionPerformed(ActionEvent e) {
        //获取两个操作数文本框内容,转成double格式参与准备运算
        double t1 = Double.parseDouble( text.getText() );
        double t2 = Double.parseDouble( text1.getText() );
        res.setText((t1+t2)+"");
    }
}