1. 课程目的
    1.1. 课程性质
    JAVA 程序设计是计算机专业本科生的必修专业主干课程,授课对象为计算
    机科学与技术专业、数字媒体技术、信息安全专业等相关专业的本科生。
    课程全面、系统地介绍 JAVA 语言的基本知识及程序设计技术,使学生掌握
    Java 语言的语法、数据类型、流程控制等基本知识和面向对象程序设计思想的
    Java 实现;了解 Java 常用的系统类;学习异常处理、线程、图形用户界面设计、
    网络通信等内容。使学生的专业知识进一步完善和丰富,为将来的开发和研究工
    作打下一定的基础。
    1.2. 课程任务
    (1) 巩固和加深学生对 Java 语言课程的基本知识的理解和掌握;
    (2) 掌握 Java 语言编程和程序调试的基本技能;
    (3) 利用 Java 语言进行基本的程序设计;
    (4) 掌握书写程序设计说明文档的能力;
    (5) 提高运用 Java 语言解决实际问题的能力。
  2. 课程设计要求
    编写一个计算器,可实现加减乘除等一系列运算。
    编程要求:选择某一种运算后,根据输入的数据给出运算结果。
    编程提示:在设计出的计算器界面上,通过按钮来实现基本的功能。
  3. 课程设计内容 3.1. 选题描述 使用图形用户界面,设计计算器,用户通过鼠标依次输入需要计算的数值, 点击相应计算功能按钮,可以完成以下功能:

java中gui程序设计 java语言gui程序设计_java

3.2. 画出流程图

(1) 核心功能流程图

java中gui程序设计 java语言gui程序设计_java计算器_02

3.3. 题目设计 (代码)
public class Calculator extends MouseAdapter {
    JFrame list;
    // Container con;
    JTextField show;
    JButton[] jbNum = new JButton[10];
    JPanel jpMain; // 主面板
    JPanel jpRight; // 右子面板主要用于存放运算符和等号
    JPanel jpLight; // 左子面板用于存放数字,符号, “.”
    JButton dight; // 小数点
    JButton sign; // 正负号
    JButton add; // 加号
    JButton sub; // 减号
    JButton multiply; // 乘号
    JButton divide; // 除号
    JButton power; // 求幂
    JButton cos; // cos
    JButton sin; // sin
    JButton ln; // ln
    JButton ce; // 清除
    JButton equal; // 等于
    JButton mod; // 取余
    JButton sqrt; // sqrt
    double sum = 0; // 临时结果
    boolean b = false; // 监控运算符是否被点击,错误是否出现,用于实现下一次点击按钮时清空
    operator i = operator.un; // 记录等号符点击前某一运算符点击次数,用于实现连加或者连减等

    int op; // 记录操作符

// 操作符一包括+-*/%^
    enum operator {
        add, sub, mul, div, mod, pow, sin, cos, sqrt, ln, un
    }

    void display() {
        // 创建主窗口,添加一个Text框,
        list = new JFrame("计算器");
        list.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        list.setSize(360, 230);
        list.setLocation(400, 300);
        list.setBackground(Color.LIGHT_GRAY); // 设置窗口背景颜色
        list.setResizable(false);

        list.setLayout(new FlowLayout(FlowLayout.CENTER));
        show = new JTextField(31);
        show.setHorizontalAlignment(JTextField.RIGHT); // 文本框内文字右对齐
        show.setEditable(false); // 文本框不可编辑
        list.add(show);
        // 创建面板并设置布局
        jpMain = new JPanel();
        jpRight = new JPanel();
        jpLight = new JPanel();
        jpMain.setLayout(new GridLayout(1, 2));
        jpRight.setLayout(new GridLayout(4, 3, 3, 3));
        jpLight.setLayout(new GridLayout(4, 3, 3, 3));
        list.add(jpMain);
        jpMain.add(jpLight);
        jpMain.add(jpRight);
        // 创建0~9按钮对象
        for (int i = 9; i >= 0; i--) {
            jbNum[i] = new JButton(String.valueOf(i));
            jbNum[i].setForeground(Color.BLUE);
            jpLight.add(jbNum[i]);
            jbNum[i].addMouseListener(this);
        }
        add = new JButton("+");
        sub = new JButton("-");
        multiply = new JButton("*");
        divide = new JButton("/");
        power = new JButton("x^y");
        sin = new JButton("sin");
        cos = new JButton("cos");
        ln = new JButton("ln");
        ce = new JButton("CE");
        equal = new JButton("=");
        mod = new JButton("%");
        sqrt = new JButton("sqrt");
        jpRight.add(divide);
        jpRight.add(sqrt);
        jpRight.add(ln);
        jpRight.add(multiply);
        jpRight.add(sin);
        jpRight.add(mod);
        jpRight.add(sub);
        jpRight.add(cos);
        jpRight.add(ce);
        jpRight.add(add);
        jpRight.add(power);
        jpRight.add(equal);

        // 给所有按钮注册监听器
        dight = new JButton(".");
        sign = new JButton("±");
        jpLight.add(sign);
        jpLight.add(dight);
        add.addMouseListener(this);
        sub.addMouseListener(this);
        multiply.addMouseListener(this);
        divide.addMouseListener(this);
        power.addMouseListener(this);
        sin.addMouseListener(this);
        cos.addMouseListener(this);
        ln.addMouseListener(this);
        ce.addMouseListener(this);
        equal.addMouseListener(this);
        mod.addMouseListener(this);
        sqrt.addMouseListener(this);
        dight.addMouseListener(this);
        sign.addMouseListener(this);
        list.setVisible(true);

    }

