代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
标准计算器
作者:
锦城暮叶
/ 发布于
2013/10/24
/
857
/* * @(#)calculate.java 1.0 2013/01/22 * * Copyright (c) 2013 大连工业大学软件工作室. * All rights reserved. * */ /** * * @author JinCityKasto * * @since 1.0 13/01/22 * @version 1.0 13/01/22 */ //第一部分 public class calculate { public static void main(String[] args) { calculateFrame frame = new calculateFrame(); frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE); } } //第二部分 import java.awt.*; import javax.swing.*; public class calculateFrame extends JFrame{//外部窗口 顶层容器 public calculateFrame(){ setTitle("Calculate Made By JinCityKasto"); calculatePane panel = new calculatePane(); add(panel); Toolkit kit = Toolkit.getDefaultToolkit(); Dimension screenSize = kit.getScreenSize(); setSize(screenSize.width/3,screenSize.width/4); setLocation(screenSize.width/4,screenSize.height/4); setVisible(true); setResizable(false); } } //第三部分 import javax.swing.*; import java.awt.*; import java.awt.event.*; public class calculatePane extends JPanel{//calculate's interface display private JLabel labelForDisplay; private JLabel labelForRemember; private JPanel panelForTop; private JPanel panelForButton; private JPanel panelForUpButton; private JPanel panelForCenterButton; private double sign =1; private double result = 0; private double rememberM = 0; private String lastCommond = "="; private boolean calculateBegin = false; private boolean judgeBegin = true; private boolean judgeCleanDisplay = true; public calculatePane(){ setLayout(new BorderLayout()); panelForTop = new JPanel(); labelForDisplay = new JLabel("0.",SwingConstants.RIGHT); labelForRemember = new JLabel(); labelForDisplay.setBorder(BorderFactory.createEmptyBorder(10, 5, 10, 5)); labelForRemember.setBorder(BorderFactory.createEmptyBorder(10, 2, 10, 2)); panelForTop.setLayout(new GridLayout(1,2)); panelForTop.setBackground(Color.GRAY); panelForTop.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2)); panelForTop.add(labelForRemember); panelForTop.add(labelForDisplay); add(panelForTop,BorderLayout.NORTH); panelForButton = new JPanel(); panelForUpButton = new JPanel(); panelForCenterButton = new JPanel(); panelForButton.setLayout(new BorderLayout()); ActionListener addNumber = new addNumberAction(); ActionListener useFunction = new useFunctionAction(); setButton("",useFunction,0); setButton("Backspace",useFunction,0); setButton("CE",useFunction,0); setButton("C",useFunction,0); setButton("MC",useFunction,1); setButton("7",addNumber,1); setButton("8",addNumber,1); setButton("9",addNumber,1); setButton("/",useFunction,1); setButton("sqrt",useFunction,1); setButton("MR",useFunction,1); setButton("4",addNumber,1); setButton("5",addNumber,1); setButton("6",addNumber,1); setButton("*",useFunction,1); setButton("%",useFunction,1); setButton("MS",useFunction,1); setButton("1",addNumber,1); setButton("2",addNumber,1); setButton("3",addNumber,1); setButton("-",useFunction,1); setButton("1/x",useFunction,1); setButton("M+",useFunction,1); setButton("0",addNumber,1); setButton("+/-",addNumber,1); setButton(".",addNumber,1); setButton("+",useFunction,1); setButton("=",useFunction,1); panelForUpButton.setLayout(new GridLayout(1,4)); panelForCenterButton.setLayout(new GridLayout(4,6)); panelForButton.add(panelForUpButton,BorderLayout.NORTH); panelForButton.add(panelForCenterButton,BorderLayout.CENTER); add(panelForButton,BorderLayout.CENTER); labelForDisplay.setText("0."); } private void setButton(String label, ActionListener action,int judgeUpOrDown){ JButton button = new JButton(label); button.addActionListener(action); if(judgeUpOrDown == 0){ panelForUpButton.add(button); if(label == ""){ button.setEnabled(false); button.setText("锦城暮叶"); } }else{ panelForCenterButton.add(button); } } private class addNumberAction implements ActionListener{ public void actionPerformed(ActionEvent e) { String inputNumber = e.getActionCommand(); if(judgeBegin){ if(judgeCleanDisplay){ labelForDisplay.setText(""); judgeCleanDisplay = false; } if(inputNumber.equals("+/-")){ sign = -sign; String temporaryWords = labelForDisplay.getText(); if( temporaryWords.length()>=1 && temporaryWords.charAt(0) == '-'){ inputNumber = labelForDisplay.getText().substring(1); labelForDisplay.setText(inputNumber); }else{ inputNumber = "-"; labelForDisplay.setText(inputNumber + labelForDisplay.getText()); } }else{ labelForDisplay.setText(labelForDisplay.getText()+inputNumber); } } } } private class useFunctionAction implements ActionListener{ public void actionPerformed(ActionEvent e) { String inputFunction = e.getActionCommand(); if(inputFunction.equals("=")){ calculateBegin = true; } if(inputFunction.equals("MC")){ labelForRemember.setText(""); rememberM = 0 ; } if(inputFunction.equals("MR") && judgeBegin ){ labelForDisplay.setText(rememberM+""); } if(inputFunction.equals("MS") && judgeBegin ){ rememberM = Double.parseDouble(labelForDisplay.getText()); if(rememberM != 0 ){ labelForRemember.setText("M"); } } if(inputFunction.equals("M+") && judgeBegin ){ rememberM += Double.parseDouble(labelForDisplay.getText()); if(rememberM != 0){ labelForRemember.setText("M"); } } if(inputFunction.equals("Backspace")){ if(labelForDisplay.getText().length() > 1){ String temporaryWords = labelForDisplay.getText(); temporaryWords = temporaryWords.substring(0, temporaryWords.length()-1); labelForDisplay.setText(temporaryWords); }else{ labelForDisplay.setText("0"); } } if(inputFunction.equals("CE")){ labelForDisplay.setText("0"); judgeCleanDisplay = true; } if(inputFunction.equals("C")){ labelForDisplay.setText("0"); judgeCleanDisplay = true; judgeBegin = true; result = 0 ; } if(inputFunction.equals("/")||inputFunction.equals("*")||inputFunction.equals("-")||inputFunction.equals("+")||inputFunction.equals("%")||inputFunction.equals("=")){ if(judgeBegin){ if(calculateBegin){ calculateTheResult(Double.parseDouble(labelForDisplay.getText()),lastCommond); lastCommond = inputFunction; }else{ calculateTheResult(Double.parseDouble(labelForDisplay.getText()),"="); lastCommond = inputFunction; calculateBegin =true; } } } if(inputFunction.equals("sqrt") && judgeBegin){ labelForDisplay.setText(Math.sqrt(Double.parseDouble(labelForDisplay.getText()))+""); judgeCleanDisplay = true; } if(inputFunction.equals("1/x") && judgeBegin){ if(Double.parseDouble(labelForDisplay.getText()) == 0 ){ labelForDisplay.setText("ERROR"); judgeBegin = false; judgeCleanDisplay = true; }else{ labelForDisplay.setText((1 / Double.parseDouble(labelForDisplay.getText()))+""); judgeCleanDisplay = true; } } } } public void calculateTheResult(double number, String symbol){ if(symbol.equals("+")){ result += (sign * number); } if(symbol.equals("-")){ result -= (sign * number); } if(symbol.equals("*")){ result *= (sign * number); } if(symbol.equals("/")){ if(number == 0){ labelForDisplay.setText("ERROR"); judgeBegin = false; }else{ result /=(sign * number); } } if(symbol.equals("%")){ if(number == 0){ labelForDisplay.setText("ERROR"); judgeBegin = false; }else{ result %= (sign * number); } } if(symbol.equals("=")){ result = number; } if(judgeBegin){ labelForDisplay.setText(result+""); judgeCleanDisplay = true; } sign = 1; } }
试试其它关键字
标准计算器
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
锦城暮叶
贡献的其它代码
(
3
)
.
标准计算器
.
个人身份证识别程序的源代码
.
学生管理系统源代码
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3