AWT 制作简易计算器

目录

效果演示

代码展示 


Java AWT (Abstract Window Toolkit) package is a library used for designing graphical user interfaces.

Java AWT(抽象窗口工具包)软件包是用于设计图形用户界面的库。

It contains classes for placing various user intractable components and graphics. However, the components of this class are platform dependent. They are heavy and rely on the OS for their functioning.

它包含用于放置各种用户难以处理的组件和图形的类。 但是,此类的组件取决于平台。 它们很重,并且依靠OS来运行。

The AWT package has classes using which we can create TextBoxes, CheckBoxes, RadioButton, List, etc.

AWT包具有一些类,通过这些类可以创建TextBoxes , CheckBoxes , RadioButton , List等。


效果演示

各功能实现详解


(1)界面设置以及布局
按钮分为两种类型,功能类型以及输入类型按钮。输入类型按钮为0~9数字键,其它则为功能按钮。
按键采用GridLayout布局管理器将其分为4*5大小格子。添加按键时需依次从左到右,从上到下添加到Panel类型容器中,再将Panel类型容器采用GridLayout布局管理器。
文本框采用BorderLayout布局管理器置于最上方。

菜单栏中包含操作和其他两部分
操作中含有重置和退出,其他包含作者信息。
菜单栏需要用MenuBar类来实现,菜单栏中的元素需要用Menu实现,每个元素中按键采用MenuItem实现。
在创建MenuItem、Menu和MenuBar对象之后,调用Menu的add()方法将多个MenuItem组合成菜单(也可将另一个Menu对象组合起来形成二级菜单),再调用MenuBar的add()方法将多个Menu组合成菜单条,最后调用Frame对象的setMenuBar()方法来为该窗口添加菜单条。
 

代码展示 

package awt01;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;

public class Myfirst {
    public static void main(String[] args) throws InterruptedException {
        Frame frame = new Frame("计算器");
        Panel p1 = new Panel();
        TextArea ta = new TextArea(3, 30);
        p1.add(ta);
        frame.add(p1, BorderLayout.NORTH);
        Panel p2 = new Panel();
        GridLayout gridLayout = new GridLayout(3, 5, 4, 4);
        p2.setLayout(gridLayout);
        ActionListener ls = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String actionCommand = e.getActionCommand();
                ta.append(actionCommand);
                if(actionCommand.equals("=")) {
                    String calculate = calculate(ta.getText());
                    ta.setText(calculate);
                }

                System.out.println(ta.getText());
            }
        };
        for (int i = 0; i < 10; i++) {
            Button b = new Button(i + "");
            b.addActionListener(ls);
            p2.add(b);
        }
        Button b1 = new Button("+");
        b1.addActionListener(ls);
        p2.add(b1);
        Button b2 = new Button("-");
        b2.addActionListener(ls);
        p2.add(b2);
        Button b3 = new Button("*");
        b3.addActionListener(ls);
        p2.add(b3);
        Button b4 = new Button("/");
        b4.addActionListener(ls);
        p2.add(b4);
        Button b5 = new Button("=");
        b5.addActionListener(ls);
        p2.add(b5);
        Button b6 =new Button("AC");
        b6.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                e.getActionCommand();
                ta.setText("");
            }
        });
        p2.add(b6);
        frame.add(p2);
        frame.pack();
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        frame.setVisible(true);
    }


    static char[] all = {'0','1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '-', '*', '/', '='};

    private static String calculate(String s) {
        int i;
        ArrayList<Integer> arr = new ArrayList<>();
        for(i=0;i<s.length();i++)
        {
            char c = s.charAt(i);
            for(int j=0;j< all.length;j++)
            {
                if(c==all[j])
                {
                    arr.add(j);
                    continue;
                }
            }
        }
        int sum=0;
        ArrayList<Integer> data = new ArrayList<>();
        ArrayList<Integer> fuhao = new ArrayList<>();
        for (Integer integer : arr) {
            if(integer<=9)
            {
                sum=sum*10;
                sum+=integer;

            }else {data.add(sum);
                fuhao.add(integer);
            sum=0;}

        }
        System.out.println(fuhao);
        int cal = cal(data, fuhao);

        return cal+"";
    }
    private static int cal(ArrayList<Integer> data,ArrayList<Integer> fuhao){
        for (int k = 0; k <fuhao.size() ; k++) {
            if(fuhao.get(k)==10&&fuhao.get(k+1)!=12&&fuhao.get(k+1)!=13){
                int i1 = data.get(0) + data.get(1);
                data.set(0,i1);
                data.remove(1);
            }
            else if (fuhao.get(k)==11&&fuhao.get(k+1)!=12&&fuhao.get(k+1)!=13)
            {
                int i2 =data.get(0)-data.get(1);
                data.set(0,i2);
                data.remove(1);
            }
            else if (fuhao.get(k)==12)
            {
                int i3 = data.get(0)*data.get(1);
                data.set(0,i3);
                data.remove(1);
            }
            else if (fuhao.get(k)==13)
            {
                int i4 =data.get(0)/data.get(1);
                data.set(0,i4);
                data.remove(1);
            }
            else if (fuhao.get(k)==10&&fuhao.get(k+1)==12)
            {
                int i5 =data.get(1)*data.get(2);
                data.set(1,i5);
                data.remove(2);
                fuhao.remove(k+1);
                cal(data,fuhao);
            }
            else if(fuhao.get(k)==10&&fuhao.get(k+1)==13)
            {
                int i6 = data.get(1)/data.get(2);
                data.set(1,i6);
                data.remove(2);
                fuhao.remove(k+1);
                cal(data,fuhao);
            }else if(fuhao.get(k)==11&&fuhao.get(k+1)==12)
            {
                int i7 = data.get(1)*data.get(2);
                data.set(1,i7);
                data.remove(2);
                fuhao.remove(k+1);
                cal(data,fuhao);
            }else if(fuhao.get(k)==11&&fuhao.get(k+1)==13)
            {
                int i8 = data.get(1)/data.get(2);
                data.set(1,i8);
                data.remove(2);
                fuhao.remove(k+1);
                cal(data,fuhao);
            }
            else break;

        }return data.get(0);
    }

}
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值