🎯功能简介:
这是一个简单的计算器小程序,可以进行加、减、乘、除四则运算。界面分为一个屏幕和多个按钮,通过按钮输入数字和运算符,同时在屏幕上显示输入内容和计算结果。具体实现包括:
- 界面布局:使用GridLayout布局,将界面分为6个部分,分别放置屏幕和按钮。
- 数字输入:通过按钮监听,每次点击数字按钮将数字添加到字符串s中,并在屏幕上显示当前输入内容。
- 运算符输入:同样通过按钮监听,点击运算符按钮时保存当前输入的数字,清空输入框,将运算符保存在flag中。
- 计算结果:在等号按钮监听中,根据flag值选择进行加、减、乘、除四则运算,并将结果显示在屏幕上。其中,除法运算需要特判被除数为0的情况。
🎯分块分析:
💎导入包和声明全局变量:
package one; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; public class MyCalculator1 { static int num1 = 0; static int num2 = 0; static String s = ""; static String flag = "";
在这一部分中,我们导入了所需的包,并声明了四个静态变量 num1
、num2
、s
和 flag
。
💎创建界面并添加组件:
public static void main(String[] args) { // TODO Auto-generated method stub JFrame win1 = new JFrame("计算器小程序"); win1.setBounds(200, 200, 500, 700); win1.setDefaultCloseOperation(3); win1.setVisible(true); win1.setLayout(new GridLayout(6, 1)); JPanel p1 = new JPanel(new GridLayout(1, 1));// 放屏幕的; JPanel p2 = new JPanel(new GridLayout(1, 4));// 放+-*/Button的; JPanel p3 = new JPanel(new GridLayout(1, 3));// 放789Button的; JPanel p4 = new JPanel(new GridLayout(1, 3));// 放456Button的; JPanel p5 = new JPanel(new GridLayout(1, 3));// 放123Button的; JPanel p6 = new JPanel(new GridLayout(1, 3));// 放C0=的; JTextField text = new JTextField(); JButton bt_add = new JButton("+"); JButton bt_sub = new JButton("-"); JButton bt_mul = new JButton("×"); JButton bt_div = new JButton("÷"); JButton bt_7 = new JButton("7"); JButton bt_8 = new JButton("8"); JButton bt_9 = new JButton("9"); JButton bt_4 = new JButton("4"); JButton bt_5 = new JButton("5"); JButton bt_6 = new JButton("6"); JButton bt_1 = new JButton("1"); JButton bt_2 = new JButton("2"); JButton bt_3 = new JButton("3"); JButton bt_c = new JButton("c"); JButton bt_0 = new JButton("0"); JButton bt_equ = new JButton("="); win1.add(p1); win1.add(p2); win1.add(p3); win1.add(p4); win1.add(p5); win1.add(p6); p1.add(text); p2.add(bt_add); p2.add(bt_sub); p2.add(bt_mul); p2.add(bt_div); p3.add(bt_7); p3.add(bt_8); p3.add(bt_9); p4.add(bt_4); p4.add(bt_5); p4.add(bt_6); p5.add(bt_1); p5.add(bt_2); p5.add(bt_3); p6.add(bt_c); p6.add(bt_0); p6.add(bt_equ);
在这一部分中,我们创建了一个 JFrame 窗口,并且设置了窗口的基本属性(标题、大小、位置、关闭方式等)。然后,我们使用 GridLayout 布局管理器创建了六个面板,并将它们添加到窗口中。接着,我们创建了所需的组件(屏幕、按钮),并将它们添加到对应的面板中。
💎添加事件监听器:
bt_0.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub s = s + "0"; text.setText(s); } }); bt_1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub s = s + "1"; text.setText(s); } }); ... bt_add.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub flag = "+"; num1 = Integer.valueOf(text.getText()); s = ""; text.setText(""); } }); ... bt_equ.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub int sum = 0; num2 = Integer.valueOf(text.getText()); switch (flag) { case "+": sum = num1 + num2; break; case "-": sum = num1 - num2; break; case "×": sum = num1 * num2; break; case "÷": if (num2 == 0) { num2 = num2 + 1; } sum = num1 / num2; break; default: break; } String value = String.valueOf(num1) + " " + flag + " " + String.valueOf(num2) + " = " + String.valueOf(sum); text.setText(String.valueOf(value)); } });
在这一部分中,我们为每个按钮添加了事件监听器,并实现了响应的功能。例如,当用户点击数字按钮时,我们将数字添加到字符串变量 s 中,并将其显示在屏幕上;当用户点击加号按钮时,我们将其作为标志变量 flag 的值,并将屏幕上的数字赋给变量 num1。
💎整合逻辑:
int sum = 0; num2 = Integer.valueOf(text.getText()); switch (flag) { case "+": sum = num1 + num2; break; case "-": sum = num1 - num2; break; case "×": sum = num1 * num2; break; case "÷": if (num2 == 0) { num2 = num2 + 1; } sum = num1 / num2; break; default: break; } String value = String.valueOf(num1) + " " + flag + " " + String.valueOf(num2) + " = " + String.valueOf(sum); text.setText(String.valueOf(value));
在这一部分中,我们处理了用户点击等号按钮后的逻辑。我们根据标志变量 flag
的值,对两个数字进行相应的运算,并将结果显示在屏幕上。
💎程序不足:
1.没有对用户输入进行验证:程序没有对用户输入进行验证,如果用户输入非数字字符或者特殊字符,可能会导致程序崩溃或输出错误结果。
2.不支持小数运算:程序只支持整数运算,无法处理带小数点的数字。
3.操作顺序限制:该计算器只支持两个操作数的连续运算,不能处理多个操作符连续操作的情况。
4.缺乏界面美化:界面设计比较简单,缺乏美观性和用户友好性。
5.代码结构不清晰:代码中存在大量的重复代码,可读性和维护性较差。
6.未处理除数为0的情况:虽然在除法运算时判断了除数是否为0,但只是简单地将除数加1而没有给出合适的提示。
🎯整改建议:
- 添加输入验证逻辑,确保用户输入的有效性。
- 添加异常处理机制,处理可能发生的异常情况。
- 重新设计代码结构,使用实例变量和方法来组织代码。
- 调整界面布局,使按钮的大小和位置更合理美观。
🎯源代码:
package one; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; public class MyCalculator1 { static int num1 = 0; static int num2 = 0; static String s = ""; static String flag = ""; public static void main(String[] args) { // TODO Auto-generated method stub JFrame win1 = new JFrame("计算器小程序"); win1.setBounds(200, 200, 500, 700); win1.setDefaultCloseOperation(3); win1.setVisible(true); win1.setLayout(new GridLayout(6, 1)); JPanel p1 = new JPanel(new GridLayout(1, 1));// 放屏幕的; JPanel p2 = new JPanel(new GridLayout(1, 4));// 放+-*/Button的; JPanel p3 = new JPanel(new GridLayout(1, 3));// 放789Button的; JPanel p4 = new JPanel(new GridLayout(1, 3));// 放456Button的; JPanel p5 = new JPanel(new GridLayout(1, 3));// 放123Button的; JPanel p6 = new JPanel(new GridLayout(1, 3));// 放C0=的; JTextField text = new JTextField(); JButton bt_add = new JButton("+"); JButton bt_sub = new JButton("-"); JButton bt_mul = new JButton("×"); JButton bt_div = new JButton("÷"); JButton bt_7 = new JButton("7"); JButton bt_8 = new JButton("8"); JButton bt_9 = new JButton("9"); JButton bt_4 = new JButton("4"); JButton bt_5 = new JButton("5"); JButton bt_6 = new JButton("6"); JButton bt_1 = new JButton("1"); JButton bt_2 = new JButton("2"); JButton bt_3 = new JButton("3"); JButton bt_c = new JButton("c"); JButton bt_0 = new JButton("0"); JButton bt_equ = new JButton("="); win1.add(p1); win1.add(p2); win1.add(p3); win1.add(p4); win1.add(p5); win1.add(p6); p1.add(text); p2.add(bt_add); p2.add(bt_sub); p2.add(bt_mul); p2.add(bt_div); p3.add(bt_7); p3.add(bt_8); p3.add(bt_9); p4.add(bt_4); p4.add(bt_5); p4.add(bt_6); p5.add(bt_1); p5.add(bt_2); p5.add(bt_3); p6.add(bt_c); p6.add(bt_0); p6.add(bt_equ); bt_0.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub s = s + "0"; text.setText(s); } }); bt_1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub s = s + "1"; text.setText(s); } }); bt_2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub s = s + "2"; text.setText(s); } }); bt_3.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub s = s + "3"; text.setText(s); } }); bt_4.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub s = s + "4"; text.setText(s); } }); bt_5.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub s = s + "5"; text.setText(s); } }); bt_6.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub s = s + "6"; text.setText(s); } }); bt_7.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub s = s + "7"; text.setText(s); } }); bt_8.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub s = s + "8"; text.setText(s); } }); bt_9.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub s = s + "9"; text.setText(s); } }); bt_c.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub num1 = 0; num2 = 0; flag = ""; s = ""; text.setText(""); } }); bt_add.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub flag = "+"; num1 = Integer.valueOf(text.getText()); s = ""; text.setText(""); } }); bt_sub.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub flag = "-"; num1 = Integer.valueOf(text.getText()); s = ""; text.setText(""); } }); bt_mul.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub flag = "×"; num1 = Integer.valueOf(text.getText()); s = ""; text.setText(""); } }); bt_div.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub flag = "÷"; num1 = Integer.valueOf(text.getText()); s = ""; text.setText(""); } }); bt_equ.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub int sum = 0; num2 = Integer.valueOf(text.getText()); switch (flag) { case "+": sum = num1 + num2; break; case "-": sum = num1 - num2; break; case "×": sum = num1 * num2; break; case "÷": if (num2 == 0) { num2 = num2 + 1; } sum = num1 / num2; break; default: break; } String value = String.valueOf(num1) + " " + flag + " " + String.valueOf(num2) + " = " + String.valueOf(sum); text.setText(String.valueOf(value)); } }); } }
🎯展示视频:
【Java】——实现可视化加减乘除计算器