java-计算器模板及源码

java-计算器模板及源码java-计算器模板及源码计算器实现了大部分基础功能:基本运算,菜单栏选项,并且拓展了普通型和科学兴选项等等,读者可以在此基础上进行修改和拓展。其他具体实现方法可以看源码,里面有详细的概述,代码框架清晰。读者在阅读和引用过程中,如有问题欢迎评论区留言和私信交流。运行环境:win10EclipseIDEforJavaDevelopers-2020-06下面是计算器的视图:importjava.awt.*;importjava.awt.event.ActionEvent;im

大家好,又见面了,我是你们的朋友全栈君。

java-计算器模板及源码

计算器实现了大部分基础功能:基本运算,菜单栏选项,并且拓展了普通型和科学兴选项等等,读者可以在此基础上进行修改和拓展。其他具体实现方法可以看源码,里面有详细的概述,代码框架清晰。

读者在阅读和引用过程中,如有问题欢迎评论区留言和私信交流。

运行环境:win10 Eclipse IDE for Java Developers – 2020-06

下面是计算器的视图:

在这里插入图片描述

在这里插入图片描述

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

/* * 计算器 */
public class CaculatorTest implements ActionListener { 
   
	// 初始框架搭建
	JFrame frame = new JFrame("计算器");
	JTextField area = new JTextField("0");
	JPanel panel1 = new JPanel();
	JPanel panel2 = new JPanel();
	JButton[] buttons = new JButton[20];
	String[] buttonsText = { 
    "sqrt", "退格", "C", "/", "7", "8", "9", "*", "4", "5", "6", "-", "1", "2", "3", "+", "0",
			".", "+/-", "=" };
	boolean point = false; // 用于判断是否输入多位小数点
	boolean key = true; // 做完运算("=")后继续输入数字
	String sign = " "; // 用于判断和记录运算符号
	double temp = 0; // 多次连续运算时,值的寄存处

	public CaculatorTest() { 
   
		initMenu();
		initText();
		initExtend();
		initFrame();
		initBorderLayout();
	}

	// 初始化菜单
	private void initMenu() { 
   
		JMenuBar mb = new JMenuBar();
		JMenu m1 = new JMenu("选项");
		JMenu m2 = new JMenu("编辑");
		JMenu m3 = new JMenu("帮助");
		JMenuItem m11 = new JMenuItem("普通型计算器");
		JMenuItem m12 = new JMenuItem("科学型计算器");
		m1.add(m11);
		m1.add(m12);
		m11.addActionListener(new ActionListener() { 
   
			@Override
			public void actionPerformed(ActionEvent e) { 
   
				boolean flag = false;
				panel2.setVisible(flag);
				frame.pack();
			}
		});
		m12.addActionListener(new ActionListener() { 
   
			@Override
			public void actionPerformed(ActionEvent e) { 
   
				boolean flag = true;
				panel2.setVisible(flag);
				frame.pack();
			}
		});
		mb.add(m1);
		mb.add(m2);
		mb.add(m3);
		frame.setJMenuBar(mb);
	}

	// 初始化输出文本域
	private void initText() { 
   
		area.setFont(new Font("TimesRoman", Font.PLAIN, 20));
		area.setSize(400, 100);
		area.setHorizontalAlignment(JTextField.RIGHT); // 向右显示
	}

	// 初始化拓展功能
	private void initExtend() { 
   
		panel2.setLayout(new GridLayout(1,4,1,1));
		JButton b1 = new JButton("sin");
		JButton b2 = new JButton("cos");
		JButton b3 = new JButton("exp");
		JButton b4 = new JButton("ln");
		b1.setFont(new Font("TimesRoman", Font.PLAIN, 20));
		b2.setFont(new Font("TimesRoman", Font.PLAIN, 20));
		b3.setFont(new Font("TimesRoman", Font.PLAIN, 20));
		b4.setFont(new Font("TimesRoman", Font.PLAIN, 20));
		b1.setSize(100, 100);
		b1.addActionListener(this);
		b2.setSize(100, 100);
		b2.addActionListener(this);
		b3.setSize(100, 100);
		b3.addActionListener(this);
		b4.setSize(100, 100);
		b4.addActionListener(this);
		panel2.add(b1);
		panel2.add(b2);
		panel2.add(b3);
		panel2.add(b4);
	}

	// 初始化计算器基本界面
	private void initFrame() { 
   
		panel1.setLayout(new GridLayout(5, 4, 1, 1));
		for (int i = 0; i < buttonsText.length; i++) { 
   
			JButton button = new JButton(buttonsText[i]);
			button.setSize(100, 100);
			button.setFont(new Font("TimesRoman", Font.PLAIN, 20));
			button.addActionListener(this);
			panel1.add(button);
		}
	}

	// 初始化计算器总基本界面
	private void initBorderLayout() { 
   
		frame.setLayout(new BorderLayout());
		frame.add(panel1, BorderLayout.SOUTH); // 插入组件
		frame.add(area, BorderLayout.NORTH);
		frame.add(panel2, BorderLayout.CENTER);
		frame.setLocation(700, 400);
		frame.setSize(400, 700);
		frame.setVisible(true); // 设置可见
		panel2.setVisible(false);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 可以关闭
		frame.pack();
	}

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

	@Override
	// 事件监听
	public void actionPerformed(ActionEvent e) { 
   
		String str = e.getActionCommand();
		String str2 = area.getText();
		if (str == "0" || str == "1" || str == "2" || str == "3" || str == "4" || str == "5" || str == "6" || str == "7"
				|| str == "8" || str == "9") { 
   
			if (key == false) { 
   
				area.setText(str2 + str);
			} else { 
   
				area.setText(str);
				key = false;
			}
		} else if (str == "C") { 
   
			area.setText("0");
			sign = " ";
			key = true;
		} else if (str == ".") { 
   
			if (point == false) { 
   
				area.setText(str2 + str);
				point = true;
			} else { 
   
				area.setText("double poits!press C to update!");
				point = false;
			}
		} else if (str == "+/-") { 
   
			double num = Double.valueOf(str2);
			num = -num;
			area.setText(String.valueOf(num));
		} else if (str == "退格") { 
   
			if (str2.length() == 0) { 
   
				area.setText("can't be deleted!please press C!");
			} else { 
   
				str2 = str2.substring(0, str2.length() - 1);
				area.setText(str2);
			}
		} else if (str == "sqrt") { 
   
			area.setText("");
			sign = "s";
		} else if (str == "sin") { 
   
			area.setText("");
			sign = "sin";
		} else if (str == "cos") { 
   
			area.setText("");
			sign = "cos";
		} else if (str == "exp") { 
   
			area.setText("");
			sign = "exp";
		} else if (str == "ln") { 
   
			area.setText("");
			sign = "ln";
		} else { 
   
			if (str == "+") { 
   
				if (sign == " ") { 
   
					sign = "+";
					temp = Double.valueOf(str2);
					area.setText("");
				} else if (sign == "-") { 
   
					if (str2.length() == 0) { 
   
						sign = "+";
					} else { 
   
						temp = temp - Double.valueOf(str2);
						sign = "+";
						area.setText("");
						key = true;
					}
				} else if (sign == "+") { 
   
					if (str2.length() == 0) { 
   
						sign = "+";
					} else { 
   
						temp = temp + Double.valueOf(str2);
						sign = "+";
						area.setText("");
						key = true;
					}
				} else if (sign == "*") { 
   
					if (str2.length() == 0) { 
   
						sign = "+";
					} else { 
   
						temp = temp * Double.valueOf(str2);
						sign = "+";
						area.setText("");
						key = true;
					}
				} else if (sign == "/") { 
   
					if (str2.length() == 0) { 
   
						sign = "+";
					} else if (Double.valueOf(str2) == 0) { 
   
						area.setText("除数不能为0哦!按 C");
					} else { 
   
						temp = temp / Double.valueOf(str2);
						area.setText("");
						sign = "+";
						key = true;
					}
				} else if (sign == "s") { 
   
					if (str2.length() == 0) { 
   
						sign = "+";
					} else { 
   
						temp = Math.sqrt(Double.valueOf(str2));
						area.setText("");
						sign = "+";
					}
				} else if (sign == "sin") { 
   
					if (str2.length() == 0) { 
   
						sign = "+";
					} else { 
   
						temp = Math.sin(Double.valueOf(str2));
						area.setText("");
						sign = "+";
					}
				} else if (sign == "cos") { 
   
					if (str2.length() == 0) { 
   
						sign = "+";
					} else { 
   
						temp = Math.cos(Double.valueOf(str2));
						area.setText("");
						sign = "+";
					}
				} else if (sign == "exp") { 
   
					if (str2.length() == 0) { 
   
						sign = "+";
					} else { 
   
						temp = Math.exp(Double.valueOf(str2));
						area.setText("");
						sign = "+";
					}
				} else if (sign == "ln") { 
   
					if (str2.length() == 0) { 
   
						sign = "+";
					} else { 
   
						temp = Math.log(Double.valueOf(str2));
						area.setText("");
						sign = "+";
					}
				}
			} else if (str == "-") { 
   
				if (sign == " ") { 
   
					sign = "-";
					temp = Double.valueOf(str2);
					area.setText("");
				} else if (sign == "-") { 
   
					if (str2.length() == 0) { 
   
						sign = "-";
					} else { 
   
						temp = temp - Double.valueOf(str2);
						sign = "-";
						area.setText("");
						key = true;
					}
				} else if (sign == "+") { 
   
					if (str2.length() == 0) { 
   
						sign = "-";
					} else { 
   
						temp = temp + Double.valueOf(str2);
						sign = "-";
						area.setText("");
						key = true;
					}
				} else if (sign == "*") { 
   
					if (str2.length() == 0) { 
   
						sign = "-";
					} else { 
   
						temp = temp * Double.valueOf(str2);
						sign = "-";
						area.setText("");
						key = true;
					}
				} else if (sign == "/") { 
   
					if (str2.length() == 0) { 
   
						sign = "-";
					} else if (Double.valueOf(str2) == 0) { 
   
						area.setText("除数不能为0哦!按 C");
					} else { 
   
						temp = temp / Double.valueOf(str2);
						area.setText("");
						sign = "-";
						key = true;
					}
				} else if (sign == "s") { 
   
					if (str2.length() == 0) { 
   
						sign = "-";
					} else { 
   
						temp = Math.sqrt(Double.valueOf(str2));
						area.setText("");
						sign = "-";
					}
				} else if (sign == "sin") { 
   
					if (str2.length() == 0) { 
   
						sign = "-";
					} else { 
   
						temp = Math.sin(Double.valueOf(str2));
						area.setText("");
						sign = "-";
					}
				} else if (sign == "cos") { 
   
					if (str2.length() == 0) { 
   
						sign = "-";
					} else { 
   
						temp = Math.cos(Double.valueOf(str2));
						area.setText("");
						sign = "-";
					}
				} else if (sign == "exp") { 
   
					if (str2.length() == 0) { 
   
						sign = "-";
					} else { 
   
						temp = Math.exp(Double.valueOf(str2));
						area.setText("");
						sign = "-";
					}
				} else if (sign == "ln") { 
   
					if (str2.length() == 0) { 
   
						sign = "-";
					} else { 
   
						temp = Math.log(Double.valueOf(str2));
						area.setText("");
						sign = "-";
					}
				}
			} else if (str == "*") { 
   
				if (sign == " ") { 
   
					sign = "*";
					temp = Double.valueOf(str2);
					area.setText("");
				} else if (sign == "-") { 
   
					if (str2.length() == 0) { 
   
						sign = "*";
					} else { 
   
						temp = temp - Double.valueOf(str2);
						sign = "*";
						area.setText("");
						key = true;
					}
				} else if (sign == "+") { 
   
					if (str2.length() == 0) { 
   
						sign = "*";
					} else { 
   
						temp = temp + Double.valueOf(str2);
						sign = "*";
						area.setText("");
						key = true;
					}
				} else if (sign == "*") { 
   
					if (str2.length() == 0) { 
   
						sign = "*";
					} else { 
   
						temp = temp * Double.valueOf(str2);
						sign = "*";
						area.setText("");
						key = true;
					}
				} else if (sign == "/") { 
   
					if (str2.length() == 0) { 
   
						sign = "*";
					} else if (Double.valueOf(str2) == 0) { 
   
						area.setText("除数不能为0哦!按 C");
					} else { 
   
						temp = temp / Double.valueOf(str2);
						area.setText("");
						sign = "*";
						key = true;
					}
				} else if (sign == "s") { 
   
					if (str2.length() == 0) { 
   
						sign = "*";
					} else { 
   
						temp = Math.sqrt(Double.valueOf(str2));
						area.setText("");
						sign = "*";
					}
				} else if (sign == "sin") { 
   
					if (str2.length() == 0) { 
   
						sign = "*";
					} else { 
   
						temp = Math.sin(Double.valueOf(str2));
						area.setText("");
						sign = "*";
					}
				} else if (sign == "cos") { 
   
					if (str2.length() == 0) { 
   
						sign = "*";
					} else { 
   
						temp = Math.cos(Double.valueOf(str2));
						area.setText("");
						sign = "*";
					}
				} else if (sign == "exp") { 
   
					if (str2.length() == 0) { 
   
						sign = "*";
					} else { 
   
						temp = Math.exp(Double.valueOf(str2));
						area.setText("");
						sign = "*";
					}
				} else if (sign == "ln") { 
   
					if (str2.length() == 0) { 
   
						sign = "*";
					} else { 
   
						temp = Math.log(Double.valueOf(str2));
						area.setText("");
						sign = "*";
					}
				}
			} else if (str == "/") { 
   
				if (sign == " ") { 
   
					sign = "/";
					temp = Double.valueOf(str2);
					area.setText("");
				} else if (sign == "-") { 
   
					if (str2.length() == 0) { 
   
						sign = "/";
					} else { 
   
						temp = temp - Double.valueOf(str2);
						sign = "/";
						area.setText("");
						key = true;
					}
				} else if (sign == "+") { 
   
					if (str2.length() == 0) { 
   
						sign = "/";
					} else { 
   
						temp = temp + Double.valueOf(str2);
						sign = "/";
						area.setText("");
						key = true;
					}
				} else if (sign == "*") { 
   
					if (str2.length() == 0) { 
   
						sign = "/";
					} else { 
   
						temp = temp * Double.valueOf(str2);
						sign = "/";
						area.setText("");
						key = true;
					}
				} else if (sign == "/") { 
   
					if (str2.length() == 0) { 
   
						sign = "/";
					} else if (Double.valueOf(str2) == 0) { 
   
						area.setText("除数不能为0哦!按 C");
					} else { 
   
						temp = temp / Double.valueOf(str2);
						area.setText("");
						sign = "/";
						key = true;
					}
				} else if (sign == "s") { 
   
					if (str2.length() == 0) { 
   
						sign = "/";
					} else { 
   
						temp = Math.sqrt(Double.valueOf(str2));
						area.setText("");
						sign = "/";
					}
				} else if (sign == "sin") { 
   
					if (str2.length() == 0) { 
   
						sign = "/";
					} else { 
   
						temp = Math.sin(Double.valueOf(str2));
						area.setText("");
						sign = "/";
					}
				} else if (sign == "cos") { 
   
					if (str2.length() == 0) { 
   
						sign = "/";
					} else { 
   
						temp = Math.cos(Double.valueOf(str2));
						area.setText("");
						sign = "/";
					}
				} else if (sign == "exp") { 
   
					if (str2.length() == 0) { 
   
						sign = "/";
					} else { 
   
						temp = Math.exp(Double.valueOf(str2));
						area.setText("");
						sign = "/";
					}
				} else if (sign == "ln") { 
   
					if (str2.length() == 0) { 
   
						sign = "/";
					} else { 
   
						temp = Math.log(Double.valueOf(str2));
						area.setText("");
						sign = "/";
					}
				}
			} else if (str == "=") { 
   
				if (sign == "+") { 
   
					if (str2.length() == 0) { 
   
						area.setText(String.valueOf(temp));
						sign = " ";
					} else { 
   
						temp = temp + Double.valueOf(str2);
						area.setText(String.valueOf(temp));
						sign = " ";
					}
				} else if (sign == "-") { 
   
					if (str2.length() == 0) { 
   
						area.setText(String.valueOf(temp));
						sign = " ";
					} else { 
   
						temp = temp - Double.valueOf(str2);
						area.setText(String.valueOf(temp));
						sign = " ";
					}
				} else if (sign == "*") { 
   
					if (str2.length() == 0) { 
   
						area.setText(String.valueOf(temp));
						sign = " ";
					} else { 
   
						temp = temp * Double.valueOf(str2);
						area.setText(String.valueOf(temp));
						sign = " ";
					}
				} else if (sign == "/") { 
   
					if (Double.valueOf(str2) == 0) { 
   
						area.setText("除数不能为0哦!按C");
						sign = " ";
					} else { 
   
						temp = temp / Double.valueOf(str2);
						area.setText(String.valueOf(temp));
						sign = " ";
					}
				} else if (sign == " ") { 
   
					if (str2.length() == 0) { 
   
						area.setText(String.valueOf(temp));
					} else { 
   
						temp = Double.valueOf(str2);
						area.setText(String.valueOf(temp));
					}
				} else if (sign == "s") { 
   
					temp = Math.sqrt(Double.valueOf(str2));
					area.setText(String.valueOf(temp));
					sign = " ";
				} else if (sign == "sin") { 
   
					temp = Math.sin(Double.valueOf(str2));
					area.setText(String.valueOf(temp));
					sign = " ";
				} else if (sign == "cos") { 
   
					temp = Math.cos(Double.valueOf(str2));
					area.setText(String.valueOf(temp));
					sign = " ";
				} else if (sign == "exp") { 
   
					temp = Math.exp(Double.valueOf(str2));
					area.setText(String.valueOf(temp));
					sign = " ";
				} else if (sign == "ln") { 
   
					temp = Math.log(Double.valueOf(str2));
					area.setText(String.valueOf(temp));
					sign = " ";
				}
				key = true;
			}
		}
	}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/161566.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • vue中watch监听对象的变化_远程监听器用法

