Frame:加法计算器

该博客展示了如何使用Java创建一个简单的加法计算器应用程序。它包括两个文本框用于输入数字,一个标签显示加号,一个等于按钮以及一个结果显示文本框。当用户点击等于按钮时,程序会将两个输入的数字相加,并显示在结果文本框中,同时清空输入框。
摘要由CSDN通过智能技术生成

Frame:加法计算器

package calculator;

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

public class CalculatorDemo01 {
    public static void main(String[] args) {
        new MyCalculator01();
    }
}

class MyCalculator01 extends Frame {
    MyCalculator01(){
        //3个文本框
        TextField t1 = new TextField(10);
        TextField t2 = new TextField(10);
        TextField t3 = new TextField(15);

        //按钮
        Button button = new Button("=");

        //文本框
        Label label = new Label("+");

        //按钮监听事件
        button.addActionListener(new MyCalculatorActionListener01(t1,t2,t3));

        //添加到frame中并设置
        setLayout(new FlowLayout());
        setVisible(true);
        setBackground(Color.RED);
        add(t1);
        add(label);
        add(t2);
        add(button);
        add(t3);

        pack();



    }


}

class MyCalculatorActionListener01 implements ActionListener{

    TextField t1,t2,t3;

    public MyCalculatorActionListener01(TextField t1,TextField t2,TextField t3) {
        this.t1=t1;
        this.t2=t2;
        this.t3=t3;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        int a = Integer.parseInt(t1.getText());
        int b = Integer.parseInt(t2.getText());

        t3.setText(""+(a+b));
        t1.setText("");
        t2.setText("");
    }
}
package calculator;

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

public class CalculatorDemo01 {
    public static void main(String[] args) {
        new MyCalculator01();
    }
}

class MyCalculator01 extends Frame {

    //3个文本框
    TextField t1,t2,t3;

    public void loadFrame(){
        t1 = new TextField(10);
        t2 = new TextField(10);
        t3 = new TextField(15);
        //按钮
        Button button = new Button("=");

        //文本框
        Label label = new Label("+");

        //按钮监听事件
        button.addActionListener(new MyCalculatorActionListener01(this));

        //添加到frame中并设置
        setLayout(new FlowLayout());
        setVisible(true);
        setBackground(Color.RED);
        add(t1);
        add(label);
        add(t2);
        add(button);
        add(t3);

        pack();
    }
}

class MyCalculatorActionListener01 implements ActionListener{

    MyCalculator01 myCalculator01 = null;

    public MyCalculatorActionListener01(MyCalculator01 myCalculator01) {
        this.myCalculator01 = myCalculator01;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        int a = Integer.parseInt(myCalculator01.t1.getText());
        int b = Integer.parseInt(myCalculator01.t2.getText());

        myCalculator01.t3.setText(""+(a+b));
        myCalculator01.t1.setText("");
        myCalculator01.t2.setText("");
    }
}

package calculator;

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

public class Calculator {
    public static void main(String[] args) {
        new MyCalculator().loadFrame();
    }
}

class MyCalculator extends Frame{

    TextField num1,num2,num3;

    public void loadFrame(){
        num1 = new TextField(10);//字符数
        num2 = new TextField(10);//字符数
        num3 = new TextField(10);//字符数

        Button button = new Button("=");
        Label label = new Label("+");

        button.addActionListener(new MyCalculatorListener());


        setBounds(100,100,300,300);
        setBackground(Color.RED);

        setLayout(new FlowLayout());

        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);

        setVisible(true);
        pack();

    }

    private class MyCalculatorListener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            //1.获取加数和被加数
            //2.将这个值运算后,放到第三个框
            //3.清空前两个框
            int a1 = Integer.parseInt(num1.getText());
            int a2 = Integer.parseInt(num2.getText());

            num3.setText(""+(a1+a2));
            num1.setText("");
            num2.setText("");
        }
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值