java实现计算器and图形界面

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/New_feature/article/details/78440506
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

//AWT实现
public class AWTCalculation{
    //将窗口中的组件定义为属性
    private Frame frame;               //定义窗体
    public TextField field;              //定义文本框
    private Button[] allButtons;     //定义按钮数组

    //在构造方法中创建组件
    public AWTCalculation(){
        frame = new Frame("AWT计算器");
        field = new TextField(20);
        //创建计算机按钮
        allButtons = new Button[20];
        String str = "←^√±789÷456×123-0.=+";
        for(int i = 0; i < str.length(); i++){
            allButtons[i] = new Button(str.substring(i,i+1));
        }
    }

    //初始化窗口,设置布局
    private void init(){
        //frame的北部面板:默认FlowLayout布局
        Panel northPanel = new Panel();
        northPanel.add(field);
        //frame的中部面板:设置GridLayout布局
        Panel centerPanel = new Panel();
        GridLayout grid = new GridLayout(5,4,8,8);
        centerPanel.setLayout(grid);
        //将按钮添加至中部面板
        for(int i = 0;i < allButtons.length; i++){
            centerPanel.add(allButtons[i]);
        }
        //将面板添加至窗口
        frame.add(northPanel,BorderLayout.NORTH);
        frame.add(centerPanel,BorderLayout.CENTER);
    }

    //设置窗口显示属性
    public void showMe(){

        init();
        addEventHandler();
        frame.pack();
        frame.setLocation(500,400);    //窗口出现的位置
        frame.addWindowListener(new WindowCloseHanDler());
        frame.setResizable(false);          //禁止改变窗口大小
        frame.setBackground(Color.PINK);
        frame.setVisible(true);             //设置窗体可见
    }

    //事件监听器 --- 关闭窗口
    private class WindowCloseHanDler extends WindowAdapter{
        public void windowClosing(WindowEvent e){
            System.exit(0);
        }
    }

    //功能实现部分
    public void addEventHandler(){   //添加监听
        for(int i = 0; i < allButtons.length; i++){   //按钮区监听
            allButtons[i].addActionListener(new CalculateActionHandler());
        }
    }

    //利用内部类继承ActionListener 实现 ActionPerformed 方法
    class CalculateActionHandler implements ActionListener{

        double op1 = 0,op2 = 0;   //存储两个操作数
        String operator = "";
        boolean flag = true;

        @Override
        public void actionPerformed(ActionEvent e) {
            String command = e.getActionCommand();          //按钮上的文本
            if("0123456789.".indexOf(command) != -1){     //数字按钮或小数点按钮
                if(flag){                                 //如果之前处理的是等号,开始组织新的操作数
                    field.setText(command);
                    flag = false;
                }else{                                    //否则继续拼接操作数
                    field.setText(field.getText()+command);
                }
            }else if( "← ^ √ ± ÷ × - +".indexOf(command) != -1 ){           //1取文本框中的数据:第一个操作数
                if(command.equalsIgnoreCase("←")){  //如果是清除,则归零
                    op1 = 0;
                    field.setText("");
                }else if(command.equalsIgnoreCase("√")) {
                    operator = command;
                    field.setText(command);
                }else {
                    op1 = Double.valueOf(field.getText());
                    //2 记下运算符
                    operator = command;
                    //3 清空文本框
                    field.setText(command);
                }
                flag = true;
            }else if( command.equalsIgnoreCase("=") ){  //等号按钮
                double res = 0;
                String text = field.getText();
                if(text.length()>0){
                    op2 = Double.valueOf(text);
                    switch(operator) {
                        case "+":
                            res = op1 + op2;
                            break;
                        case "-":
                            res = op1 - op2;
                            break;
                        case "×":
                            res = op1 * op2;
                            break;
                        case "÷":
                            res = op1 / op2;
                            break;
                        default:
                            break;
                    }
                    field.setText(String.valueOf(res));
                }
                flag = true;
            }
        }
    }
    public static void main(String []args){
        new AWTCalculation().showMe();
    }
}

猜你喜欢

转载自blog.csdn.net/New_feature/article/details/78440506