首页 [原创]jisuanqi

[原创]jisuanqi

举报
开通vip

[原创]jisuanqi[原创]jisuanqi jisuanqi 一、 public class Calculator{ private String result = "0"; private int op = 0,add = 1,sub = 2,mul = 3,div = 4 private double stringToDouble(String x){ double y = Double.parseDouble(x); return y; } private void operate(String x){ do...

[原创]jisuanqi
[原创]jisuanqi jisuanqi 一、 public class Calculator{ private String result = "0"; private int op = 0,add = 1,sub = 2,mul = 3,div = 4 private double stringToDouble(String x){ double y = Double.parseDouble(x); return y; } private void operate(String x){ double x1 = stringToDouble(x); double y = stringToDouble(result); switch (op){ case 0: result = x; break; case 1: result = String.valueOf(y+x1); break; case 2: result = String.valueOf(y-x1); break; ase 3: c result = String.valueOf(y*x1); break; case 4: if(x1!=0){ result = String.valueOf(y/x1); }else{ result = "The divisor can't be zero!"; } break; } } public String opAdd(String x){ operate(x); op = add; return result; } public String opSubtract(String x){ operate(x); op = sub; return result; } public String opMultiply(String x){ operate(x); op = mul; return result; } public String opDivide(String x){ operate(x); op = div; return result; } public String opEquals(String x){ operate(x); op = 0; return result; } public void opClean(){ op = 0; result = "0"; } } 二、 CalculatorGUI.java import java.awt.*; import java.awt.event.*; import java.util.EventObject; public class CalculatorGUI{ private Frame f; private Panel p1,p2; private Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9; private Button bPoint,bAdd,bDec,bMul,bDiv,bCal; private TextField tf; private String s,op; private Calculator cal = new Calculator(); private boolean ifOp; public CalculatorGUI(){ f = new Frame("Calculator"); p1 = new Panel(); p2 = new Panel(); b0 = new Button("0"); b1 = new Button("1"); b2 = new Button("2"); b3 = new Button("3"); b4 = new Button("4"); b5 = new Button("5"); b6 = new Button("6"); b7 = new Button("7"); b8 = new Button("8"); b9 = new Button("9"); bPoint = new Button("."); bAdd = new Button("+"); bDec = new Button("-"); bMul = new Button("*"); bDiv = new Button("/"); bCal = new Button("="); tf = new TextField(25); tf.setEditable(false); } public void launchFrame(){ f.setSize(220,160); f.setResizable(false); f.addWindowListener(new myWindowListener()); p1.setLayout(new FlowLayout(FlowLayout.CENTER)); p1.add(tf); f.add(p1,BorderLayout.NORTH); p2.setLayout(new GridLayout(4,4)); b0.addActionListener(new setLabelText_ActionListener()); b1.addActionListener(new setLabelText_ActionListener()); b2.addActionListener(new setLabelText_ActionListener()); b3.addActionListener(new setLabelText_ActionListener()); b4.addActionListener(new setLabelText_ActionListener()); b5.addActionListener(new setLabelText_ActionListener()); b6.addActionListener(new setLabelText_ActionListener()); b7.addActionListener(new setLabelText_ActionListener()); b8.addActionListener(new setLabelText_ActionListener()); b9.addActionListener(new setLabelText_ActionListener()); bPoint.addActionListener(new setLabelText_ActionListener()); bAdd.addActionListener(new setOperator_ActionListener()); bDec.addActionListener(new setOperator_ActionListener()); bMul.addActionListener(new setOperator_ActionListener()); bDiv.addActionListener(new setOperator_ActionListener()); bCal.addActionListener(new setOperator_ActionListener()); p2.add(b7); p2.add(b8); p2.add(b9); p2.add(bAdd); p2.add(b4); p2.add(b5); p2.add(b6); p2.add(bDec); p2.add(b1); p2.add(b2); p2.add(b3); p2.add(bMul); p2.add(b0); p2.add(bPoint); p2.add(bCal); p2.add(bDiv); f.add(p2,BorderLayout.SOUTH); f.setVisible(true); } public void setTextFieldText_Temp(){ if (tf.getText().length()<15 && (tf.getText().indexOf(".")==-1 || !s.equals("."))){ tf.setText(tf.getText()+s); }else{ tf.setText((tf.getText()+s).substring(0,15)); } } public void setTextFieldText(){ if(ifOp){ ifOp = false; tf.setText(""); setTextFieldText_Temp(); }else{ setTextFieldText_Temp(); } } public static void main(String[] args){ CalculatorGUI calculator = new CalculatorGUI(); calculator.launchFrame(); } class myWindowListener extends WindowAdapter{ public void windowClosing(WindowEvent e){ System.exit(0); } } class setLabelText_ActionListener implements ActionListener{ public void actionPerformed(ActionEvent e){ Button tempB = (Button)e.getSource(); s = tempB.getLabel(); setTextFieldText(); } } class setOperator_ActionListener implements ActionListener{ public void actionPerformed(ActionEvent e){ Button tempB = (Button)e.getSource(); op = tempB.getLabel(); if(op.equals("+")){ tf.setText(cal.opAdd(tf.getText())); ifOp = true; }else if(op.equals("-")){ tf.setText(cal.opSubtract(tf.getText())); ifOp = true; }else if(op.equals("*")){ tf.setText(cal.opMultiply(tf.getText())); ifOp = true; }else if(op.equals("/")){ tf.setText(cal.opDivide(tf.getText())); ifOp = true; }else if(op.equals("=")){ tf.setText(cal.opEquals(tf.getText())); ifOp = true; } } } }
本文档为【[原创]jisuanqi】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_997338
暂无简介~
格式:doc
大小:25KB
软件:Word
页数:0
分类:企业经营
上传时间:2018-05-06
浏览量:8