⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 caculator.java

📁 用java编写的小型计算器程序
💻 JAVA
字号:
package caculator;

import java.awt.*; 
import java.awt.event.*; 

public class Caculator extends Frame implements ActionListener { 
/**
	 * 
	 */
	private static final long serialVersionUID = 7906660113192680846L;
double firstarg = 0.0; //第一个操作数 
double secondarg = 0.0; //第二个操作数 
int number = 1; //一个状态值,代表当前是在输入第几个操作数 
String type = ""; //当前操作符是什么 
String lastkey = ""; // 最后一次的操作符 
boolean secondinputaction = true; //一个状态值,为真代表正在输入第2个操作数 
static Panel pan = new Panel(); 
static Label lab = new Label("0.0", Label.RIGHT); //显示框 

static Button b[] = new Button[10]; //0123456789 
static Button b_point //+-*/ 
, 
b_add 
, 
b_subtract 
, 
b_multiply 
, 
b_divide 
, 
b_equal; 

public Caculator() { //界面初始化,把按纽加上,指定布局 
int i; 
for (i = 0; i < 10; i++) { 
b[i] = new Button("" + i); 
b[i].addActionListener(this); 
} 
b_point = new Button("."); 
b_point.addActionListener(this); 
b_add = new Button("+"); 
b_add.addActionListener(this); 
b_subtract = new Button("-"); 
b_subtract.addActionListener(this); 
b_multiply = new Button("*"); 
b_multiply.addActionListener(this); 
b_divide = new Button("/"); 
b_divide.addActionListener(this); 
b_equal = new Button("="); 
b_equal.addActionListener(this); 
this.setTitle("网格式布局管理器GridLayout"); 
this.setLayout(null); 
this.setSize(180, 200); 
this.setResizable(false); 
GridLayout grid = new GridLayout(4, 4); 
pan.setLayout(grid); 
pan.setBounds(20, 60, 150, 120); 
lab.setBounds(20, 35, 150, 20); 
lab.setBackground(Color.CYAN); 
pan.add(b[7]); 
pan.add(b[8]); 
pan.add(b[9]); 
pan.add(b_divide); 
pan.add(b[4]); 
pan.add(b[5]); 
pan.add(b[6]); 
pan.add 

(b_multiply); 
pan.add(b[1]); 
pan.add(b[2]); 
pan.add(b[3]); 
pan.add 

(b_subtract); 
pan.add(b[0]); 
pan.add(b_point); 
pan.add(b_equal); 
pan.add 

(b_add); 
this.add(lab); 
this.add(pan); 
this.addWindowListener(new WindowClose()); 
this.setVisible(true); 
} 

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

public void actionPerformed(ActionEvent e) { //按下按纽触发的事件 
String command = e.getActionCommand(); //按下的按纽上的文字 
if (lab.getText().indexOf("错误") > -1) { //如果出错了,那么程序清空以前的操作,重新开始计算 
number = 1; 
firstarg = 0.0; 
secondarg = 0.0; 
type = ""; 
lab.setText("0.0"); 
} 
if ("+".equals(command) || "-".equals(command) || "*".equals(command) || "/".equals(command)) { //如果按下+-*/ 
secondinputaction = true; //开始输入第2个操作数 
if (!"+".equals(lastkey) && !"-".equals(lastkey) && !"*".equals(lastkey) && ! "/".equals(lastkey)) { //如果上一个按钮不是+-*/ 
if (number == 1) { //如果当前输入第一个操作数,就把第一个操作数记录到 firstarg里 
try { 
firstarg = Double.parseDouble(lab.getText()); 
lab.setText("0.0"); 
} catch (Exception err) { 
lab.setText("错误" + err.getMessage()); 
} 
number = 2; 
} else { //否则记录到 secondarg 中,并进行相应运算 
try { 
secondarg = Double.parseDouble(lab.getText()); 
if (type.equals("+")) { 
firstarg += secondarg; 
} else if (type.equals("-")) { 
firstarg -= secondarg; 
} else if (type.equals("*")) { 
firstarg *= secondarg; 
} else if (type.equals("/")) { 
firstarg /= secondarg; 
} 
String temp = firstarg + ""; 
if (temp.length() > 20) temp = temp.substring(0, 20); 
firstarg = Double.parseDouble(temp); 
lab.setText(temp); 
} catch (Exception err) { 
lab.setText("错误" + err.getMessage()); 
} 
secondarg = 0.0; //计算完成后就把第二操作数清0 
} 
} 
type = command; //记录下操作符 
} else if ("=".equals(command)) { //如果按下= 
if (number == 2) { //如果已经有第一个操作数了 
try { 
secondarg = Double.parseDouble(lab.getText()); //记录第2操作数并进行相应运算 
if (type.equals("+")) { 
firstarg += secondarg; 
} else if (type.equals("-")) { 
firstarg -= secondarg; 
} else if (type.equals("*")) { 
firstarg *= secondarg; 
} else if (type.equals("/")) { 
firstarg /= secondarg; 
} 
String temp = firstarg + ""; 
if (temp.length() > 20) temp = temp.substring(0, 20); 
firstarg = Double.parseDouble(temp); 
lab.setText(temp); 
} catch (Exception err) { 
lab.setText("错误" + err.getMessage()); 
} 
secondarg = 0.0; 
type = ""; //按下=后,运算结束,且没有新的运算符产生,所以type要清除 
} 
} else { //如果按下的数字或小数点 
if (number == 1) { //没有记录第一个操作数 
if (!(lab.getText().indexOf(".") > -1 && ".".equals(command))) { //显示框没有小数点,且按下的按纽是小数点 
if (!"0.0".equals(lab.getText())) lab.setText(lab.getText() + command); //假如显示框不是0.0,累加字符 
else lab.setText(command); //直接用数字替换显示框的内容 
} 
} else { //记录了第一个操作数 
if (command.equals(".")) { //按下小数点 
if (secondinputaction) { //正在输入第2操作符 
lab.setText("0."); 
secondinputaction = false; 
} else { 
lab.setText(lab.getText() + command); 
} 
} else { 
if (secondinputaction) { 
lab.setText(command); 
secondinputaction = false; 
} else lab.setText(lab.getText() + command); 
} 
} 
} 
lastkey = command; //记录操作符 
} 

static class WindowClose extends WindowAdapter { 
public void windowClosing(WindowEvent e) { 
System.exit(0); 
} 
} 
} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -