[java]代码库
/**
* 计算器
* @author
*
*/
public class Calculator extends JFrame implements ActionListener{
//定义创建组件
JTextField t=new JTextField(20);
JButton[] b=new JButton[27];
String[] s={"Backspace","CE","C",
"MC","7","8","9","/","sqrt",
"MR","4","5","6","*","%",
"MS","1","2","3","-","1/x",
"M+","0","+/-",".","+","="
};
JMenuBar jmb = new JMenuBar();
JMenu jm1 = new JMenu("编辑(E)");
JMenu jm2 = new JMenu("查看(V)");
JMenu jm3 = new JMenu("帮助(H)");
JMenuItem jmi1 = new JMenuItem("复制(C)");
JMenuItem jmi2 = new JMenuItem("粘贴(P)");
public Calculator() {
this.setLayout(null);
this.add(t);
t.setHorizontalAlignment(JTextField.RIGHT);
t.setEditable(false);
t.setBounds(10,10,400,30);
//添加按钮,同时注册监听
for(int i=0;i<27;i++){
b[i]=new JButton(s[i]);
b[i].addActionListener(this);//注册监听
b[i].setActionCommand(s[i]);
this.add(b[i]);
}
//下面设置各个按钮之间的距离以及按钮的大小
//前三个按钮设置为大小100,30,间距为10
for(int i=0;i<3;i++){
b[i].setBounds(90+110*i,50,100,30);
}
//剩余的按钮大小相同,只是间距有所不同
int n=3;
for(int i=0;i<4;i++){
for(int j=0;j<6;j++){
if((n-3)%6==0){
b[n].setBounds(10,90+35*i,60,30);
}else{
b[n].setBounds(90+65*(j-1),90+35*i,60,30);
}
n++;
}
}
//设置菜单
this.setJMenuBar(jmb);
jmb.add(jm1);
jmb.add(jm2);
jmb.add(jm3);
jmi1.setMnemonic('C');
jmi1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK));
jmi2.setMnemonic('V');
jmi2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK));
jm1.add(jmi1);
jm1.add(jmi2);
jmi1.addActionListener(this);//注册菜单监听
jmi2.addActionListener(this);
jmi1.setActionCommand("copy");
jmi2.setActionCommand("paste");
//设置窗体
this.setTitle("计算器");
ImageIcon icon = new ImageIcon("images/jisuan.jpg");
this.setIconImage(icon.getImage());
this.setSize(430,290);
this.setLocation(200,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setVisible(true);
}
public static void main(String[] args) {
new Calculator();
}
String text[] = {"",""} ;
String showString = "";//要显示的字符串
int dotCount = 0;//点击小数点的次数
int j = 0;//用来表示是第几个操作数
int op = -1; //操作类型,0:/;1:*;2:-;3:+;4:%
int flag = 0;
String copyedString = "";
@Override
public void actionPerformed(ActionEvent e) {
String comand = e.getActionCommand();
if("copy".equals(comand)){
copyedString = t.getText();
}
if("paste".equals(comand)){
if(!("".equals(copyedString))){
showString = copyedString;
text[j] = copyedString;
}
}
for(int i = 4; i<7; i++){ //点击7、8、9
if(s[i].equals(comand)) text[j] += s[i];
showString = text[j];
}
for(int i = 10; i<13; i++){//点击4、5、6
if(s[i].equals(comand)) text[j] += s[i];
showString = text[j];
}
for(int i = 16; i<19; i++){//点击1、2、3
if(s[i].equals(comand)) text[j] += s[i];
showString = text[j];
}
if(s[22].equals(comand)) {//点击0
text[j] += s[22];
showString = text[j];
}
if(s[24].equals(comand)){ //点击".",只允许点一次,点第二次不给反应
if(dotCount == 0) {
text[j] += s[24];
showString = text[j];
dotCount ++;
}
}
if(s[7].equals(comand)){//点击"/"
showString = operator();
text[0] = showString;
text[1] = "";
op = 0;
j = 1;//下面要换成text[1]来记录第二个操作数
}
if(s[13].equals(comand)){//点击"*",只能点一次
showString = operator();
text[0] = showString;
text[1] = "";
op = 1;
j = 1;//下面要换成text[1]来记录第二个操作数
}
if(s[19].equals(comand)){//点击"-",只能点一次
showString = operator();
text[0] = showString;
text[1] = "";
op = 2;
j = 1;//下面要换成text[1]来记录第二个操作数
}
if(s[25].equals(comand)){//点击"+",只能点一次
showString = operator();
text[0] = showString;
text[1] = "";
op = 3;
j = 1;//下面要换成text[1]来记录第二个操作数
}
if(s[14].equals(comand)){//点击%
if(j==0) {
op = 4;
j = 1;
}
}
if(s[0].equals(comand)){//点击Backspace
int len = text[j].length();
if(len > 0){
text[j] = text[j].substring(0,text[j].length()-1);
showString = text[j];
}
}
if(s[2].equals(comand)){//点击c
text[j] = "";
showString = "";
}
if(s[23].equals(comand)){//点击+/-
if(flag == 0){
text[j] = "-"+text[j];
showString = text[j];
flag = 1;
}else if(flag==1){
text[j] = showString.substring(1,showString.length());
showString = text[j];
flag = 0;
}
System.out.println("flag===="+flag);
}
if(s[26].equals(comand)){//点击=
showString = operator();
}
if(s[1].equals(comand)){//点击CE
//此处直接将所有的数据全部恢复初始值,同时将文本框中的数值清空
text[0] = "";
text[1] = "";
t.setText("");
showString = "";
dotCount = 0;
op = -1;
j = 0;
}
if(s[20].equals(comand)){//点击1/x
if(!("".equals(text[j]))){
double num = Double.parseDouble(text[j]);
num = 1/num;
text[j] = num+"";
showString = num+"";
}
}
if(s[8].equals(comand)){//点击sqrt
if(!("".equals(text[j]))){
double num = Double.parseDouble(text[j]);
num = Math.sqrt(num);
text[j] = dealResult(num+"");
showString = text[j];
}
}
t.setText(showString);
}
public String operator(){//进行计算
if("".equals(text[1]) || "".equals(text[0])){
return text[0];
}
double op1 = Double.parseDouble(text[0]);//得到第一个操作数
double op2 = Double.parseDouble(text[1]);//得到第二个操作数
double result = 0.0;
//进行计算
switch (op) {
case 0://除
if(op2 != 0){//除法时第二操作数不能是0
result = op1 / op2;
}
break;
case 1://乘
result = op1*op2;
break;
case 2://减
result = op1-op2;
break;
case 3://加
result = op1 + op2;
break;
case 4:
int mu1 = (int)op1;
int mu2 = (int)op2;
if((op1 == mu1) && (op1 == mu2)){//整除需要两个操作数都是整数
result = (int)op1%(int)op2;
System.out.println("zhen");
}else{
result = op2;
}
break;
default:
break;
}
String resString = result+"";
resString = dealResult(resString);
return resString;
}
String dealResult(String resString){
int dotpos = resString.indexOf(".");
if(resString.substring(dotpos+1, resString.length()).equals("0")){
resString = resString.substring(0,dotpos);
}
return resString;
}
}