简易计算器(源码)
放假结束,2019正式开⼯~
基本实现的简单功能 + - * /
MC:清除存储的数据 MR:读取存储的数据
MS:将所显⽰的数存⼊存储器中,存储器中原有的数据被覆盖
M+:将输⼊的数加上存储器中的数,所得结果存⼊存储器
M-:将存储器中的数减去输⼊的数,所得结果存⼊存储器
CE:在数字输⼊期间按下此键,将清除输⼊寄存器中的值并显⽰"0",可重新输⼊
C:清除全部数据结果和运算符
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.curity.PrivateKey;
import java.util.Vector;
import java.math.BigDecimal;
/*
MC:清除存储的数据
MR:读取存储的数据
MS:将所显⽰的数存⼊存储器中,存储器中原有的数据被覆盖
M+:将输⼊的数加上存储器中的数,所得结果存⼊存储器
M-:将存储器中的数减去输⼊的数,所得结果存⼊存储器
pi
CE:在数字输⼊期间按下此键,将清除输⼊寄存器中的值并显⽰"0",可重新输⼊
C:清除全部数据结果和运算符
*/
public class Calculator extends JFrame implements ActionListener {
private final String[] KEYS = {"7", "8", "9", "/", "sqrt",//创建数组,⽤于存储操作按键
"4", "5", "6", "*", "%",
"1", "2", "3", "-", "1/x",
"0", "+/-", ".", "+", "="};
private final String[] COMMAND = {" 回退", "CE", "C"};//创建数组⽤于存储功能按键
// CE:在数字输⼊期间按下此键,将清除输⼊寄存器中的值并显⽰"0",可重新输⼊ C:清除全部数据结果和运算符
private final String[] M = {"功能键", "MC", "MR", "MS", "M+"};//创建数组存储M类型的操作按键
/*MC:清除存储的数据
MR:读取存储的数据
MS:将所显⽰的数存⼊存储器中,存储器中原有的数据被覆盖
M+:将输⼊的数加上存储器中的数,所得结果存⼊存储器
M-:将存储器中的数减去输⼊的数,所得结果存⼊存储器*/
//创建三个JButton数组⽤于存储上述String数组的各个功能
private JButton keys[] = new JButton[KEYS.length];
private JButton commands[] = new JButton[COMMAND.length];
private JButton m[] = new JButton[M.length];
private JTextField resultText = new JTextField("0");
private boolean firstDigit = true;
private double resultNum = 0.0;
private String operator = "=";
private boolean operatorValidFlag = true;
private boolean operatorValidFlag = true;
public Calculator() {
super();
init();
//设置计算器背景颜⾊
this.tBackground(Color.LIGHT_GRAY);
//设置计算器标题
ry
this.tTitle("MR:FAN's Calculator");
//设置计算器的左上⾓坐标
this.tLocation(500, 300);
//禁⽌修改计算器窗⼝尺⼨
this.tResizable(fal);
//使计算器⾃动调整组件⼤⼩
specifiedthis.pack();
}
private void init() {
//将JtextField的内容设置为右对齐
resultText.tHorizontalAlignment(JTextField.RIGHT);
/
/将⽂本框的背景设置为⽩⾊
resultText.tBackground(Color.white);
/************将String数组的内容添加到JButton数组中*************/
//按钮panel
JPanel calckeyPanel1 = new JPanel();
calckeyPanel1.tLayout(new GridLayout(4, 5, 3, 3));
for (int i = 0; i < KEYS.length; i++) {
keys[i] = new JButton(KEYS[i]);
calckeyPanel1.add(keys[i]);
keys[i].tForeground(Color.black);
}
/
/命令panel
JPanel commandsPanel = new JPanel();
commandsPanel.tLayout(new GridLayout(1, 3, 3, 3));
for (int i = 0; i < COMMAND.length; i++) {
commands[i] = new JButton(COMMAND[i]);
commandsPanel.add(commands[i]);
commands[i].);
}
//计算panel
JPanel calmsPanel = new JPanel();
calmsPanel.tLayout(new GridLayout(5, 1, 3, 3));
for (int i = 0; i < M.length; i++) {
m[i] = new JButton(M[i]);
calmsPanel.add(m[i]);
m[i].an);
}
/****************************************************************************/
//创建⼀个新的panel将按钮和命令panel按照格式置放
winter怎么读
JPanel panel1 = new JPanel();
panel1.tLayout(new BorderLayout(3, 3));
panel1.add("North", commandsPanel);
panel1.add("Center", calckeyPanel1);
/
/创建⼀个新的panel将点击的数字输⼊到该panel中
JPanel top = new JPanel();
became
top.tLayout(new BorderLayout());
top.add("Center", resultText);
//将三个JPanel 添加到当前的⾯板
getContentPane().tLayout(new BorderLayout(3, 5));
getContentPane().tLayout(new BorderLayout(3, 5));
getContentPane().add("North", top);
getContentPane().add("Center", panel1);
getContentPane().add("West", calmsPanel);礼节性
//所有JButton添加监听
for (int i = 0; i < KEYS.length; i++)
草莓的英文keys[i].addActionListener(this);
for (int i = 0; i < COMMAND.length; i++)
commands[i].addActionListener(this);
for (int i = 0; i < M.length; i++)
m[i].addActionListener(this);
}
//实现监听接⼝
public void actionPerformed(ActionEvent e) {
String label = e.getActionCommand();
if(label.equals(COMMAND[0])){
Backspace();
}
el if(label.equals(COMMAND[1])){
resultText.tText("0");
}
el if(label.equals(COMMAND[2]))
C();
el if("0123456789.".indexOf(label)>=0)
Number(label);
el { handleOperator(label);
}
}
//处理回退功能将字符串利⽤subString截取
public void Backspace(){
String text = Text();
int i = text.length();
if(i > 0){
text = text.substring(0,i-1);
if(text.length() == 0){
resultText.tText("0");
firstDigit = true;
operator = "=";
}
el{
resultText.tText(text);
}
}
}
// 处理数字先判断是不是⾸数字再判断是否为⼩⿏
public void Number(String key){
if(firstDigit){
resultText.tText(key);
}el if ((key.equals("."))&&(Text().indexOf(".")< 0 )){ resultText.Text()+".");
}el if (!key.equals(".")){
resultText.Text()+key);
}
firstDigit = fal;
}
//初始化
public void C(){
resultText.tText("0");
firstDigit = true;
operator = "=";
}
/
/处理操作运算符 ”/“ 和”1/x“需考虑 0为除数
private void handleOperator(String key){
if(operator.equals("/")) {
if (getNumberFromText() == 0.0) {
operatorValidFlag = fal;
resultText.tText("除数不为0");
} el {免费英语在线翻译
resultNum /= getNumberFromText();
}
}
el if (operator.equals("1/x")){
if(resultNum == 0.0) {
operatorValidFlag = fal;
resultText.tText(" 0 没有倒数");
}
el{
columnresultNum = 1 / resultNum;
}
}
el if (operator.equals("+")){ resultNum += getNumberFromText(); } el if (operator.equals("-")){ resultNum -= getNumberFromText(); } el if (operator.equals("*")){ resultNum *= getNumberFromText(); } el if (operator.equals("sqrt")){resultNum = Math.sqrt(resultNum);} el if (operator.equals("%")){ resultNum /=100;}
el if (operator.equals("+/-")){resultNum = resultNum*(-1);}
el if (operator.equals("=")){resultNum =getNumberFromText();}
if(operatorValidFlag){
long t1;
double t2;
t1 = (long)resultNum;
t2 = resultNum - t1;
if(t2 == 0)
resultText.tText(String.valueOf(t1));
el
resultText.tText(String.valueOf(resultNum));
}
operator = key;
firstDigit = true;
operatorValidFlag = true;
}
public double getNumberFromText(){
double result = 0;
try{ result = Double.Text()).doubleValue();} catch(NumberFormatException E){E.printStackTrace();}
南非世界杯主题曲return result;
}
public static void main(String args[]) {
Calculator calculator1 = new Calculator();
calculator1.tVisible(true);
calculator1.tDefaultCloOperation(JFrame.EXIT_ON_CLOSE); }
}