    public void mouseClicked(MouseEvent e) {
        // 0~9的输入
        if (e.getSource() == jbNum[0]) {
            input(0, e);
        }
        if (e.getSource() == jbNum[1]) {
            input(1, e);
        }
        if (e.getSource() == jbNum[2]) {
            input(2, e);
        }
        if (e.getSource() == jbNum[3]) {
            input(3, e);
        }
        if (e.getSource() == jbNum[4]) {
            input(4, e);
        }
        if (e.getSource() == jbNum[5]) {
            input(5, e);
        }
        if (e.getSource() == jbNum[6]) {
            input(6, e);
        }
        if (e.getSource() == jbNum[7]) {
            input(7, e);
        }
        if (e.getSource() == jbNum[8]) {
            input(8, e);
        }
        if (e.getSource() == jbNum[9]) {
            input(9, e);
        }

        // 小数点,正负号,CE,等号
        if (e.getSource() == dight) {
            if (show.getText().indexOf('.') == -1) {
                show.setText(show.getText() + ".");
            }

        }
        if (e.getSource() == sign) {
            if (show.getText().indexOf("-") == -1) {
                show.setText("-" + show.getText());
            } else {
                show.setText(show.getText().replace('-', '\0'));
            }

        }
        if (e.getSource() == ce) {
            show.setText("0");
            sum = 0;
            i = operator.un;
            b = false;
        }
        outer: if (e.getSource() == equal) {
            try {
                if (i == operator.un) {
                    b = true;
                } else {
                    if (i == operator.add) {
                        sum += Double.parseDouble(show.getText());

                    }
                    if (i == operator.sub) {
                        sum -= Double.parseDouble(show.getText());

                    }
                    if (i == operator.mul) {
                        sum *= Double.parseDouble(show.getText());

                    }
                    if (i == operator.div) {
                        if (Double.parseDouble(show.getText()) != 0) {
                            sum /= Double.parseDouble(show.getText());

                        } else {
                            show.setText("ERROR");
                            b = true;
                            sum = 0;
                            break outer; // 不执行trimIn()方法 屏幕显示错误
                        }
                    }
                    if (i == operator.mod) {
                        sum %= Double.parseDouble(show.getText());

                    }
                    if (i == operator.pow) {
                        sum = Math.pow(sum, Double.parseDouble(show.getText()));

                    }
                    trimIn(sum);
                }
            } catch (Exception ex) {
                show.setText("ERROR");
                b = true;
                sum = 0;
            }

            sum = 0;
            i = operator.un;
            b = true;
        }
        // 加减乘除//幂指函数//取余
        if (e.getSource() == add) {
            cal(i);
            i = operator.add;
            b = true;

        }
        if (e.getSource() == sub) {
            cal(i);
            i = operator.sub;
            b = true;

        }
        if (e.getSource() == multiply) {
            cal(i);
            i = operator.mul;
            b = true;

        }
        if (e.getSource() == divide) {
            cal(i);
            i = operator.div;
            b = true;

        }
        if (e.getSource() == mod) {
            cal(i);
            i = operator.mod;
            b = true;

        }
        if (e.getSource() == power) {
            cal(i);
            i = operator.pow;
            b = true;

        }

        // sqrt,sin,cos,ln
        try {
            if (show.getText() != "ERROR") {
                if (e.getSource() == sqrt) {
                    sum = Math.sqrt(Double.parseDouble(show.getText()));
                    trimIn(sum);
                    b = true;
                }
                if (e.getSource() == sin) {
                    sum = Math.sin(Double.parseDouble(show.getText()));
                    trimIn(sum);
                    b = true;
                }
                if (e.getSource() == cos) {
                    sum = Math.cos(Double.parseDouble(show.getText()));
                    trimIn(sum);
                    b = true;
                }
                if (e.getSource() == ln) {
                    sum = Math.log(Double.parseDouble(show.getText()));
                    trimIn(sum);
                    b = true;
                }
            }
        } catch (Exception ex) {
            show.setText("ERROR");
            b = true;
        }
    }

3.4. 运行程序(结果截图)

(1) 运行界面

java中gui程序设计 java语言gui程序设计_java中gui程序设计_03

(2)加减乘除(±*/)

java中gui程序设计 java语言gui程序设计_gui计算器_04

(3)清除运算(CE)

java中gui程序设计 java语言gui程序设计_gui计算器_05

(4)开平方根(sqrt)

java中gui程序设计 java语言gui程序设计_java计算器_06

(5)三角函数(sin\cos)

java中gui程序设计 java语言gui程序设计_gui计算器_07

  1. 总结
    该文章分析了一个系统设计与实现,实际上就是让我们建立一个技术体现出来,最主要是要明白自己开发的这个程序它到底有哪些功能,这都是需要我们一开始就需要思考并且根据流程图来设计的。
    为此,从题目上来讲,这个系统是需要JAVA一定基础及图形化界面的知识,但仅靠这两个是不行的,中间会运用到网页相关模块。而且开发这个系统我也需要去思考这个开发环境和开发的工具,我在做这个项目的时候,中途很多不会的技术需要自己去学习,比如运算符的实现,界面api函数的调用,都需要解决,经过网上资料的查阅,及同学的帮助。最终设计出一个比较完整的图形化GUI计算器系统。
  2. 参考文献
    [1]张墨华,张永强.Java程序设计[M].北京:清华大学出版社,.2021:123-124.
    [2]贾素玲等编译.J2EE技术实践[M]. 高等教育出版社. 2018.
    [3]柳永坡,刘雪梅,赵长海.JSP应用开发技术[M].北京:人民邮电出版, 2019.
    [4]林帅,林雄.Java泛型研究[J]. 电脑开发与应用. 2018(03).