java简单计算器源代码
用java代码写的简易计算器(可以实现基本的加减乘除功能)

⽤java代码写的简易计算器(可以实现基本的加减乘除功能)package A;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import javax.swing.*;public class Calcular3 extends JFrame implements ActionListener,MouseListener{private int m1=0,n=0;//private double m2=0;//运算的数private int flag=0;JFrame f;JPanel p1,p2,p3;JTextField t;JButton b1[]=new JButton[18];String b[]= {"1","2","3","4","5","6","7","8","9","0","清空","退格",".","=","+","-","*","/"};public Calcular3(){f=new JFrame("计算器");t=new JTextField(35);p1=new JPanel();p2=new JPanel();p3=new JPanel();f.setBounds(100, 100, 400, 200);f.add(p1,BorderLayout.NORTH);f.add(p2,BorderLayout.CENTER);f.add(p3,BorderLayout.EAST);p2.setLayout(new GridLayout(5,3));p3.setLayout(new GridLayout(4,1));p1.add(t);for(int i=0;i<14;i++) {b1[i]=new JButton(b[i]);p2.add(b1[i]);b1[i].addActionListener(this);}for(int i=14;i<18;i++) {b1[i]=new JButton(b[i]);p3.add(b1[i]);b1[i].addActionListener(this);}/*for(int i=0;i<18;i++) {b1[i].addActionListener(this);}*/f.setVisible(true);}//实现接⼝的⽅法public void mouseClicked(MouseEvent e) {}public void mousePressed(MouseEvent e) {}public void mouseReleased(MouseEvent e) {}public void mouseEntered(MouseEvent e) {}public void mouseExited(MouseEvent e) {}public void actionPerformed(ActionEvent e) {String str="";int i;for(i=0;i<=9;i++) {if(e.getSource()==b1[i]) {if(i==9) {n=n*10;}else {n=n*10+i+1;}str=String.valueOf(n);//整形n转换成字符串strt.setText(str);//显⽰到⽂本框上}}for(i=14;i<18;i++) {//+、-、*、/if(e.getSource()==b1[i]) {//匹配运算符m1=Integer.parseInt(t.getText());if(flag==15) {m2=m1+m2;}else if(flag==16) {m2=m1-m2;}else if(flag==17) {m2=m1*m2;}else if(flag==18) {m2=m1/m2;}else m2=m1;//若⽆连续的运算符运算,保存当前数据到m2 if(i==14) flag=15;else if(i==15) flag=16;else if(i==16) flag=17;else flag=18;str=String.valueOf(b[i]);t.setText(str);//显⽰到⽂本框上n=0;//还原,记录下次数据break;//找到匹配数据退出循环}}if(e.getSource()==b1[13]) {//=m1=Integer.parseInt(t.getText());if(flag==15) {m2=m2+m1;}else if(flag==16) {m2=m2-m1;}else if(flag==17) {m2=m2*m1;}else if(flag==18) {m2=m2/m1;}else m2=m1;str=String.valueOf(m2);t.setText(str);//显⽰到⽂本框上n=0;//还原,记录下次数据flag=0;//flag还原0,表明没有未处理的运算符}if(e.getSource()==b1[10]) {//各变量变为0 清空m1=0;m2=0;flag=0;n=0;t.setText("0");//显⽰到⽂本框上}if(e.getSource()==b1[11]) {//退格m1=(int)(Double.parseDouble(t.getText())/10);n=m1;str=String.valueOf(m1);t.setText(str);}if(e.getSource()==b1[12]) {//⼩数点m1=Integer.parseInt(t.getText());str=String.valueOf(m1+b[12]);t.setText(str);//显⽰到⽂本框上int j=0;for(i=0;i<=9;i++) {if(e.getSource()==b1[i]) {j++;m2=Math.pow(0.1, j)*Integer.parseInt(b[i]);str=String.valueOf(m1+m2);t.setText(str);//显⽰到⽂本框上}}}}//主函数public static void main(String[] args) {new Calcular3();}}。
JAVA简易计算器程序源代码

JAVA简易计算器程序源代码package myText;import java.awt.*;import javax.swing.*;import java.awt.event.*;import javax.swing.border.*;public class Calculator extends JFrame implements ActionListener{ private JPanel Panel1=new JPanel();private JPanel Panel2=new JPanel();private JTextField tfResult=new JTextField(25);private JLabel label=new JLabel("计算结果:");private String recentOperation=null;private String recentNum=null;private boolean isNew=true;public void addButton(Container c,String s){JButton b=new JButton(s);b.setFont(new java.awt.Font("SansSerif",0,12));b.setForeground(Color.red);b.setBorder(BorderFactory.createRaisedBevelBorder());c.add(b);b.addActionListener(this);}//end public void addButton(Container c,String s)public void actionPerformed(ActionEvent e){String s=e.getActionCommand();if(s.charAt(0)>='0'&&s.charAt(0)<='9'){if(!isNew){tfResult.setText(tfResult.getText()+s);}//end if(!isNew)else{tfResult.setText(s);}//end elseisNew=false;}//end if(s.charAt(0)>='0'&&s.charAt(0)<='9') else if(s.equals(".")){if(tfResult.getText().indexOf(".")!=-1)return;if(!isNew&&tfResult.getText()!=""){tfResult.setText(tfResult.getText()+".");}//end ifelse{tfResult.setText("0.");}//end elseisNew=false;}//end if(s.equals("."))else if(s.equals("=")){equalaction(e);}//end if(s.equals("="))else{if((tfResult.getText()).equals("")){return;}//endif((tfResult.getText()).equals(""))if(recentOperation!=null){equalaction(e);}//end if(recentOperation!=null) recentOperation=s;recentNum=tfResult.getText();isNew=true;}//end else}//end public void actionPerformed(ActionEvent e) void equalaction(ActionEvent e){if(recentOperation==null||recentNum==null||tfResult.getTex t().equals("")){ return;}//end if()double last=0,now=0;try{last=Double.parseDouble(recentNum);now=Double.parseDouble(tfResult.getText());}//end trycatch(NumberFormatException ne){recentOperation=null;recentNum=null;tfResult.setText("数据输入不合法");System.out.println("数据输入不合法");isNew=true;return;}//end catch(NumberFormatException ne)if(recentOperation.equals("+")){last+=now;}//end if(recentOperation.equals("+"))if(recentOperation.equals("-")){last-=now;}//endif(recentOperation.equals("-"))if(recentOperation.equals("*")){last*=now;}//endif(recentOperation.equals("*"))if(recentOperation.equals("/")){last/=now;}//endif(recentOperation.equals("/"))tfResult.setText(""+last);recentNum=tfResult.getText();recentOperation=null;isNew=true;}//end void equalaction(ActionEvent e)public Calculator(){tfResult.setBorder(BorderFactory.createLoweredBevelBorder ());tfResult.setEditable(false);tfResult.setText("0");tfResult.setHorizontalAlignment(SwingConstants.RIGHT);////本java源代码由839682048整理修改//////Panel1.add(label,FlowLayout.LEFT);Panel1.add(tfResult);////本java源代码由839682048整理修改///Panel2.setLayout(new GridLayout(4,4,2,2));String buttons="789/456*123-0.=+";for(int i=0;i<buttons.length();i++){< p="">addButton(Panel2,buttons.substring(i,i+1));}//end for()//本java源代码由839682048整理修改///this.getContentPane().add(Panel1,"North");this.getContentPane().add(Panel2,"Center");this.setResizable(false);this.setTitle("计算器");this.addWindowListener(newjava.awt.event.WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);}});}//end public Calculator1()public static void main(String[]args){Calculator mf=new Calculator();mf.setBounds(200,200,400,400);mf.show();}//end main()}//end public class Calculator1extends JFrame implements ActionListener</buttons.length();i++){<>。
计算器的Java源代码

button_7.setText("7");
button_8.setFont(new Font("SansSerif",0,12));
button_8.setForeground(Color.blue);
button_4.setBorder(BorderFactory.createRaisedBevelBorder());
button_4.setText("4");
button_5.setFont(new Font("SansSerif",0,12));
button_5.setForeground(Color.blue);
//保存最近一次运算符
String recentOperation=null;
//保存最近一次的运算数据
String recentNum=null;
//描述当前输入状态,是重新输入还是接在后面,重新输入时为true
boolean isNew=true;
public Calculator(){
button_4.addActionListener(this);
button_5.addActionListener(this);
button_6.addActionListener(this);
button_7.addActionListener(this);
button_8.addActionListener(this);
JButton button_CE=new JButton();
java简单计算器源代码

简单计算器代码package calcultorthree;import java.awt.BorderLayout;//导入边界布局管理器类import java.awt.GridLayout;//导入网格布局管理器类import java.awt.T extField;//导入文本区域类import java.awt.event.ActionEvent;//导入事件类import java.awt.event.ActionListener;//导入事件监听者类import javax.swing.JButton;//导入按钮类import javax.swing.JFrame;//导入窗体import javax.swing.JPanel;//导入面板/***本例实现了简单计算器代码,具备加减乘除和正弦功能,旨在抱砖引玉。
熟悉java的同学,可以在此基础上实现更复杂的功能。
* author Fjsh*/public class CalcultorThree {//新建对象,在构造函数中进行初始化JFrame frame;//新建窗体对象JButton buttonzero,buttondot,buttonequal;//新建按钮“0”“.”“=”JButton buttonplus,buttonminus,buttonmultiple,buttondevision,buttonsin,buttontozero;//新建按钮“+”“-”“*”“/”“sin”和归零按钮JButton buttonone,buttontwo,buttonthree,buttonfour,buttonfive,buttonsix, buttonseven,buttoneight,buttonnine;//新建数字按钮“0”“1”“2”“3”“4”“5”“6”“7”“8”“9”JPanel panelwest,panelcenter,paneleast;//新建三个面板T extField tf;//新建文本区域对象public CalcultorThree(){//初始化对象tf=new T extField(30);//构造空文本字段,字符宽度为30frame =new JFrame("CalculatorThree");//构造窗体对象,名称为“CalculatorThree”panelcenter=new JPanel();//构造面板,放到窗体中央panelwest=new JPanel();//构造面板,放到窗体西边paneleast=new JPanel();//构造面板,放到窗体东边Handle h=new Handle();//新建Handle类对象,Handle类为事件监听类//创建数字按钮对象,1、2、3、4、5、6、7、8、9buttonone=new JButton("1");buttontwo=new JButton("2");buttonthree=new JButton("3");buttonfour=new JButton("4");buttonfive=new JButton("5");buttonsix=new JButton("6");buttonseven=new JButton("7");buttoneight=new JButton("8");buttonnine=new JButton("9");panelcenter.setLayout(new GridLayout(3,3));//设置面板布局为网格布局,3行3列//将数字按钮添加到中间面板panelcenter.add(buttonone);panelcenter.add(buttontwo);panelcenter.add(buttonthree);panelcenter.add(buttonfour);panelcenter.add(buttonfive);panelcenter.add(buttonsix);panelcenter.add(buttonseven);panelcenter.add(buttoneight);panelcenter.add(buttonnine);//为10个按钮注册事件监听器buttonone.addActionListener(h);buttontwo.addActionListener(h);buttonthree.addActionListener(h);buttonfour.addActionListener(h);buttonfive.addActionListener(h);buttonsix.addActionListener(h);buttonseven.addActionListener(h);buttoneight.addActionListener(h);buttonnine.addActionListener(h);//构造按钮“0”“.”“=”,注册事件监听器,设置1行3列的布局,添加到到西边的面板buttonzero=new JButton("0");buttondot=new JButton(".");buttonequal=new JButton("=");buttonzero.addActionListener(h);buttondot.addActionListener(h);buttonequal.addActionListener(h);panelwest.setLayout(new GridLayout(3,1));panelwest.add(buttonzero);panelwest.add(buttondot);panelwest.add(buttonequal);//构造操作按钮“+”“-”“*”“/”“sin”“>0”,其中“>0”为归零按钮buttonplus=new JButton("+");buttonminus=new JButton("-");buttonmultiple=new JButton("*");buttondevision=new JButton("/");buttonsin=new JButton("sin");buttontozero=new JButton(">0");paneleast.setLayout(new GridLayout(3,1));//设置西边的布局为3行1列//将操作按钮“+”“-”“*”“/”“sin”“>0”添加到西边的面板中paneleast.add(buttonplus);paneleast.add(buttonminus);paneleast.add(buttonmultiple);paneleast.add(buttondevision);paneleast.add(buttonsin);paneleast.add(buttontozero);//为操作按钮“+”“-”“*”“/”“sin”“>0”注册监听器buttonplus.addActionListener(h);buttonminus.addActionListener(h);buttonmultiple.addActionListener(h);buttondevision.addActionListener(h);buttonsin.addActionListener(h);buttontozero.addActionListener(h);frame.setLayout(new BorderLayout());//设置窗体为边界布局frame.add(paneleast,"East");//将东边面板paneleast添加到窗体的东边frame.add(tf,BorderLayout.NORTH); //将tf文本区域添加到窗体的北边,即顶部frame.add(panelwest,BorderLayout.WEST);//将panelwest面板添加到窗体西边frame.add(panelcenter,BorderLayout.CENTER);//将panelcenter面板添加到窗体中间frame.pack();//设置窗体大小,适合其子组件的首选大小和布局frame.setLocation(500,500);//设置窗体显示位置为(500,500)frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置布局窗体默认关闭方式frame.setVisible(true);//设置窗体可见}public static void main(String[] args) {new CalcultorThree();//主方法中新建对象}class Handle implements ActionListener{//实现动作监听器类int biaozhi=0;//此标志标志加减乘除操作double flag1=0,flag2=0,flag3=0;//flag1、flag2为两个操作数,flag3为结果Overridepublic void actionPerformed(ActionEvent e) { //方法重写try{//此处可能会产生异常,用try、catch捕捉异常,不用处理if(e.getSource()==buttondot){//小数点tf.setT ext("0.");}if(e.getSource()==buttontozero){//归零操作tf.setT ext("");}if(e.getSource()==buttonzero){//按键0操作tf.setT ext(tf.getT ext()+"0");flag1=Double.parseDouble(tf.getT ext());//将文本区域转换成Double类型,赋给flag1}if(e.getSource()==buttonone){//按键1操作tf.setT ext(tf.getT ext()+"1");flag1=Double.parseDouble(tf.getT ext());}else if(e.getSource()==buttontwo){//按键2操作tf.setT ext(tf.getT ext()+"2");flag1=Double.parseDouble(tf.getT ext());}else if(e.getSource()==buttonthree){//按键3操作tf.setT ext(tf.getT ext()+"3");flag1=Double.parseDouble(tf.getT ext());}else if(e.getSource()==buttonfour){//按键4操作tf.setT ext(tf.getT ext()+"4");flag1=Double.parseDouble(tf.getT ext());}else if(e.getSource()==buttonfive){//按键5操作tf.setT ext(tf.getT ext()+"5");flag1=Double.parseDouble(tf.getT ext());}else if(e.getSource()==buttonsix){//按键6操作tf.setT ext(tf.getT ext()+"6");flag1=Double.parseDouble(tf.getT ext());}else if(e.getSource()==buttonseven){//按键7操作tf.setT ext(tf.getT ext()+"7");flag1=Double.parseDouble(tf.getT ext());}else if(e.getSource()==buttoneight){//按键8操作tf.setT ext(tf.getT ext()+"8");flag1=Double.parseDouble(tf.getT ext());}else if(e.getSource()==buttonnine){//按键9操作tf.setT ext(tf.getT ext()+"9");flag1=Double.parseDouble(tf.getT ext());}if(e.getSource()==buttonplus){//加法操作tf.setT ext("");flag2=flag1;biaozhi=0;}if(e.getSource()==buttonminus){//减法操作tf.setT ext("");flag2=flag1;biaozhi=1;}if(e.getSource()==buttonmultiple){//乘法操作tf.setT ext("");flag2=flag1;biaozhi=2;}if(e.getSource()==buttondevision){//除法操作tf.setT ext("");flag2=flag1;biaozhi=3;}if(e.getSource()==buttonsin){//正弦操作flag3=Math.sin(flag1);tf.setT ext(flag3+"");}if(e.getSource()==buttonequal){//等号操作,利用biaozhi判断进行相应加减乘除操作if(biaozhi==0){flag3=flag1+flag2;}if(biaozhi==1){flag3=flag1-flag2;}if(biaozhi==2){flag3=flag1*flag2;}if(biaozhi==3){flag3=flag1/flag2;}tf.setT ext(flag3+""); }}catch(Exception ex){}}}}。
简单的java代码例子

简单的java代码例子1. 使用Java实现一个简单的计算器,可以进行加减乘除运算。
```javaimport java.util.Scanner;public class Calculator {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);System.out.print("请输入第一个数:");double num1 = scanner.nextDouble();System.out.print("请输入运算符(+、-、*、/):");String operator = scanner.next();System.out.print("请输入第二个数:");double num2 = scanner.nextDouble();double result = 0;switch (operator) {case "+":result = num1 + num2;break;case "-":result = num1 - num2;break;case "*":result = num1 * num2;break;case "/":result = num1 / num2;break;default:System.out.println("输入的运算符不正确!");break;}System.out.println("计算结果为:" + result);}}```2. 使用Java实现一个简单的猜数字游戏,随机生成一个1-100之间的整数,让用户猜,直到猜中为止。
```javaimport java.util.Random;import java.util.Scanner;public class GuessNumber {public static void main(String[] args) {Random random = new Random();int number = random.nextInt(100) + 1;Scanner scanner = new Scanner(System.in);int guess;do {System.out.print("请输入你猜的数字(1-100):");guess = scanner.nextInt();if (guess > number) {System.out.println("猜大了!");} else if (guess < number) {System.out.println("猜小了!");}} while (guess != number);System.out.println("恭喜你,猜对了!");}}```3. 使用Java实现一个简单的学生信息管理系统,可以添加、删除、修改、查询学生信息。
java简单计算器--代码

}
}
if(act.equals("sqrt")){try{float num=(float)Math.sqrt(Float.parseFloat(t.getText()));
t.setText(String.valueOf(num));can=true;return;
JOptionPane.ERROR_MESSAGE);return;}
}
}
public float cacu(float a,char c,float b){
float sum;
switch(c){
case '+':sum=a+b;break;
case '-':sum=a-b;break;
t.setText(String.valueOf(num));can=true;return;
}catch(NumberFormatException e1){JOptionPane.showMessageDialog(null, "输入格式错误!", "警告!",
JOptionPane.ERROR_MESSAGE);return;}
p1.add(backButton);
p1.add(clearButton);
backButton.addActionListener(this);
clearButton.addActionListener(this);
p2.add(t);
p2.add(p1);
p2.setBackground(Color.black);
JAVA编写的计算器源代码

JAVA编写的计算器源代码// Calculator.javaimport javax.swing.*; // 引入swing库import java.awt.*; // 引入awt库import java.awt.event.*; // 引入awt.event库public class Calculator extends JFrame implements ActionListener//定义按钮private JButton zero;private JButton one;private JButton two;private JButton three;private JButton four;private JButton five;private JButton six;private JButton seven;private JButton eight;private JButton nine;private JButton point;private JButton equal; private JButton plus; private JButton minus; private JButton multiply; private JButton divide; private JButton backspace; private JButton ac;private JButton ce;private JButton sqrt; private JButton sqr;private JButton plus_minus; private JButton delete; private JButton sin;private JButton cos;private JButton tan;private JButton log;private JButton nfactorial; private JButton cubic; private JButton coln;private JButton factorial;//定义文本框private JTextField resulttext;// 定义boolean变量boolean clrdisp = true; // 昵称确定是否清除计算器显示boolean isCalculate = false; // 是否可以执行计算// 定义String变量,用于存储操作符String optr;//定义存储第一个操作数double num1;//初始化构造函数public Calculato//设置布局setLayout(new BorderLayout();//创建面板JPanel northPanel = new JPanel(;JPanel centerPanel = new JPanel(;JPanel southPanel = new JPanel(;//设置面板布局northPanel.setLayout(new FlowLayout(); centerPanel.setLayout(new GridLayout(4, 5)); southPanel.setLayout(new FlowLayout();//设置计算器显示resulttext = new JTextField(28); northPanel.add(resulttext);resulttext.setEditable(false);//初始化按钮zero = new JButton("0");one = new JButton("1");two = new JButton("2");three = new JButton("3");four = new JButton("4");five = new JButton("5");six = new JButton("6");seven = new JButton("7");eight = new JButton("8");nine = new JButton("9");point = new JButton(".");equal = new JButton("=");plus = new JButton("+");minus = new JButton("-"); multiply = new JButton("*"); divide = new JButton("/"); backspace = new JButton("<-"); ac = new JButton("AC");ce = new JButton("CE");sqrt = new JButton("sqrt");sqr = new JButton("sqr");plus_minus = new JButton("+/-");。
java语言写的计算器源代码附解析

//把代码改成Calc.java编译运行,完整版计算器,最下面是计算器界面import java.awt.*;import java.awt.event.*;public class Calc extends WindowAdapter implements ActionListener, MouseListener{Color cMoveOut=new Color(240 ,240 ,240);Color cMoveIn =new Color( 255,255,55);//状态变量boolean clicked=true;//判断是否单击了小数点boolean clear=true;//判断是否单击了符号位double previous;//记录第一个操作数double next;//记录第二个操作数String fuhao;//记录符号位int first=1;//标记是否开始一次新的运算过程Frame f;Panel p1,p2;TextField tf;Font fnt;Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b0;Button bDiv,bSqrt,bMulti,bMinus,bPercent,bPlus,bReciprocal,bEqual,bDot,bNegative,bBack;Button bPingFang , bDaoShu , bSin, bCos ,bTan;Button bC;GridLayout p2Layout;public void display(){f=new Frame("计算器");f.setSize(280,213);f.setLocation(200,200);f.setBackground(Color.LIGHT_GRAY);f.setResizable(false);p1=new Panel(new FlowLayout());tf=new TextField(35);tf.setText("0.");tf.setEditable(false);p1.add(tf);f.add(p1,BorderLayout.NORTH);p2Layout=new GridLayout(5,5,5,4);p2=new Panel(p2Layout);f.add(p2,BorderLayout.CENTER);fnt=new Font("Serief",Font.BOLD,20);b1=new Button("1");b1.setFont(fnt);b2=new Button("2");b2.setFont(fnt);b3=new Button("3");b3.setFont(fnt);b4=new Button("4");b4.setFont(fnt);b5=new Button("5");b5.setFont(fnt);b6=new Button("6");b6.setFont(fnt);b7=new Button("7");b7.setFont(fnt);b8=new Button("8");b8.setFont(fnt);b7.setFont(fnt);b9=new Button("9");b9.setFont(fnt);b0=new Button("0");b0.setFont(fnt);b9.setFont(fnt);bPingFang=new Button("^2"); bPingFang.setFont(fnt); bDaoShu=new Button("1/X"); bDaoShu.setFont(fnt);bSin=new Button("sin");bSin.setFont(fnt);bCos=new Button("cos"); bCos.setFont(fnt);bTan=new Button("tan"); bTan.setFont(fnt);b1.addActionListener(this);b2.addActionListener(this);b3.addActionListener(this);b4.addActionListener(this);b5.addActionListener(this);b6.addActionListener(this);b7.addActionListener(this);b8.addActionListener(this);b9.addActionListener(this);b0.addActionListener(this); bBack=new Button("Back");//退格bBack.setFont(fnt);bC=new Button("C");//清空bC.setFont(fnt);bDiv=new Button("/");bDiv.setFont(fnt);bSqrt=new Button("sqrt"); bSqrt.setFont(fnt);bMulti=new Button("*"); bMulti.setFont(fnt);bMinus=new Button("-"); bMinus.setFont(fnt);bPercent=new Button("%"); bPercent.setFont(fnt);bPlus=new Button("+"); bPlus.setFont(fnt);bEqual=new Button("="); bEqual.setFont(fnt);bDot=new Button(".");bDot.setFont(fnt);bNegative=new Button("+/-"); bNegative.setFont(fnt); bBack.addActionListener(this);bC.addActionListener(this); bDiv.addActionListener(this); bSqrt.addActionListener(this); bPingFang.addActionListener(this); bPercent.addActionListener(this); bDaoShu.addActionListener(this); bMulti.addActionListener(this); bPlus.addActionListener(this); bMinus.addActionListener(this); bMulti.addActionListener(this); bDiv.addActionListener(this); bSin.addActionListener(this); bCos.addActionListener(this); bTan.addActionListener(this); bEqual.addActionListener(this); bDot.addActionListener(this); bNegative.addActionListener(this); bSqrt.addMouseListener(this); bPingFang.addMouseListener(this); bPercent.addMouseListener(this); bC.addMouseListener(this); bBack.addMouseListener(this);b7.addMouseListener(this);b8.addMouseListener(this);b9.addMouseListener(this); bDiv.addMouseListener(this); bDaoShu.addMouseListener(this); b4.addMouseListener(this);b5.addMouseListener(this);b6.addMouseListener(this); bMulti.addMouseListener(this); bSin.addMouseListener(this);b1.addMouseListener(this);b2.addMouseListener(this);b3.addMouseListener(this); bMinus.addMouseListener(this); bCos.addMouseListener(this);b0.addMouseListener(this); bDot.addMouseListener(this); bEqual.addMouseListener(this); bPlus.addMouseListener(this); bTan.addMouseListener(this);p2.add(bSqrt);p2.add(bPingFang);p2.add(bPercent);p2.add(bC);p2.add(bBack);p2.add(b7);p2.add(b8);p2.add(b9);p2.add(bDiv);p2.add(bDaoShu);p2.add(b4);p2.add(b5);p2.add(b6);p2.add(bMulti);p2.add(bSin);p2.add(b1);p2.add(b2);p2.add(b3);p2.add(bMinus);p2.add(bCos);p2.add(b0);p2.add(bDot);p2.add(bEqual);p2.add(bPlus);p2.add(bTan);f.setV isible(true);f.addWindowListener(this);}public void actionPerformed(ActionEvent e) {if (first ==1)tf.setText("");first=0;Object temp=e.getSource();//退格if (temp== bBack){String s=tf.getText();tf.setText("");for (int i=0;i<s.length()-1;i++){char a=s.charAt(i);tf.setText(tf.getText()+a);}}//清零if (temp==bC){tf.setText("0.");clear=true;first=1;}if (temp==b0){//判断是否单击了符号位if (clear==false)tf.setText("");tf.setText(tf.getText()+"0");}if (temp==b1){if (clear==false)tf.setText("");tf.setText(tf.getText()+"1");clear=true;}if (temp==b2){if (clear==false)tf.setText("");tf.setText(tf.getText()+"2");clear=true;}if (temp==b3){if (clear==false)tf.setText("");tf.setText(tf.getText()+"3");clear=true;}if (temp==b4){if (clear==false)tf.setText("");tf.setText(tf.getText()+"4");clear=true;}if (temp==b5){if (clear==false)tf.setText("");tf.setText(tf.getText()+"5");clear=true;}if (temp==b6){if (clear==false)tf.setText("");tf.setText(tf.getText()+"6");clear=true;}if (temp==b7){if (clear==false)tf.setText("");tf.setText(tf.getText()+"7");clear=true;}if (temp==b8){if (clear==false)tf.setText("");tf.setText(tf.getText()+"8");clear=true;}if (temp==b9){if (clear==false)tf.setText("");tf.setText(tf.getText()+"9");clear=true;}//判断是否含有小数点if (temp == bDot){clicked=true;for (int i=0;i<tf.getText().length();i++)if ('.'==tf.getText().charAt(i)){clicked = false;break;}if (clicked == true){tf.setText(tf.getText()+".");clear=true;}}//bSqrtif(temp == bSqrt){tf.setText( Math.sqrt( Double.parseDouble(tf.getText())) + "" );clear=false;}//bPingFangif(temp == bPingFang ){tf.setText( Math.pow( Double.parseDouble(tf.getText() ), 2 ) + "" );clear=false;}//倒数if(temp == bDaoShu){tf.setText( 1 / Double.parseDouble(tf.getText()) + "" );clear =false;}//Sinif(temp == bSin){tf.setText( Math.sin( Math.toRadians( Double.parseDouble( tf.getText() ) ) ) + "" );clear =false;}//Cosif(temp == bCos){tf.setText( Math.cos( Math.toRadians( Double.parseDouble( tf.getText() ) ) ) + "" );clear =false;}//tanif(temp == bTan){tf.setText( Math.tan( Math.toRadians( Double.parseDouble( tf.getText() ) ) ) + "");clear =false;}//加法运算if (temp==bPlus){previous=Double.parseDouble(tf.getText());fuhao="+";clear=false;//System.out.print("+");}//减法运算if (temp == bMinus){previous=Double.parseDouble(tf.getText());fuhao="-";clear=false;}//乘法运算if (temp == bMulti){previous=Double.parseDouble(tf.getText());fuhao="*";clear=false;}//除法运算bDivif (temp == bDiv){previous=Double.parseDouble(tf.getText());fuhao="/";clear=false;}//求余数if(temp == bPercent){previous=Double.parseDouble( tf.getText());fuhao="%";clear=false;}//等于if (temp==bEqual){try{next = Double.parseDouble(tf.getText());tf.setText("");if (fuhao =="+")tf.setText(previous + next +"");if(fuhao == "-")tf.setText(previous - next +"");if(fuhao == "*")tf.setText(previous * next +"");if(fuhao == "/")tf.setText(previous / next +"");if(fuhao == "%")tf.setText(previous % next +"");clear=false;}catch(Exception ex){tf.setText("请检查输入的数据:" + ex.getMessage()) ;clear=false;}}}public void mouseEntered(MouseEvent e){Object temp= e.getSource();if(temp==bPingFang){//System.out.println( bBack.getBackground() );bPingFang.setBackground(cMoveIn);}if(temp==bSqrt){bSqrt.setBackground(cMoveIn);}if(temp==bPercent){bPercent.setBackground(cMoveIn);}if(temp==bC){bC.setBackground(cMoveIn);}if(temp==bBack){bBack.setBackground(cMoveIn);}if(temp==b7){b7.setBackground(cMoveIn);}if(temp==b8){b8.setBackground(cMoveIn);}if(temp==b9){b9.setBackground(cMoveIn);}if(temp==bDiv){bDiv.setBackground(cMoveIn);}if(temp==bDaoShu){bDaoShu.setBackground(cMoveIn); }if(temp==b4){b4.setBackground(cMoveIn);}if(temp==b5){b5.setBackground(cMoveIn);}if(temp==b6){b6.setBackground(cMoveIn);}if(temp==bMulti){bMulti.setBackground(cMoveIn); }if(temp==bSin){bSin.setBackground(cMoveIn);}if(temp==b1){b1.setBackground(cMoveIn);}if(temp==b2){b2.setBackground(cMoveIn);}if(temp==b3){b3.setBackground(cMoveIn);}if(temp==bMinus){bMinus.setBackground(cMoveIn); }if(temp==bCos){bCos.setBackground(cMoveIn);}if(temp==b0){b0.setBackground(cMoveIn);}if(temp==bDot){bDot.setBackground(cMoveIn);}if(temp==bEqual){bEqual.setBackground(cMoveIn);}if(temp==bPlus){bPlus.setBackground(cMoveIn);}if(temp==bTan){bTan.setBackground(cMoveIn);}}public void mouseExited(MouseEvent e) {Object temp= e.getSource();if(temp==bPingFang){//System.out.println( bBack.getBackground() );bPingFang.setBackground(cMoveOut);}if(temp==bSqrt){bSqrt.setBackground(cMoveOut);}if(temp==bPercent){bPercent.setBackground(cMoveOut);}if(temp==bC){bC.setBackground(cMoveOut);}if(temp==bBack){bBack.setBackground(cMoveOut);}if(temp==b7){b7.setBackground(cMoveOut);}if(temp==b8){b8.setBackground(cMoveOut);}if(temp==b9){b9.setBackground(cMoveOut);}if(temp==bDiv){bDiv.setBackground(cMoveOut);}if(temp==bDaoShu){bDaoShu.setBackground(cMoveOut); }if(temp==b4){b4.setBackground(cMoveOut);}if(temp==b5){b5.setBackground(cMoveOut);}if(temp==b6){b6.setBackground(cMoveOut);}if(temp==bMulti){bMulti.setBackground(cMoveOut); }if(temp==bSin){bSin.setBackground(cMoveOut);}if(temp==b1){b1.setBackground(cMoveOut);}if(temp==b2){b2.setBackground(cMoveOut);}if(temp==b3){b3.setBackground(cMoveOut);}if(temp==bMinus){bMinus.setBackground(cMoveOut); }if(temp==bCos){bCos.setBackground(cMoveOut);}if(temp==b0){b0.setBackground(cMoveOut);}if(temp==bDot){bDot.setBackground(cMoveOut);}if(temp==bEqual){bEqual.setBackground(cMoveOut);}if(temp==bPlus){bPlus.setBackground(cMoveOut);}if(temp==bTan){bTan.setBackground(cMoveOut);}}public static void main(String args[]){Calc frame=new Calc();frame.display();}public void windowClosing(WindowEvent e){System.exit(0);}public void mouseClicked(MouseEvent arg0) {}public void mousePressed(MouseEvent arg0) {}public void mouseReleased(MouseEvent arg0) { }}。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
buttonseven=new JButton("7");
buttoneight=new JButton("8");
buttonnine=new JButton("9");
panelcenter.setLayout(new GridLayout(3,3));//设置面板布局为网格布局,3行3列
* @author Fjsh
*/
public class CalcultorThree {
//新建对象,在构造函数中进行初始化
JFrame frame;//新建窗体对象
JButton buttonzero,buttondot,buttonequal;//新建按钮“0”“.”“=”
JButton buttonplus,buttonminus,buttonmultiple,buttondevision,
import java.awt.event.ActionListener;//导入事件监听者类
import javax.swing.JButton;//导入按钮类
import javax.swing.JFrame;//导入窗体
import javax.swing.JPanel;//导入面板
/**
*本例实现了简单计算器代码,具备加减乘除和正弦功能,旨在抱砖引玉。熟悉java的同学,可以在此基础上实现更复杂的功能。
panelwest.setLayout(new GridLayout(3,1));
panelwest.add(buttonzero);
panelwest.add(buttondot);
panelwest.add(buttonequal);
//构造操作按钮“+”“-”“*”“/”“sin”“>0”,其中“>0”为归零按钮
buttontwo.addActionListener(h);
buttonthree.addActionListener(h);
buttonfour.addActionListener(h);
buttonfive.addActionListener(h);
buttonsix.addActionListener(h);
JPanel panelwest,panelcenter,paneleast;//新建三个面板
TextField tf;//新建文本区域对象
public CalcultorThree(){
//初始化对象
tf=new TextField(30);//构造空文本字段,字符宽度为30
frame =new JFrame("CalculatorThree");//构造窗体对象,名称为“CalculatorThree”
buttondot=new JButton(".");
buttonequal=new JButton("=");
buttonzero.addActionListener(h);
buttondot.addActionListener(h);
buttonequal.addActionListener(h);
//创建数字按钮对象,1、2、3、4、5、6、7、8、9
buttonone=new JButton("1");
buttontwo=new JButton("2");
buttonthree=new JButton("3");
buttonfour=new JButton("4");
buttonfive=new JButton("5");
buttonseven.addActionListener(h);
buttoneight.addActionListener(h);
buttonnine.addActionListener(h);
//构造按钮““.”“=”,注册事件监听器,设置1行3列的布局,添加到到西边的面板
buttonzero=new JButton("0");
//将数字按钮添加到中间面板
panelcenter.add(buttonone);
panelcenter.add(buttontwo);
panelcenter.add(buttonthree);
panelcenter.add(buttonfour);
panelcenter.add(buttonfive);
package calcultorthree;
import java.awt.BorderLayout;//导入边界布局管理器类
import java.awt.GridLayout;//导入网格布局管理器类
import java.awt.TextField;//导入文本区域类
import java.awt.event.ActionEvent;//导入事件类
panelcenter.add(buttonsix);
panelcenter.add(buttonseven);
panelcenter.add(buttoneight);
panelcenter.add(buttonnine);
//为10个按钮注册事件监听器
buttonone.addActionListener(h);
panelcenter=new JPanel();//构造面板,放到窗体中央
panelwest=new JPanel();//构造面板,放到窗体西边
paneleast=new JPanel();//构造面板,放到窗体东边
Handle h=new Handle();//新建Handle类对象,Handle类为事件监听类
buttonplus=new JButton("+");
buttonminus=new JButton("-");
buttonmultiple=new JButton("*");
buttonsin,buttontozero;//新建按钮“+”“-”“*”“/”“sin”和归零按钮
JButton buttonone,buttontwo,buttonthree,buttonfour,buttonfive,buttonsix,
buttonseven,buttoneight,buttonnine;//新建数字按钮“0”“1”“2”“3”“4”“5”“6”“7”“8”“9”