package calframe;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class CalFrame
extends JFrame {
/**
* 计算器的外观设计
*/
private static final long serialVersionUID = 1L;
private final static int PRE_WIDTH = 500;
private final static int PRE_HEIGHT = 400;
private JButton button
= null; //存储标记
private String[] nOp
= {"7",
"8",
"9",
"/",
"sqrt",
"4",
"5",
"6",
"*",
"%",
"1",
"2",
"3",
"-",
"1/x",
"0",
"+/-",
".",
"+",
"="};
private String[] mOp
= {"MC",
"MR",
"MS",
"M+"};
private String[] rOp
= {"Back",
"CE",
"C"};
private CalService service = new CalService();
public CalFrame(){
this.setTitle("计算器");
this.setSize(PRE_WIDTH, PRE_HEIGHT);
this.setLocationRelativeTo(null);
this.setResizable(false);
//添加底层
panel.
setPreferredSize(new Dimension(PRE_WIDTH, PRE_HEIGHT
));
//WEST
panel1.add(b);
}
//
panel21.add(b);
}
panel22.add(b);
}
this.add(panel);
this.
setDefaultCloseOperation(JFrame.
DISPOSE_ON_CLOSE);
this.setVisible(true);
}
//返回显示框
//text.setSize(480, 50);
return text;
}
//返回数字键
String[] redButton
= {"/",
"*",
"-",
"+",
"="};
for(int i = 0; i < this.nOp.length; i++){
b.addActionListener(getActionListener());
if(Arrays.
binarySearch(redButton, nOp
[i
]) >= 0){
b.
setForeground(Color.
red);
}else{
b.
setForeground(Color.
blue);
}
nbutton[i] = b;
}
return nbutton;
}
//返回操作健
mbutton[0] = getButton();
for(int i = 0; i < this.mOp.length; i++){
b.addActionListener(getActionListener());
b.
setForeground(Color.
red);
mbutton[i+1] = b;
}
return mbutton;
}
for(int i = 0; i < this.rOp.length; i++){
b.addActionListener(getActionListener());
b.
setForeground(Color.
red);
rbutton[i] = b;
}
return rbutton;
}
return button;
}
@Override
String cmd
= e.
getActionCommand();
try {
result = service.callMethod(cmd, text.getText());
System.
out.
println(e2.
getMessage());
}
if(cmd.indexOf("MC") == 0){
button.setText("");
}else if(cmd.indexOf("M") == 0){
button.setText("M");
}
//显示计算结果
if(result != null){
text.setText(result);
}
}
};
return actionListener;
}
public static void main
(String[] args
) {
new CalFrame();
}
}
package calframe;
import java.math.BigDecimal;
public class MyMath {
/**
* 为一个double类型创建BigDecimal对象
*/
private static BigDecimal getBigDecimal
(double number
){
}
public static double add(double num1, double num2) {
return first.add(second).doubleValue();
}
public static double subtract(double num1, double num2) {
return first.subtract(second).doubleValue();
}
public static double multiply(double num1, double num2) {
return first.multiply(second).doubleValue();
}
public static double divide(double num1, double num2) {
return first.
divide(second,
3,
BigDecimal.
ROUND_HALF_UP).
doubleValue();
}
}
最后处理按钮的事件,我们定义了一个CalService类处理业务逻辑:
[java] view plain copy
package calframe;
public class CalService {
private boolean isSecondNum = false;
private String firstNum
= "0";
private String secondNum
= "null";
private double store;
private String numString
= "0123456789.";
private String opString
= "+-*/";
// 如果text不等于0
if (!"0".equals(text)) {
if (isSecondNum) {
isSecondNum = false;
} else {
result = text + cmd;
}
}
if (result.indexOf(".") == 0) {
result = "0" + result;
}
return result;
}
this.lastOp = cmd;
this.firstNum = text;
this.secondNum = null;
this.isSecondNum = true;
return null;
}
double secondResult
= secondNum
== null ? Double.
valueOf(text
)
.
doubleValue() : Double.
valueOf(secondNum
).
doubleValue();
//除数为0
if(secondResult == 0 && this.lastOp.equals("/")){
return "0";
}
//有%
if(isPercent){
secondResult
= MyMath.
multiply(Double.
valueOf(firstNum
), MyMath.
divide(secondResult,
100));
}
if(this.lastOp.equals("+")){
firstNum
= String.
valueOf(MyMath.
add(Double.
valueOf(firstNum
),secondResult
));
}else if (this.lastOp.equals("-")) {
firstNum
= String.
valueOf(MyMath.
subtract(Double.
valueOf(firstNum
),secondResult
));
}else if (this.lastOp.equals("*")) {
firstNum
= String.
valueOf(MyMath.
multiply(Double.
valueOf(firstNum
),secondResult
));
}else if (this.lastOp.equals("/")) {
firstNum
= String.
valueOf(MyMath.
divide(Double.
valueOf(firstNum
),secondResult
));
}
secondNum = secondNum == null ? text :secondNum;
this.isSecondNum = true;
return firstNum;
}
//求开方
this.isSecondNum = true;
}
//求倒数
if (text.equals("0")){
return text;
}else{
this.isSecondNum = true;
return String.
valueOf(MyMath.
divide(1,
Double.
valueOf(text
)));
}
}
//存储
if(cmd.equals("M+")){
store
= MyMath.
add(store,
Double.
valueOf(text
));
}else if (cmd.equals("MC")) {
store = 0;
}else if (cmd.equals("MR")) {
isSecondNum = true;
}else if (cmd.equals("MS")) {
store
= Double.
valueOf(text
).
doubleValue();
}
return null;
}
return text.equals("0") || text.equals("") ? "0" :text.substring(0,text.length()-1);
}
if(text.indexOf("-") == 0){
return text.substring(1,text.length());
}else{
return "-" + text;
}
}
this.firstNum = "0";
this.secondNum = null;
return this.firstNum;
}
return "0";
}
if(cmd.equals("C")){
return clearAll();
}else if(cmd.equals("CE")){
return clear(text);
}else if (cmd.equals("Back")) {
return backSpace(text);
}else if (numString.indexOf(cmd) != -1) {
return catNum(cmd, text);
}else if (opString.indexOf(cmd) != -1) {
return setOp(cmd, text);
}else if (cmd.equals("+/-")) {
return setNegative(text); //设置正负号
}else if(cmd.equals("1/x")){
return setReciprocal(text);
}else if (cmd.equals("sqrt")) {
return sqrt(text);
}else if(cmd.equals("%")){
return cal(text, true);
}else if(cmd.equals("=")){
return cal(text, false);
}else {
return mCmd(cmd, text);
}
}
}