    vue中watch监听对象的变化_远程监听器用法官方解释:一个对象,键是需要观察的表达式,值是对应回调函数。值也可以是方法名,或者包含选项的对象。Vue实例将会在实例化时调用watch,遍历watch对象的每一个属性。示例:滴哟<template> <el-cardclass=”box-card”><el-inputv-model=”name”style=”width:30%;”></el-input></el-card></template.

    2022年10月28日
  • webservice技术介绍

    一、WebService到底是什么?   一言以蔽之:WebService是一种跨编程语言和跨操作系统平台的远程调用技术。   所谓跨编程语言和跨操作平台,就是说服务端程序采用java编写,客户端程序则可以采用其他编程语言编写,反之亦然!跨操作系统平台则是指服务端程序和客户端程序可以在不同的操作系统上运行。    所谓远程调用,就是一台计算机a上的一个程序可以调用到另外一台

  • Mac里配置maven环境变量

    Mac里配置maven环境变量从windows转IOS还是有点难度的,就如环境变量来说吧,整整配置了一天啊。说实话网上教程很多,也写的很全,但不是每个人遇到的情况都一样,所以就有我这样的情况出现了,按照网上的教程配了好久,一直不好使。现在我就记录下来,避免以后忘记了。。。先说明下我的问题,我是在fishshell下编辑的profile文件,就是编辑完保存后就会一直报错,不是文件里面“=”不支持,就是git命令不好…

