0
点赞
收藏
分享

微信扫一扫

java基础案例11-2简易计算器

覃榜言 2022-04-13 阅读 51
java
package com.itheima;

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

public class Main{
    public static void main(String[] args){
        //容器
        JFrame frame = new JFrame("calculator");
        frame.setLayout(new FlowLayout(FlowLayout.LEADING, 30, 20));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(590, 500);
        frame.setLocation(400, 400);
        //文本框
        Font Ff = new Font("幼圆", Font.BOLD, 20);//字体大小
        JTextField input = new JTextField(30);
        input.setEditable(false);
        input.setFont(Ff);
        input.setPreferredSize(new Dimension(200, 50));//框的大小
        JPanel panel = new JPanel();
        panel.add(input);
        frame.add(panel);

        //清除按钮
        JButton btn = new JButton("Clear");
        Font F = new Font("幼圆", Font.BOLD, 30);
        btn.setFont(F);
        JPanel panel1 = new JPanel();
        btn.setPreferredSize((new Dimension(150, 50)));
        panel1.add(btn);
        frame.add(panel1);
        btn.addActionListener(e->{
            input.setText("");
        });

        //N个按钮
        Font f = new Font("幼圆", Font.BOLD, 20);//字体
        JButton btn1 = new JButton("7");
        btn1.setFont(f);
        btn1.setPreferredSize((new Dimension(127, 50)));
        JButton btn2 = new JButton("8");
        btn2.setFont(f);
        btn2.setPreferredSize((new Dimension(127, 50)));
        JButton btn3 = new JButton("9");
        btn3.setFont(f);
        btn3.setPreferredSize((new Dimension(127, 50)));
        JButton btn4 = new JButton("/");
        btn4.setFont(f);
        btn4.setPreferredSize((new Dimension(127, 50)));
        JPanel panel2 = new JPanel();
        panel2.add(btn1);
        panel2.add(btn2);
        panel2.add(btn3);
        panel2.add(btn4);

        JButton btn5 = new JButton("4");
        btn5.setFont(f);
        btn5.setPreferredSize((new Dimension(127, 50)));
        JButton btn6 = new JButton("5");
        btn6.setFont(f);
        btn6.setPreferredSize((new Dimension(127, 50)));
        JButton btn7 = new JButton("6");
        btn7.setFont(f);
        btn7.setPreferredSize((new Dimension(127, 50)));
        JButton btn8 = new JButton("*");
        btn8.setFont(f);
        btn8.setPreferredSize((new Dimension(127, 50)));
        JPanel panel3 = new JPanel();
        panel3.add(btn5);
        panel3.add(btn6);
        panel3.add(btn7);
        panel3.add(btn8);

        JButton btn9 = new JButton("1");
        btn9.setFont(f);
        btn9.setPreferredSize((new Dimension(127, 50)));
        JButton btn10 = new JButton("2");
        btn10.setFont(f);
        btn10.setPreferredSize((new Dimension(127, 50)));
        JButton btn11 = new JButton("3");
        btn11.setFont(f);
        btn11.setPreferredSize((new Dimension(127, 50)));
        JButton btn12 = new JButton("-");
        btn12.setFont(f);
        btn12.setPreferredSize((new Dimension(127, 50)));
        JPanel panel4 = new JPanel();
        panel4.add(btn9);
        panel4.add(btn10);
        panel4.add(btn11);
        panel4.add(btn12);

        JButton btn13 = new JButton("0");
        btn13.setFont(f);
        btn13.setPreferredSize((new Dimension(127, 50)));
        JButton btn14 = new JButton(".");
        btn14.setFont(f);
        btn14.setPreferredSize((new Dimension(127, 50)));
        JButton btn15 = new JButton("=");
        btn15.setFont(f);
        btn15.setPreferredSize((new Dimension(127, 50)));
        JButton btn16 = new JButton("+");
        btn16.setFont(f);
        btn16.setPreferredSize((new Dimension(127, 50)));
        JPanel panel5 = new JPanel();
        panel5.add(btn13);
        panel5.add(btn14);
        panel5.add(btn15);
        panel5.add(btn16);

        frame.add(panel2);
        frame.add(panel3);
        frame.add(panel4);
        frame.add(panel5);

        //添加监听器
        btn1.addActionListener(e->{
            String content = input.getText();
            input.setText(content + "7");
        });
        btn2.addActionListener(e->{
            String content = input.getText();
            input.setText(content + "8");
        });
        btn3.addActionListener(e->{
            String content = input.getText();
            input.setText(content + "9");
        });
        btn4.addActionListener(e->{
            String content = input.getText();
            input.setText(content + "/");
        });
        btn5.addActionListener(e->{
            String content = input.getText();
            input.setText(content + "4");
        });
        btn6.addActionListener(e->{
            String content = input.getText();
            input.setText(content + "5");
        });
        btn7.addActionListener(e->{
            String content = input.getText();
            input.setText(content + "6");
        });
        btn8.addActionListener(e->{
            String content = input.getText();
            input.setText(content + "*");
        });
        btn9.addActionListener(e->{
            String content = input.getText();
            input.setText(content + "1");
        });
        btn10.addActionListener(e->{
            String content = input.getText();
            input.setText(content + "2");
        });
        btn11.addActionListener(e->{
            String content = input.getText();
            input.setText(content + "3");
        });
        btn12.addActionListener(e->{
            String content = input.getText();
            input.setText(content + "-");
        });
        btn13.addActionListener(e->{
            String content = input.getText();
            input.setText(content + "0");
        });
        btn14.addActionListener(e->{
            String content = input.getText();
            input.setText(content + ".");
        });
        btn15.addActionListener(e->{
            String content = input.getText();
            String cont = cal(content);
            input.setText(content + "=" + cont);
        });
        btn16.addActionListener(e->{
            String content = input.getText();
            input.setText(content + "+");
        });

        frame.setVisible(true);
    }
    //进行字符串的计算
    public static String cal(String str)
    {
        char[] fh = {'+', '-', '*', '/'};
        char[] a = str.toCharArray();
        int d =  0;
        for(int i = 0; i < str.length(); i++)
            if(a[i] == '+' || a[i] == '-' || a[i] == '*' || a[i] == '/')
                d = i;
        String start = str.substring(0, d);
        String end = str.substring(d + 1);
        double num1 =  Double.parseDouble(start);
        double num2 =  Double.parseDouble(end);
        int t = 0;
        for(int i = 0; i < 4; i++)
            if(a[d] == fh[i])
                t  = i;
        String s = "";
        switch(t)
        {
            case 0: s = (num1 + num2) + ""; break;
            case 1: s = (num1 - num2) + "";break;
            case 2: s = (num1 * num2) + "";break;
            case 3: s = (num1 / num2) + "";break;
        }
        return s;
    }
}
举报

相关推荐

0 条评论