计算器代码(Java)
2016-08-28 21:12
155 查看
package jyh; import java.awt.*; import java.applet.*; import java.awt.event.*; import javax.swing.*; public class helloa extends Applet implements ActionListener { String x="",y="",symbol=""; double answer; JTextField tfAnswer; Button button[]=new Button[10]; Button Jia,Jian,Cheng,Chu,ReStart,Pow,Sqrt,Deng,Fu,Point,sin,cos,tan,ln; public void init() { this.setBackground(Color.CYAN); setLayout(null); tfAnswer=new JTextField(); tfAnswer.setBounds(5, 5, 265, 30); tfAnswer.setHorizontalAlignment(JTextField.RIGHT); tfAnswer.setText("0."); add(tfAnswer); SetButton(button[9],"9",95,50,40,30,Color.blue); SetButton(button[8],"8",50,50,40,30,Color.blue); SetButton(button[7],"7",5,50,40,30,Color.blue); SetButton(button[6],"6",95,85,40,30,Color.blue); SetButton(button[5],"5",50,85,40,30,Color.blue); SetButton(button[4],"4",5,85,40,30,Color.blue); SetButton(button[3],"3",95,120,40,30,Color.blue); SetButton(button[2],"2",50,120,40,30,Color.blue); SetButton(button[1],"1",5,120,40,30,Color.blue); SetButton(button[0],"0",5,155,40,30,Color.blue); SetButton(Jia,"+",140,155,40,30,Color.red); SetButton(Jian,"-",140,120,40,30,Color.red); SetButton(Cheng,"*",140,85,40,30,Color.red); SetButton(Chu,"/",140,50,40,30,Color.red); SetButton(ReStart,"ReStart",185,50,40,30,Color.red); SetButton(Pow,"^",185,85,40,30,Color.blue); SetButton(Sqrt,"sqrt",185,120,40,30,Color.blue); SetButton(Deng,"=",185,155,40,30,Color.red); SetButton(Fu,"+/-",50,155,40,30,Color.blue); SetButton(Point,".",95,155,40,30,Color.blue); SetButton(sin, "sin", 230, 50, 40, 30, Color.red); SetButton(cos, "cos", 230, 85, 40, 30, Color.red); SetButton(tan, "tan", 230, 120, 40, 30, Color.red); SetButton(ln, "ln", 230, 155, 40, 30, Color.red); } public void SetButton(Button name,String symbol,int x,int y,int width,int height,Color color) { setLayout(null); name=new Button(symbol); name.setBounds(x,y,width,height); name.setBackground(Color.white); name.setForeground(color); name.addActionListener(this); add(name); } public void Equal(String z) { if(z.equals("+")) answer=Double.parseDouble(x)+Double.parseDouble(y); if(z.equals("-")) answer=Double.parseDouble(x)-Double.parseDouble(y); if(z.equals("*")) answer=Double.parseDouble(x)*Double.parseDouble(y); if(z.equals("/")) answer=Double.parseDouble(x)/Double.parseDouble(y); if(z.equals("^")) answer=Math.pow(Double.parseDouble(x),Double.parseDouble(y)); if(z.equals("sin")) answer=Math.sin(Double.parseDouble(x)); if(z.equals("cos")) answer=Math.cos(Double.parseDouble(x)); if(z.equals("tan")) answer=Math.tan(Double.parseDouble(x)); if(z.equals("ln")) answer=Math.log(Double.parseDouble(x)); x=Double.toString(answer); tfAnswer.setText(x); y="";symbol=""; } public void actionPerformed(ActionEvent e) throws IndexOutOfBoundsException { if ( e.getActionCommand().equals("0")|| e.getActionCommand().equals("1")|| e.getActionCommand().equals("2")|| e.getActionCommand().equals("3")|| e.getActionCommand().equals("4")|| e.getActionCommand().equals("5")|| e.getActionCommand().equals("6")|| e.getActionCommand().equals("7")|| e.getActionCommand().equals("8")|| e.getActionCommand().equals("9") ) { if (symbol.equals("")) { x=x+e.getActionCommand(); if (x.startsWith("00")) { x=x.substring(1); } tfAnswer.setText(x); } else { y=y+e.getActionCommand(); if (y.startsWith("00")) { y=y.substring(1); } tfAnswer.setText(y); } } if (e.getActionCommand().equals(".")) { if (symbol.equals("")) { int i=0,j=0; for (i=0;i<x.length();i++) { if (x.charAt(i)=='.') { j++; } } if (j==0) { x=x+"."; } tfAnswer.setText(x); } else { int i=0,j=0; for (i=0;i<y.length();i++) { if (y.charAt(i)=='.') { j++; } } if (j==0) { y=y+"."; } tfAnswer.setText(y); } } if (e.getActionCommand().equals("ReStart")) { x="";y="";symbol=""; tfAnswer.setText("0."); } if (e.getActionCommand().equals("+/-")) { if (symbol.equals("")) { if(x.substring(0,1).equals("-")) { x=x.substring(1); } else { x="-"+x; } tfAnswer.setText(x); } else { if(y.substring(0,1).equals("-")) { y=y.substring(1); } else { y="-"+y; } tfAnswer.setText(y); } } if(e.getActionCommand().equals("sqrt")) { if(symbol!="") { Equal(symbol); } answer=Math.sqrt(Double.parseDouble(x)); x=Double.toString(answer); tfAnswer.setText(x); } if(e.getActionCommand().equals("+")) { if(symbol!="") Equal(symbol); symbol="+"; } if(e.getActionCommand().equals("-")) { if(symbol!="") Equal(symbol); symbol="-"; } if(e.getActionCommand().equals("*")) { if(symbol!="") Equal(symbol); symbol="*"; } if(e.getActionCommand().equals("/")) { if(symbol!="") Equal(symbol); symbol="/"; } if(e.getActionCommand().equals("^")) { if(symbol!="") Equal(symbol); symbol="^"; } if(e.getActionCommand().equals("sin")) { if(symbol!="") Equal(symbol); answer=Math.sin(Double.parseDouble(x)); x=Double.toString(answer); tfAnswer.setText(x); } if(e.getActionCommand().equals("ln")) { if(symbol!="") Equal(symbol); answer=Math.log(Double.parseDouble(x)); x=Double.toString(answer); tfAnswer.setText(x); } if(e.getActionCommand().equals("cos")) { if(symbol!="") Equal(symbol); answer=Math.cos(Double.parseDouble(x)); x=Double.toString(answer); tfAnswer.setText(x); } if(e.getActionCommand().equals("tan")) { if(symbol!="") Equal(symbol); answer=Math.tan(Double.parseDouble(x)); x=Double.toString(answer); tfAnswer.setText(x); } if(e.getActionCommand().equals("=")) Equal(symbol); } }
相关文章推荐
- 今天没怎么写程序,就用JAVA调了下记事本,计算器,嘿嘿,写一个代码也是热身呀~~~
- java 计算器代码
- java学习后,复习,写计算器代码(第一阶段)
- java代码实例 使用switch实现简易的计算器(实现加减乘除)
- java代码------计算器核心位置添加
- java 计算器代码
- 一段简单的手写Java计算器代码
- java学习后,复习,写计算器代码(第三阶段)
- java代码---------计算器实现
- java代码------计算器
- 一个比较好的Java写的计算器代码
- java 计算器 仅300多行代码
- java代码写的图形化个人收入计算器
- java学习后,复习,写计算器代码(第二阶段)
- Java AWT实现的计算器代码
- 计算器基础原理代码(java)
- java实现计算器的代码
- java代码实例 使用switch实现简易的计算器(实现加减乘除)
- 一个比较好的Java写的计算器代码 (转)
- java 计算器完整版,代码!