  • 免费的ssl证书申请_公需课怎么申请证书

    免费的ssl证书申请_公需课怎么申请证书第一步:在阿里云上申请免费的https证书先点击Symantec然后选择1个域名然后选择免费型DVSSL第二步:我的订单页面完成信息的补全第三步:点击详情查看DNS信息,并且配置DNS信息第四步:下载证书,上传到nginx服务器第五步:配置nginx代理,并重启nginx服务第六步:访问https的接口或者页面,查看是否配置正确。

  • 服务器矿机系统,云服务器矿机系统选择_云服务器系统哪个好

    服务器矿机系统,云服务器矿机系统选择_云服务器系统哪个好云服务器矿机内容精选换一换公有云平台支持弹性云服务器在专属主机与公共资源池之间迁移,具体包括:将创建在专属主机上的弹性云服务器迁移至其他专属主机。将创建在专属主机上的弹性云服务器迁移至公共资源池,即不再部署在专属主机上。将公共资源池的弹性云服务器迁移至专属主机上,成为专属主机上创建的弹性云服务器。关机状态的弹性云服务器才能执行迁移操作。云备份或云服务器备份不受冷迁用户可以在管理控制台对专属主机上…

  • SDRAM控制器设计(数字控制器的两种设计方法)

    FPGA之SDRAM控制器设计(二):刷新这次要来解决上次留下来的刷新问题,在100us后首先要经过两次刷新才进行模式寄存器设置。这颗SDRAM芯片需要每隔64ms对8192行(列地址10-位,行地址13位)的每一个存储电容进行一次刷新,因为不刷新电充会泄露电流导致存储信息丢失。那每一行的刷新时间为64/8192≈7810ns,注意刷新是以行为单位,器芯片内部有一个计数器,这个计数器不是时钟直接驱动的,而是AUTOPRECHARGE驱动,每发一次atuoprecharge命令这个计数器加1,我们是

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号