铁哥727
2024-05-30 15:15:30
最佳回答
kankan我的import j**a.awt.borderlayout;import j**a.awt.gridlayout;import j**a.awt.event.actionevent;import j**a.awt.event.actionl**tener;import j**a.math.bigdecimal;import j**ax.swing.**utton;import j**ax.swing.jframe;import j**ax.swing.jpanel;import j**ax.swing.jtextfield;public class testcalc implements actionl**tener { jframe jf = new jframe("计算器"); jpanel jp = new jpanel(); jtextfield jtf = new jtextfield("0.", 200); **utton[] ** = new **utton[20]; private int tag = 0; private double a; private double b; private string operator; stringbuilder **1 = new stringbuilder(); stringbuilder **2 = new stringbuilder(); public testcalc() { jf.add(jtf, borderlayout.north); // jtf.requestfocus(); jtf.seteditable(false); jtf.setcaretposition(jtf.gettext().length() - 1); jf.add(jp); jp.setlayout(new gridlayout(5, 4, 4, 4)); string[] str = { "back", "ce", "c", "+", "7", "8", "9", "-", "4", "5", "6", "*", "1", "2", "3", "/", "0", "+/-", ".", "=" }; int i = 0; for (i = 0; i < str.length; i++) { **[i] = new **utton(str[i]); jp.add(**[i]); **[i].addactionl**tener(th**); } jf.setsize(300, 240); // jf.setlocation(300,200); jf.setlocationrelativeto(**); jf.setv**ible(true); // jf.pack();//自动调整大小; jf.setresizable(false);// 不允许别人调大小; jf.setdefaultcloseoperation(jframe.exit_on_close); } public static void main(string[] args) { testcalc tc = new testcalc(); } public void actionperformed(actionevent e) { string command = e.getactioncommand(); if (command.matches("^[[0-9].]$")) { if (tag == 0) { **1.append(command); jtf.settext(**1.substring(0)); } else { **2.append(command); jtf.settext(**2.substring(0)); } } else if (command.matches("^[-/*+]$")) { tag = 1; operator = command; } else { if (command.matches("=")) { string str1 = **1.substring(0); string str2 = **2.substring(0); if (str1 == **) { a = 0.0; } else { a = double.parsedouble(str1); } if (str2 == **) { b = 0.0; } else { b = double.parsedouble(str2); } if (operator.equals("+")) { jtf.settext("" + (a + b)); } else if (operator.equals("-")) { bigdecimal bd1 = new bigdecimal(double.tostring(a)); // 必须使用string做参数才可以精确运算 bigdecimal bd2 = new bigdecimal(double.tostring(b)); double yu1 = bd1.subtract(bd2).doublevalue(); jtf.settext("" + (yu1)); } else if (operator.equals("*")) { jtf.settext("" + (a * b)); } else if (operator.equals("/")) { bigdecimal bd1 = new bigdecimal(double.tostring(a)); // 必须使用string做参数才可以精确运算 bigdecimal bd2 = new bigdecimal(double.tostring(b)); double yu1 = bd1.divide(bd2).doublevalue(); jtf.settext("" + yu1); } tag = 0; **1.delete(0, **1.length()); **2.delete(0, **2.length()); } else if (command.matches("c")) { tag = 0; **1.delete(0, **1.length()); **2.delete(0, **2.length()); jtf.settext("0."); jtf.setcaretposition(jtf.gettext().length() - 1); } else if (command.matches("ce")) { tag = 0; **2.delete(0, **2.length()); jtf.settext(**1.substring(0)); } else if (command.matches("back")) {//back功能键的实现; if (tag == 0) { **1.deletecharat(**1.length() - 1); jtf.settext(**1.substring(0)); } else { **2.deletecharat(**2.length() - 1); jtf.settext(**2.substring(0)); } } else if (command.matches("\\+/-")) { if (tag == 0) { if(**1.substring(0,1).equals("-")){ **1.replace(0,1,"+"); }else{ **1.insert(0, "-"); } jtf.settext(**1.substring(0)); } else { if(**2.substring(0,1).equals("-")){ **2.replace(0,1,"+"); }else{ **2.insert(0, "-"); } jtf.settext(**2.substring(0)); } }else { } } }}
import j**ax.swing.*;import j**a.awt.*;import j**a.awt.event.*;public class calculator extends jframe implements actionl**tener{private static final long serialversionuid = 8199443193151152362l;private **utton bto_s=new **utton("sqrt"),bto_zf=new **utton("+/-"),bto_ce=new **utton("ce"),bto_c=new **utton("c"),bto_7=new **utton("7"),bto_8=new **utton("8"),bto_9=new **utton("9"),bto_chu=new **utton("/"),bto_4=new **utton("4"),bto_5=new **utton("5"),bto_6=new **utton("6"),bto_cheng=new **utton("*"),bto_1=new **utton("1"),bto_2=new **utton("2"),bto_3=new **utton("3"),bto_jian=new **utton("-"),bto_0=new **utton("0"),bto_dian=new **utton("."),bto_deng=new **utton("="),bto_jia=new **utton("+");**utton button[]={bto_s,bto_zf,bto_ce,bto_c,bto_7,bto_8,bto_9,bto_chu,bto_4,bto_5,bto_6,bto_cheng,bto_1,bto_2,bto_3,bto_jian, bto_0,bto_dian,bto_deng,bto_jia};private jtextfield text_double;// = new jtextfield("0");private string operator = "="; //当前运算的运算符private boolean firstdigit = true; // 标志用户按的是否是整个表达式的第一个数字,或者是运算符后的第一个数字private double resultnum = 0.0; // 计算的中间结果private boolean operatevalidflag = true; //判断操作是否合法public calculator(){ super("calculator"); th**.setbounds(300, 300, 300, 300); th**.setresizable(false); th**.setbackground(color.orange); th**.setdefaultcloseoperation(exit_on_close); th**.getcontentpane().setlayout(new borderlayout());//设置布局 text_double=new jtextfield("0",20);//设置文本区 text_double.sethorizontalalignment(jtextfield.right);//设置水平对齐方式未右对齐 th**.getcontentpane().add(text_double,borderlayout.north);//将文本区添加到content北部 jpanel panel=new jpanel(new gridlayout(5,4));//在内容窗口添加一个网格布局 th**.getcontentpane().add(panel);//添加panel面板 for(int i=0;i<button.length;i++)//在面板上添加按钮 panel.add(button[i]); for(int i=0;i<button.length;i++) button[i].addactionl**tener(th**);//为按钮注册 text_double.seteditable(false);//文本框不可编辑 text_double.addactionl**tener(th**);// th**.setv**ible(true);}public void actionperformed(actionevent e)//{ string c= e.getactioncommand();//返回与此动作相关的命令字符串。 system.out.println("##########command ** "+c); if(c.equals("c")){ handlec(); //用户按了“c”键 } else if (c.equals("ce")) // 用户按了"ce"键 { text_double.settext("0"); } else if ("0123456789.".indexof(c) >= 0) // 用户按了数字键或者小数点键 { handlenumber(c); // handlezero(zero); } else //用户按了运算符键 { handleoperator(c); }}private void handlec() // 初始化计算器的各种值{ text_double.settext("0"); firstdigit = true; operator = "="; }private void handlenumber(string button) { if (firstdigit)//输入的第一个数字 { text_double.settext(button); } else if ((button.equals(".")) && (text_double.gettext().indexof(".") < 0))//输入的是小数点,并且之前没有小数点,则将小数点附在结果文本框的后面 //如果字符串参数作为一个子字符串在此对象中出现,则返回第一个这种子字符串的第一个字符的索引;如果它不作为一个子字符串出现,则返回 -1 { text_double.settext(text_double.gettext() + "."); } else if (!button.equals("."))// 如果输入的不是小数点,则将数字附在结果文本框的后面 { text_double.settext(text_double.gettext() + button); }// 以后输入的肯定不是第一个数字了 firstdigit = false; }private void handleoperator(string button) { if (operator.equals("/")) {// 除法运算// 如果当前结果文本框中的值等于0 if (getnumberfromtext() == 0.0){// 操作不合法 operatevalidflag = false; text_double.settext("除数不能为零"); } else { resultnum /= getnumberfromtext(); } } else if (operator.equals("+")){// 加法运算 resultnum += getnumberfromtext(); } else if (operator.equals("-")){// 减法运算 resultnum -= getnumberfromtext(); } else if (operator.equals("*")){// 乘法运算 resultnum *= getnumberfromtext(); } else if (operator.equals("sqrt")) {// 平方根运算 if(getnumberfromtext()<0){ operatevalidflag = false; text_double.settext("被开方数不能为负数");} else resultnum = math.sqrt(resultnum); } else if (operator.equals("+/-")){// 正数负数运算 resultnum = resultnum * (-1); } else if (operator.equals("=")){// 赋值运算 resultnum = getnumberfromtext(); } if (operatevalidflag) {// 双精度浮点数的运算 long t1; double t2; t1 = (long) resultnum; t2 = resultnum - t1; if (t2 == 0) { text_double.settext(string.valueof(t1)); } else { text_double.settext(string.valueof(resultnum)); } } operator = button; //运算符等于用户按的按钮 firstdigit = true; operatevalidflag = true;}private double getnumberfromtext() //从结果的文本框获取数字{ double result = 0; try { result = double.valueof(text_double.gettext()).doublevalue(); // valueof()返回表示指定的 double 值的 double 实例 } catch (numberformatexception e){ } return result; }public static void main(final string[] args) {new calculator();}} 20210311