计算器java

来源:互联网 发布:倒计日软件 编辑:程序博客网 时间:2024/05/02 03:04
 package home; import java.awt.*; import java.awt.event.*; public class MyTestMyCalculator extends Frame { private static final long serialVersionUID = 1L; private MyTestMyCalculator cal = this;//定义计算器对象cal private TextField tf;//定义文本框tf private String s=""; double aa=0;//定义被动数 例如A+B 中的 A double bb=0;//定义主动数 例如A+B 中的 B private String c="=";//定义运算符号 例如A+B 中的 + private String strA=""; private String strB=""; private int count=0; private String fuhao; MyTestMyCalculator() { super("小禾牌计算器"); this.setLayout(new BorderLayout(5, 5)); this.setBackground(Color.ORANGE); MyWindowListener mywlis = new MyWindowListener();//定义窗口接听对象mywlis this.addWindowListener(mywlis); ActionListener myalis = new MyActionListener(); tf = new TextField("", 20); this.add(tf, BorderLayout.NORTH); //生成中间德3个button------------------------------------------------- Panel p2 = new Panel(new GridLayout(1, 3, 5, 5)); this.add(p2, BorderLayout.CENTER); Button bb1 = new Button("BlackSpace"); Button bb2 = new Button("CE"); Button bb3 = new Button("C"); bb1.setBackground(Color.pink); bb2.setBackground(Color.CYAN); bb3.setBackground(Color.pink); p2.add(bb1); p2.add(bb2); p2.add(bb3); bb1.addActionListener(myalis); bb2.addActionListener(myalis); bb3.addActionListener(myalis); //生成主体button--------------------------- Panel p1 = new Panel(new GridLayout(4, 5, 5, 5)); String[] s = { "7", "8", "9", "/", "sqrt", "4", "5", "6", "*", "%", "1", "2", "3", "-", "1/x", "0", "+/-", ".", "+", "="}; for (int i = 0; i < s.length; i++) { Button b = new Button(s[i]); b.setBackground(Color.lightGray); p1.add(b); b.addActionListener(myalis); } this.add(p1, BorderLayout.SOUTH); this.pack(); this.setResizable(false); this.setVisible(true); } //核心部分控制button--------------------------------------------------- private class MyActionListener implements ActionListener { String str1="0123456789."; String str2="+-*/="; public void actionPerformed(ActionEvent e) { Object obj=e.getSource(); Button b=(Button)obj; methordB(b);//变色功能 methordA(b); } public void methordA(Button b){//算法!!!!!!!!!!!!!************* b.getActionCommand(); if(str1.indexOf(b.getActionCommand())!=-1){//是数字 strB+=b.getActionCommand(); bb=Double.valueOf(strB).doubleValue(); tf.setText(strB); } if(str2.indexOf(b.getActionCommand())!=-1){//是运算符号 strB=""; methordA1(b.getActionCommand());//调用运算方法methordA1() } public void methordA1(String fuhao){ //运算功能 if(bb==0){ tf.setText(aa+""); }else{ if(c.equals("=")){ aa=bb; }else if(c.equals("+")){ aa=aa+bb; }else if(c.equals("-")){ aa=aa-bb; }else if(c.equals("*")){ aa=aa*bb; }else if(c.equals("/")){ aa=aa/bb; } } tf.setText(""+aa); bb=0; c=fuhao; } if(b.getActionCommand().equals("+/-")){//添加+,-号 if(bb==0){ aa=aa*(-1); tf.setText(aa+""); }else{ bb=bb*(-1); strB=bb+""; tf.setText(bb+""); } } if(b.getActionCommand().equals("BlackSpace")){//BlackSpace功能 strB=String.valueOf(bb).substring(0,(strB.length()-1)); bb=Double.valueOf(strB).doubleValue(); tf.setText(bb+""); } if(b.getActionCommand().equals("C")){//总体清零功能 strB=""; bb=0; aa=0; strB=bb+""; tf.setText(bb+""); } if(b.getActionCommand().equals("CE")){//单步骤清零功能 count=0; if(bb==0){ aa=0; tf.setText(aa+""); }else{ bb=0; strB=bb+""; tf.setText(bb+""); } } if(b.getActionCommand().equals("1/x")){//1/x功能 if(bb==0){ aa=1/aa; tf.setText(aa+""); }else{ bb=1/bb; strB=bb+""; tf.setText(bb+""); } } if(b.getActionCommand().equals("sqrt")){//sqrt功能 if(bb==0){ aa=Math.sqrt(aa); tf.setText(aa+""); }else{ bb=Math.sqrt(bb); strB=bb+""; tf.setText(bb+""); } } if(b.getActionCommand().equals("%")){//%号功能 if(bb==0){ aa=Math.sqrt(aa); tf.setText(aa+""); }else{ bb=Math.sqrt(bb); strB=bb+""; tf.setText(bb+""); } } } public void methordB(Button b){//变色功能 if (count % 3== 0) b.setBackground(Color.CYAN); else if (count % 3 == 1) b.setBackground(Color.red); else if(count%3==2) b.setBackground(Color.PINK); count++; } } // 控制窗口 关闭 ----------------------------------------------------------------- private class MyWindowListener extends WindowAdapter{ public void windowClosing(WindowEvent e) { System.exit(0); } } //----------------------------------------------------------------------------- public static void main(String[] args) { MyTestMyCalculator testmycalculator = new MyTestMyCalculator(); } }