java计算器程序.doc

上传者: gangshou | 上传时间: 2023-12-19 07:02:42 | 文件大小: 110KB | 文件类型: DOC
java 编写计算器的简单程序//一个较为简洁的巧妙的计算器程序, import java.io.*; import java.awt.event.*; import java.awt.*; //需要解决的问题,数学的运算都有正负号的出现,在点击等号的时候就会有冲突,应该怎样解决,经验:双精度浮点型数据类型是会像后减一位。0.7会显示成0.69999999 public class app74 { static int i=0; static int j=0; static int k=-1; static int l=-1; //static int Data1[]={1}; static String str9=""; static Frame frm1=new Frame("计算器"); static Button btn1=new Button("1"); static Button btn2=new Button("2"); static Button btn3=new Button("3"); static Button btn5=new Button("4"); static Button btn6=new Button("5"); static Button btn7=new Button("6"); static Button btn9=new Button("7"); static Button btn14=new Button("8"); static Button btn16=new Button("9"); static Button btn11=new Button("0");//数字0 static Button btn4=new Button("."); static Button btn8=new Button("+"); static Button btn10=new Button("-"); static Button btn15=new Button("*"); static Button btn13=new Button("/"); static Button btn20=new Button("="); static Button btn18=new Button("清零"); static Button btn19=new Button("后退"); static Button btn12=new Button("π"); static Button btn17=new Button("颜色"); static TextField TF1=new TextField(); static Label Lb1=new Label(".0",Label.RIGHT); //标签 static Label Lb2=new Label(".0",Label.RIGHT); //BS字符长度 static Label Lb3=new Label(".0",Label.RIGHT); //对象str22字符长度 static Label Lb4=new Label(".0",Label.RIGHT); //BS剩余字符长度字符长度 static Label Lb5=new Label("无",Label.LEFT); //执行错误抛出 static Label Lb6=new Label("对象BS字符长度 : ",Label.LEFT); static Label Lb7=new Label("对象str22字符长度 :",Label.LEFT); static Label Lb8=new Label("对象BS剩余字符长度:",Label.LEFT); static Label Lb9=new Label("错误:",Label.LEFT); static Label Lb10=new Label(".0",Label.RIGHT); //计算器下面的标签 static Label Lb11=new Label("余数:",Label.RIGHT); //余数标签 static Label Lb12=new Label(".0",Label.RIGHT); static TextArea TA=new TextArea("说明:由于软件不够完善,请规范运算的输入:1.可在里面按键输入",1000,TextArea.SCROLLBARS_VERTICAL_ONLY); static TextField TF=new TextField(); static GridLayout GL1=new GridLayout(5,4); //流动布局 static Panel Pl=new Panel(); //面板 static BorderLayout BL=new BorderLayout(); static shijian app1=new shijian(); //实现监听器的类对象 static char Data2[]=new char[5]; static Font fnt=new Font("Serief",Font.ITALIC+Font.BOLD,18); static Font fnt2=new Font("Serief",Font.BOLD,15); static String str22; static StringBuffer BS=new StringBuffer(""); //可创建空的字符串对象,这个很重要 static String str23=str22; public static void main(String args[]) throws IOException { frm1.setLayout(null); Pl.setLayout(GL1);//面板加入布局 frm1.setSize(253,450);//没有设置窗口大小 frm1.setLocation(250,450); Lb1.setBounds(0,55,250,25);//计算器数字输入显示框之一上边框 Pl.setBounds(0,80,250,290); Lb2.setBounds(370,60,50,25); Lb3.setBounds(370,90,50,25); Lb4.setBounds(370,120,50,25); Lb5.setBounds(300,150,320,25); Lb6.setBounds(250,60,120,25); Lb7.setBounds(250,90,120,25); Lb8.setBounds(250,120,120,25); Lb9.setBounds(250,150,30,25); Lb10.setBounds(0,365,250,30);//计算器数字输入显示框之一下边框 Lb11.setBounds(250,30,120,25); Lb12.setBounds(370,30,50,25); TF.setBounds(0,30,250,25); TA.setBounds(0,395,250,75); TA.setEditable(false); Pl.add(btn1);//面板 Pl.add(btn2); Pl.add(btn3); Pl.add(btn4); Pl.add(btn5); Pl.add(btn6); Pl.add(btn7); Pl.add(btn8); Pl.add(btn9);//加入按钮 Pl.add(btn14); Pl.add(btn16); Pl.add(btn17); Pl.add(btn10);//加 Pl.add(btn11);//减 Pl.add(btn12);//乘 Pl.add(btn13);//除 Pl.add(btn15);//这里是等号注意事件的监听器在不同的类上 Pl.add(btn17); Pl.add(btn18); Pl.add(btn19); Pl.add(btn20); frm1.add(Pl);//加入面板 frm1.addWindowListener(new shijian2()); frm1.add(Lb1); frm1.add(Lb2); frm1.add(Lb3); frm1.add(Lb4); frm1.add(Lb5); frm1.add(Lb6); frm1.add(Lb7); frm1.add(Lb8); frm1.add(Lb9); frm1.add(Lb10); frm1.add(Lb11); frm1.add(Lb12); frm1.add(TA); frm1.add(TF); //Lb2.setBackground(Color.pink); //Lb3.setBackground(Color.pink); //Lb4.setBackground(Color.pink); //Lb5.setBackground(Color.white); Lb6.setBackground(Color.white); Lb7.setBackground(Color.white); Lb8.setBackground(Color.white); Lb9.setBackground(Color.white); Lb10.setBackground(Color.gray); Lb11.setBackground(Color.white); TF.setBackground(Color.pink); //TA.setBackground(Color.pink); //Lb12.setBackground(Color.gray); Lb1.setFont(fnt); Lb2.setFont(fnt); Lb3.setFont(fnt); Lb4.setFont(fnt); Lb10.setFont(fnt); Lb11.setFont(fnt2); Lb12.setFont(fnt); Lb1.setBackground(Color.gray); btn1.addActionListener(app1); btn2.addActionListener(app1); btn3.addActionListener(app1); btn4.addActionListener(app1); btn5.addActionListener(app1); btn6.addActionListener(app1); btn7.addActionListener(app1); btn8.addActionListener(app1); btn9.addActionListener(app1); btn10.addActionListener(app1); btn11.addActionListener(app1); btn12.addActionListener(app1); //btn12.addActionListener(app1);//写两个这个则会被触发两次事件 btn13.addActionListener(app1); btn14.addActionListener(app1); btn15.addActionListener(app1); btn16.addActionListener(app1); btn17.addActionListener(app1); btn18.addActionListener(app1); btn19.addActionListener(app1); btn20.addActionListener(app1); TF.addTextListener(new shijian3()); frm1.setBackground(Color.white); frm1.setResizable(false); frm1.setVisible(true);//可见 } static class shijian implements ActionListener//接口的实现 { public void actionPerformed(ActionEvent e) { Button BT=(Button)e.getSource(); //取得事件的对象,用于判断事件所属 String str1=BT.getLabel(); //取得按钮的名字,也就是相应的数字 if(str1=="0"||str1=="1"||str1=="2"||str1=="3"||str1=="4"||str1=="5"||str1=="6"||str1=="7"||str1=="8"||str1=="9"||str1=="+"||str1=="-"||str1=="*"||str1=="/"||str1=="."||str1=="π") { if(str1=="π"){str1=Double.toString(Math.PI);} BS.append(str1);//将得到的字符串加入到原有字符串 String str22=BS.toString();//转换成字符串 String str56=Integer.toString(str22.length());//将整形转换成字符串 Lb10.setText(str22); Lb2.setText(str56); Lb3.setText(str56); Lb4.setText(str56); Lb5.setText("无"); Lb12.setText(".0"); Lb1.setText(str22); try{ FileWriter FQ=new FileWriter("D:\\java\\10.txt");FQ.write(str22);FQ.flush();} catch(Exception u) {System.out.println("出错了");}} if(str1=="=")//如果等于等号if里面包含多个if或者while { // System.out.println("str22="+str22); char Data[]=new char[BS.length()]; try{ FileReader FV=new FileReader("D:\\java\\10.txt");//写入所需数据 FV.read(Data);} //这是我们写入的数据, catch(Exception y){System.out.println("出错了");} while(k

文件下载

评论信息

免责申明

【只为小站】的资源来自网友分享,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,【只为小站】 无法对用户传输的作品、信息、内容的权属或合法性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论 【只为小站】 经营者是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。
本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二条之规定,若资源存在侵权或相关问题请联系本站客服人员,zhiweidada#qq.com,请把#换成@,本站将给予最大的支持与配合,做到及时反馈和处理。关于更多版权及免责申明参见 版权及免责申明