【Java】Swing實現一個簡單的計算器


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

/**
 * 計算器
 * @author paul
 * 2019.11.25 21:43
 * */
public class MyCalculator {
    private String str="";//輸入輸出框內容
    private JTextField text_input;//輸出框
    private JPanel jp_bottomArea;//按鈕區域
    private String []addsButtonString={"1","2","3","+","4","5","6","-","7","8","9","*",".","0","求根","/","=","取反","AC"};
    public MyCalculator(){
        //初始化窗體
        JFrame frame=new JFrame("計算器");
        Container c=frame.getContentPane();
        c.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));//設置排布方式為Y軸排列

        frame.setLocation(200,300);//設置位置
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        text_input=new JTextField(30);
        text_input.setHorizontalAlignment(JTextField.RIGHT);
        JPanel jPanel=new JPanel(new GridLayout(1,1,10,10));
        jPanel.add(text_input);
        c.add(jPanel);

        GridBagLayout gridBagLayout=new GridBagLayout();
        GridBagConstraints cs=new GridBagConstraints();
        jp_bottomArea=new JPanel();
        jp_bottomArea.setLayout(gridBagLayout);
        for(int i=0;i<addsButtonString.length;i++){
            if((i+1)%4==0){
                cs.gridwidth=GridBagConstraints.REMAINDER;
            }else if(addsButtonString[i].equals("=")){
                cs.gridwidth=2;
            }else {
                cs.fill=GridBagConstraints.BOTH;
                cs.weightx=1.0;
                cs.gridwidth=1;
            }
            JButton btn = new JButton(addsButtonString[i]);
            gridBagLayout.setConstraints(btn,cs);
            btn.addActionListener(e -> {
                String command = e.getActionCommand();
                setShowTextFiledNew(command);
            });
            jp_bottomArea.add(btn);
        }
        c.add(jp_bottomArea);

        frame.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;
       }
       text_input.setText(str);
    }
    /**
     * 計算
     * @param str 需要計算的字符串
     * 根據空格進行分割成字符串數組
     * 然后判斷是哪種類型的運算符並進行計算
     * 通過一個result來存放最終結果
     * */
    public String getResult(String str){
        Double result=0.0;
        String []need_to_do=str.split(" ");
        for(int i=0;i<need_to_do.length;i++){
            switch (need_to_do[i]){
                case "+":
                    result=result+(Double.parseDouble(need_to_do[i-1])+Double.parseDouble(need_to_do[i+1]));
                    break;
                case "-":
                    result=result+(Double.parseDouble(need_to_do[i-1])-Double.parseDouble(need_to_do[i+1]));
                    break;
                case "*":
                    result=result+(Double.parseDouble(need_to_do[i-1])*Double.parseDouble(need_to_do[i+1]));
                    break;
                case "/":
                    result=result+(Double.parseDouble(need_to_do[i-1])/Double.parseDouble(need_to_do[i+1]));
                    break;
                case "求根":
                    result=result+(Math.sqrt(Double.parseDouble(need_to_do[i-1])));
                    break;
                case "取反":
                    result=result+(-Double.parseDouble(need_to_do[i-1]));
                    break;
            }

        }
        return result+"";
    }


    public static void main(String[] args) {
        new MyCalculator();
    }
}

界面:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM