欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

学习使用Java制作简易计算器的第三天

程序员文章站 2022-03-20 23:30:03
学习使用Java制作简易计算器的第三天今天是使用JFormDesigner来搭建简易计算器。一、搭建计算器页面使用JTextField和JBottom分别来搭建输入行、输出行和按钮,然后点击红色框框,给按钮添加事件。当然,前提是要把按钮和文本框的名字都改好,好见名知意。也好调整好他们的宽和高。二、写代码public static void main(String[] args) { Calculator calculator = new Calculator();...

学习使用Java制作简易计算器的第三天

今天是使用JFormDesigner来搭建简易计算器。

学习使用Java制作简易计算器的第三天

一、搭建计算器页面

使用JTextField和JBottom分别来搭建输入行、输出行和按钮,然后点击红色框框,给按钮添加事件。

学习使用Java制作简易计算器的第三天当然,前提是要把按钮和文本框的名字都改好,好见名知意。也好调整好他们的宽和高。
学习使用Java制作简易计算器的第三天
学习使用Java制作简易计算器的第三天最后点击这里生成大致的图形,一般来说JFormDesigner会自动生成。

学习使用Java制作简易计算器的第三天

二、写代码

public static void main(String[] args) {
        Calculator calculator = new Calculator();
        calculator.setSize(435,580);
        calculator.setResizable(false);
        calculator.setVisible(true);
    }

第一次使用swing,一开始连main方法里的设置是否可见为true都忘了。

接下来是部分重要的函数。
①点击等号在answer文本框生成的答案。

private void equMouseClicked(MouseEvent e) {
        try {
            Calculate cl = new Calculate();
            String[] postfixArray;
            char[] strArray = question.getText().toCharArray();
            postfixArray = cl.convertArray(strArray);
            float val = cl.getValue(postfixArray);
            if (val == (int) val) {
                answer.setText(String.valueOf((int) val));
            } else {
                //保留三位有效数字
                String temp = String.format("%.3f",val);
                answer.setText(temp);
            }
        } catch (Exception exp) {
            answer.setText("请输入正确的运算表达式!");
        }
    }

②点击<-往后退一格

private void backMouseClicked(MouseEvent e) {
        int len = ques.length();
        try {
            ques = ques.substring(0, len - 1);
            question.setText(ques);
        } catch (Exception exp) {
            question.setText("");
        }

    }

③按钮的具体外形

//---- seven ----
        seven.setText("7");
        seven.setFocusPainted(false);
        seven.setFont(new Font("宋体",Font.BOLD,30));
        seven.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                sevenMouseClicked(e);
            }
        });
        contentPane.add(seven, "cell 0 2,wmin 60,hmin 60");

④选项中的历史记录查看

private void reMousePressed(MouseEvent e) {
        area.setFont(new Font("宋体",Font.BOLD,20));
        area.setText(reText);
        reFrame.setLocation(800,400);
        //reFrame.setTitle("record");
        reFrame.setResizable(false);
        reFrame.pack();
        reFrame.setVisible(true);
    }

三、演示

学习使用Java制作简易计算器的第三天学习使用Java制作简易计算器的第三天
记录保存(保存最近计算的10次,多余10次会更新最旧的一条)
学习使用Java制作简易计算器的第三天

这里有两点缺陷,有些时候鼠标点击按键不灵,如果点击快的话,问题栏就不会更新。还有一个是因为我的数学计算是使用float类型,所以有些时候总会有一些计算上的小问题,比如 (1.2*6) 如果不保留三位有效数字的话,它在最后可能会出现比答案多或少0.0000000003(具体按情况为准),这是由于小数在计算机里存储的方式决定的。
希望对您有所帮助,也希望您能多多提意见!

四、具体代码:

package com.braden.exp1;

import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import javax.swing.*;

import com.braden.exp1.Calculate;
import net.miginfocom.swing.*;
/*
 * Created by JFormDesigner on Thu Dec 03 19:13:09 GMT+08:00 2020
 */



/**
 * @author Braden
 */
public class Calculator extends JFrame {

    String ques = "";
    String reText = "";
    int num = 0;
    public Calculator() {
        initComponents();
    }

    private void sevenMouseClicked(MouseEvent e) {
        ques += "7";
        question.setText(ques);
    }

    private void eightMouseClicked(MouseEvent e) {
        ques += "8";
        question.setText(ques);
    }

    private void nineMouseClicked(MouseEvent e) {
        ques += "9";
        question.setText(ques);
    }

    private void backMouseClicked(MouseEvent e) {
        int len = ques.length();
        try {
            ques = ques.substring(0, len - 1);
            question.setText(ques);
        } catch (Exception exp) {
            question.setText("");
        }

    }

    private void CMouseClicked(MouseEvent e) {
        ques = "";
        question.setText(ques);
    }

    private void fourMouseClicked(MouseEvent e) {
        ques += "4";
        question.setText(ques);
    }

    private void fiveMouseClicked(MouseEvent e) {
        ques += "5";
        question.setText(ques);
    }

    private void sixMouseClicked(MouseEvent e) {
        ques += "6";
        question.setText(ques);
    }

    private void addMouseClicked(MouseEvent e) {
        ques += "+";
        question.setText(ques);
    }

    private void subMouseClicked(MouseEvent e) {
        ques += "-";
        question.setText(ques);
    }

    private void oneMouseClicked(MouseEvent e) {
        ques += "1";
        question.setText(ques);
    }

    private void twoMouseClicked(MouseEvent e) {
        ques += "2";
        question.setText(ques);
    }

    private void threeMouseClicked(MouseEvent e) {
        ques += "3";
        question.setText(ques);
    }

    private void mulMouseClicked(MouseEvent e) {
        ques += "*";
        question.setText(ques);
    }

    private void divMouseClicked(MouseEvent e) {
        ques += "/";
        question.setText(ques);
    }

    private void zeroMouseClicked(MouseEvent e) {
        ques += "0";
        question.setText(ques);
    }

    private void dotMouseClicked(MouseEvent e) {
        ques += ".";
        question.setText(ques);
    }

    private void lBracketMouseClicked(MouseEvent e) {
        ques += "(";
        question.setText(ques);
    }

    private void rBracketMouseClicked(MouseEvent e) {
        ques += ")";
        question.setText(ques);
    }

    private void equMouseClicked(MouseEvent e) {
        try {
            Calculate cl = new Calculate();
            String[] postfixArray;
            char[] strArray = question.getText().toCharArray();
            postfixArray = cl.convertArray(strArray);
            float val = cl.getValue(postfixArray);
            if (val == (int) val) {
                answer.setText(String.valueOf((int) val));
            } else {
                //保留三位有效数字
                String temp = String.format("%.3f",val);
                answer.setText(temp);
            }
        } catch (Exception exp) {
            answer.setText("请输入正确的运算表达式!");
        } finally {
            if (num >= 10) {
                String[] tempString = reText.split("\\n");
                /*for (String i : tempString) {
                    System.out.println(i);
                }*/
                reText = "";
                for (int i = 1; i < tempString.length; i ++) {
                    reText = reText + tempString[i] + "\n";
                }
            }
            num ++;
            reText += question.getText() + " = " + answer.getText() +"\n";
        }
    }

    private void reMousePressed(MouseEvent e) {
        area.setFont(new Font("宋体",Font.BOLD,20));
        area.setText(reText);
        reFrame.setLocation(800,400);
        //reFrame.setTitle("record");
        reFrame.setResizable(false);
        reFrame.pack();
        reFrame.setVisible(true);
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        question = new JTextField();
        answer = new JTextField();
        seven = new JButton();
        eight = new JButton();
        nine = new JButton();
        back = new JButton();
        C = new JButton();
        four = new JButton();
        five = new JButton();
        six = new JButton();
        add = new JButton();
        sub = new JButton();
        one = new JButton();
        two = new JButton();
        three = new JButton();
        mul = new JButton();
        div = new JButton();
        zero = new JButton();
        dot = new JButton();
        lBracket = new JButton();
        rBracket = new JButton();
        equ = new JButton();
        chooseMenu = new JMenu("选项");
        reback = new JMenuItem("历史记录");
        area = new JTextArea(11,20);
        lb = new JLabel();
        reFrame = new JFrame();

        //======== this ========
        setTitle("\u8ba1\u7b97\u5668");
        URL url = this.getClass().getResource("calpic.png");
        /*System.out.println(url);*/
        Image img = Toolkit.getDefaultToolkit().getImage(url);
        this.setIconImage(img);
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        Container contentPane = getContentPane();
        //---- menuBar ----
        chooseMenu.add(reback);
        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);
        menuBar.add(chooseMenu);
        area.setVisible(true);
        area.setEditable(false);
        reFrame.add(area);
        reFrame.setIconImage(img);
        reback.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                reMousePressed(e);
            }
        });
        contentPane.setLayout(new MigLayout(
                "hidemode 3",
                // columns
                "[300,fill]",
                // rows
                "[60]" +
                        "[60]" +
                        "[60]" +
                        "[60]" +
                        "[60]" +
                        "[60]"));
        question.setFont(new Font("宋体",Font.PLAIN,25));
        answer.setFont(new Font("宋体",Font.PLAIN,25));
        answer.setEditable(false);
        contentPane.add(question, "cell 0 0,wmin 300,hmin 70");
        contentPane.add(answer, "cell 0 1,wmin 300,hmin 70");

        //---- seven ----
        seven.setText("7");
        seven.setFocusPainted(false);
        seven.setFont(new Font("宋体",Font.BOLD,30));
        seven.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                sevenMouseClicked(e);
            }
        });
        contentPane.add(seven, "cell 0 2,wmin 60,hmin 60");

        //---- eight ----
        eight.setText("8");
        eight.setFocusPainted(false);
        eight.setFont(new Font("宋体",Font.BOLD,30));
        eight.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                eightMouseClicked(e);
            }
        });
        contentPane.add(eight, "cell 0 2 10 10,wmin 60,hmin 60");

        //---- nine ----
        nine.setText("9");
        nine.setFocusPainted(false);
        nine.setFont(new Font("宋体",Font.BOLD,30));
        nine.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                nineMouseClicked(e);
            }
        });
        contentPane.add(nine, "cell 0 2 10 10,wmin 60,hmin 60");

        //---- back ----
        back.setText("<-");
        back.setFocusPainted(false);
        back.setFont(new Font("宋体",Font.BOLD,30));
        back.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                backMouseClicked(e);
            }
        });
        contentPane.add(back, "cell 0 2 10 10,wmin 60,hmin 60");

        //---- C ----
        C.setText("C");
        C.setFocusPainted(false);
        C.setFont(new Font("宋体",Font.BOLD,30));
        C.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                CMouseClicked(e);
            }
        });
        contentPane.add(C, "cell 0 2 10 10,wmin 60,hmin 60");

        //---- four ----
        four.setText("4");
        four.setFocusPainted(false);
        four.setFont(new Font("宋体",Font.BOLD,30));
        four.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                fourMouseClicked(e);
            }
        });
        contentPane.add(four, "cell 0 3,wmin 60,hmin 60");

        //---- five ----
        five.setText("5");
        five.setFocusPainted(false);
        five.setFont(new Font("宋体",Font.BOLD,30));
        five.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                fiveMouseClicked(e);
            }
        });
        contentPane.add(five, "cell 0 3,wmin 60,hmin 60");

        //---- six ----
        six.setText("6");
        six.setFocusPainted(false);
        six.setFont(new Font("宋体",Font.BOLD,30));
        six.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                sixMouseClicked(e);
            }
        });
        contentPane.add(six, "cell 0 3,wmin 60,hmin 60");

        //---- add ----
        add.setText("+");
        add.setFocusPainted(false);
        add.setFont(new Font("宋体",Font.BOLD,30));
        add.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                addMouseClicked(e);
            }
        });
        contentPane.add(add, "cell 0 3,wmin 60,hmin 60");

        //---- sub ----
        sub.setText("-");
        sub.setFocusPainted(false);
        sub.setFont(new Font("宋体",Font.BOLD,30));
        sub.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                subMouseClicked(e);
            }
        });
        contentPane.add(sub, "cell 0 3,wmin 60,hmin 60");

        //---- one ----
        one.setText("1");
        one.setFocusPainted(false);
        one.setFont(new Font("宋体",Font.BOLD,30));
        one.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                oneMouseClicked(e);
            }
        });
        contentPane.add(one, "cell 0 4,wmin 60,hmin 60");

        //---- two ----
        two.setText("2");
        two.setFocusPainted(false);
        two.setFont(new Font("宋体",Font.BOLD,30));
        two.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                twoMouseClicked(e);
            }
        });
        contentPane.add(two, "cell 0 4,wmin 60,hmin 60");

        //---- three ----
        three.setText("3");
        three.setFocusPainted(false);
        three.setFont(new Font("宋体",Font.BOLD,30));
        three.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                threeMouseClicked(e);
            }
        });
        contentPane.add(three, "cell 0 4,wmin 60,hmin 60");

        //---- mul ----
        mul.setText("*");
        mul.setFocusPainted(false);
        mul.setFont(new Font("宋体",Font.BOLD,30));
        mul.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                mulMouseClicked(e);
            }
        });
        contentPane.add(mul, "cell 0 4,wmin 60,hmin 60");

        //---- div ----
        div.setText("/");
        div.setFocusPainted(false);
        div.setFont(new Font("宋体",Font.BOLD,30));
        div.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                divMouseClicked(e);
            }
        });
        contentPane.add(div, "cell 0 4,wmin 60,hmin 60");

        //---- zero ----
        zero.setText("0");
        zero.setFocusPainted(false);
        zero.setFont(new Font("宋体",Font.BOLD,30));
        zero.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                zeroMouseClicked(e);
            }
        });
        contentPane.add(zero, "cell 0 5,wmin 60,hmin 60");

        //---- dot ----
        dot.setText(".");
        dot.setFocusPainted(false);
        dot.setFont(new Font("宋体",Font.BOLD,30));
        dot.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                dotMouseClicked(e);
            }
        });
        contentPane.add(dot, "cell 0 5,wmin 60,hmin 60");

        //---- lBracket ----
        lBracket.setText("(");
        lBracket.setFocusPainted(false);
        lBracket.setFont(new Font("宋体",Font.BOLD,30));
        lBracket.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                lBracketMouseClicked(e);
            }
        });
        contentPane.add(lBracket, "cell 0 5,wmin 60,hmin 60");

        //---- rBracket ----
        rBracket.setText(")");
        rBracket.setFocusPainted(false);
        rBracket.setFont(new Font("宋体",Font.BOLD,30));
        rBracket.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                rBracketMouseClicked(e);
            }
        });
        contentPane.add(rBracket, "cell 0 5,wmin 60,hmin 60");

        //---- equ ----
        equ.setText("=");
        equ.setFocusPainted(false);
        equ.setFont(new Font("宋体",Font.BOLD,30));
        equ.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                equMouseClicked(e);
            }
        });
        contentPane.add(equ, "cell 0 5,wmin 60,hmin 60");
        pack();
        setLocationRelativeTo(getOwner());

        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JTextField question;
    private JTextField answer;
    private JButton seven;
    private JButton eight;
    private JButton nine;
    private JButton back;
    private JButton C;
    private JButton four;
    private JButton five;
    private JButton six;
    private JButton add;
    private JButton sub;
    private JButton one;
    private JButton two;
    private JButton three;
    private JButton mul;
    private JButton div;
    private JButton zero;
    private JButton dot;
    private JButton lBracket;
    private JButton rBracket;
    private JButton equ;
    private JMenu chooseMenu;
    private JMenuItem reback;
    private JTextArea area;
    private JLabel lb;
    private JFrame reFrame;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
    public static void main(String[] args) {
        Calculator calculator = new Calculator();
        /*Image frame_icon = Toolkit.getDefaultToolkit().createImage((".\\calpic.png"));
        calculator.setIconImage(frame_icon);*/
        calculator.setSize(435,600);
        calculator.setResizable(false);
        calculator.setVisible(true);
    }
}

本文地址:https://blog.csdn.net/qq_43632370/article/details/110633505

相关标签: 学习java java