用Java的swing开发一个简易计算器

话不多说直接贴源码,如下:

package cn.sjy.calculator;

import javax.swing.*;
import java.awt.*;

/**
 * 简易计算器
 * @author 石俊熠
 * 2020.7.13 11:24
 * 注:仿照某Java大佬的源码改之
 */
public class Test {
     
    private String str=" ";//输入输出框显示内容
    private JTextField input;//输出框
    private JPanel button;//按钮区域
    private  String[] addbutton={
     "AC","求根","取反","/","7","8","9","*","4","5","6","-","1","2","3","+","+/-","0",".","="};
    private String showTextFiledNew;

    //设置主窗体
    public Test() {
     
        //初始化窗体
        JFrame f = new JFrame("计算器");
        Container c = f.getContentPane();
        c.setLayout(new BoxLayout(f.getContentPane(), BoxLayout.Y_AXIS));//设置排列布局为Y轴排列

        f.setLocation(200, 300);
        //f.setSize(500, 600);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);

        input = new JTextField(30);//设置输入框的长度为30
        input.setHorizontalAlignment(JTextField.RIGHT);//设置输入框内容的对齐方式,设置为右对齐
        JPanel jPanel = new JPanel();//建立一个面板
        jPanel.add(input);
        c.add(jPanel);

        GridBagLayout gridBagLayout = new GridBagLayout();
        GridBagConstraints cs = new GridBagConstraints();
        button = new JPanel();
        button.setLayout(gridBagLayout);
        for (int i = 0; i < addbutton.length; i++) {
     
            if ((i + 1) % 4 == 0) {
     
                cs.gridwidth = GridBagConstraints.REMAINDER;
            } else {
     
                cs.fill = GridBagConstraints.BOTH;//使组件完全填充其显示区域
                cs.weightx = 1.0;
                cs.gridwidth = 1;
            }
            JButton b = new JButton(addbutton[i]);
            gridBagLayout.setConstraints(b, cs);//设置组件的约束
            b.addActionListener(e -> {
     
                String command = e.getActionCommand();
                setShowTextFiledNew(command);
            });
            button.add(b);
        }
        c.add(button);
        f.pack();
    }

    /**
     * 设置显示内容得窗体
     * @param command 按钮点击命令
     * 如果按下=执行计算命令
     * 如果按下运算符 则格式为 空格+运算符+空格
     * 如果按下数字 则直接拼接
     */
    public void setShowTextFiledNew(String command) {
     
        if(command.equals("=")){
     
            str = getResult(str);
        }else if(command.equals("+")||command.equals("-")||command.equals("*")||command.equals("/")||command.equals("求根")||command.equals("取反")){
     
            str = str+" "+command+" ";
        }else if(command.equals("AC")){
     
            str = "";
        }else{
     
            str = str+command;
        }
        input.setText(str);//将按钮的信息返回到input中
    }

    /**
     * 计算模块
     * @param str 需要计算的字符串
     * 根据空格进行分割成字符串数组
     * 然后判断是哪种类型进行计算
     *通过一个result来存放那个结果
     */
    private String getResult(String str) {
     
        Double result = 0.0;//用来存放结果
        String[] need = str.split(" ");//进行字符串分割
        for (String j : need) {
     
            System.out.print(j+" ");
        }
        for (int i = 0; i < need.length; i++) {
     
            switch (need[i]){
     
                case "+":
                    if (result == 0.0 && i==1){
     
                        result=result+(Double.parseDouble(need[i-1])+Double.parseDouble(need[i+1]));
                        break;
                    }else{
     
                        result=result+Double.parseDouble(need[i+1]);
                        break;
                    }
                case "-":
                        if(Double.parseDouble(need[i-1])>=Double.parseDouble(need[i+1])){
     
                            result = (Double.parseDouble(need[i-1])-Double.parseDouble(need[i+1]));
                            break;
                        }else {
     
                            result = +(Double.parseDouble(need[i-1])-Double.parseDouble(need[i+1]));
                            break;
                        }
                case "*":
                    result=result+(Double.parseDouble(need[i-1])*Double.parseDouble(need[i+1]));
                    break;
                case "/":
                    result=result+(Double.parseDouble(need[i-1])/Double.parseDouble(need[i+1]));
                    break;
                case "求根":
                    result=result+(Math.sqrt(Double.parseDouble(need[i-1])));
                    break;
                case "取反":
                    result=result+(-Double.parseDouble(need[i-1]));
                    break;
            }
        }
        return  result+"";
    }

    //设置主方法,调用Test方法
    public static void main(String[] args ){
     
        new Test();
    }
}

效果如图
用Java的swing开发一个简易计算器_第1张图片
某大佬的源码地址
祝大家生活愉快~

你可能感兴趣的:(日常学习,java)