简易计算器

import java.awt.*;
import java.awt.event.*;

public class calputer implements ActionListener {
	private String op1 = "0";//运算数备用

	private String operator = "+";//运算符备用

	private boolean append = false;//备用

	StringBuffer sb = new StringBuffer();
	Button A;
	Button A1;
	Button A2;
	Button A3;
	Button A4;
	Button A5;
	Button A6;

	Button A7;
	Button A8;
	Button A9;
	Button B;
	Button B1;
	Button B2;
	Button B3;
	Button B4;
	Button B5;
	TextField a;

	public calputer() {

		Panel p1 = new Panel();

		a = new TextField();

		p1.setLayout(new FlowLayout());

		p1.setLayout(new GridLayout(4, 4, 4, 4));// 使面板选择网格布局管理器以备储存16个按钮(4行4列)

		A = new Button("7");
		A1 = new Button("8");
		A2 = new Button("9");
		A3 = new Button("+");
		A4 = new Button("4");
		A5 = new Button("5");
		A6 = new Button("6");
		A7 = new Button("-");
		A8 = new Button("1");
		A9 = new Button("2");
		B = new Button("3");
		B1 = new Button("*");
		B2 = new Button("0");
		B3 = new Button(".");
		B4 = new Button("=");
		B5 = new Button("/");

		A.addActionListener(this);
		A1.addActionListener(this);
		A2.addActionListener(this);
		A3.addActionListener(this);
		A4.addActionListener(this);
		A5.addActionListener(this);
		A6.addActionListener(this);
		A7.addActionListener(this);
		A8.addActionListener(this);
		A9.addActionListener(this);
		B.addActionListener(this);
		B1.addActionListener(this);
		B2.addActionListener(this);
		B3.addActionListener(this);
		B4.addActionListener(this);

		p1.add(A);
		p1.add(A1);
		p1.add(A2);
		p1.add(A3);
		p1.add(A4);
		p1.add(A5);
		p1.add(A6);
		p1.add(A7);
		p1.add(A8);
		p1.add(A9);
		p1.add(B);
		p1.add(B1);
		p1.add(B2);
		p1.add(B3);
		p1.add(B4);
		p1.add(B5);

		Frame f = new Frame("简易计算器");
		f.setLayout(new BorderLayout());
		f.add("North", a);// 把文本框添加到窗口的北部
		f.add("Center", p1);// 把面板按钮组添加到窗口的中心

		f.addWindowListener(new WindowAdapter() {// 给窗口f添加窗口事件监听器

					public void windowClosing(WindowEvent eve) {// 运行窗口关闭方法

						System.exit(0);// 退出程序

					}

				});

		f.setSize(250, 250);// 设置窗口大小
		f.setVisible(true);// 显示窗口

	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == A) {
			sb.append("7");
			String s = sb.toString();
			a.setText(s);
		}
		if (e.getSource() == A1) {
			sb.append("8");
			String s = sb.toString();
			a.setText(s);
		}
		if (e.getSource() == A2) {
			sb.append("9");
			String s = sb.toString();
			a.setText(s);
		}
		if (e.getSource() == A4) {
			sb.append("4");
			String s = sb.toString();
			a.setText(s);
		}
		if (e.getSource() == A5) {
			sb.append("5");
			String s = sb.toString();
			a.setText(s);
		}
		if (e.getSource() == A6) {
			sb.append("6");
			String s = sb.toString();
			a.setText(s);
		}
		if (e.getSource() == A8) {
			sb.append("1");
			String s = sb.toString();
			a.setText(s);
		}
		if (e.getSource() == A9) {
			sb.append("2");
			String s = sb.toString();
			a.setText(s);
		}
		if (e.getSource() == B) {
			sb.append("3");
			String s = sb.toString();
			a.setText(s);
		}
		if (e.getSource() == B2) {
			sb.append("0");
			String s = sb.toString();
			a.setText(s);
		}
		if (e.getSource() == B3) {
			sb.append(".");
			String s = sb.toString();
			a.setText(s);

		}String comm = e.getActionCommand();//返回与此动作相关的命令字符串,即:使用者第一次点击的按钮是什么。

	    if("0123456789".indexOf(comm)!=-1)//如果相关命令字符串为0~9之间的数字则执行

	    {

	        if(append){

	            String temp = a.getText();//新数字

	            a.setText(temp+comm);

	        }else{                         //因为此时append为false执行这个

	            a.setText(comm); //将文本行t1设置为相关命令字符串(你按中的按钮代码)

	            append =  true;//此时append=true若继续按按钮若继续按数字的话1.第一次的按话不会改变2.非第一次按得话会覆盖之前按得数字(即缺点:只能进行单个数字的计算)

	        }

	    }

	    else if(("+-*/".indexOf(comm)!=-1))//如果相关命令字符串为+-*/之间的数字则执行

	    {

	        //保存

	  

	        op1 = a.getText();//把第一个数赋值给op1 

	        operator = comm;//把命令字符串赋值给operator

	        append = false;//此时append为false即若继续按按钮若按数字的话将重复上面的动作,按符号的话将覆盖之前的符号

	    }

	    else if("=".equals(comm))//如果按的是=号,则按条件进行下面的运算

	    {

	        String op2 = a.getText();//op2第二个数

	        double d1 = Double.parseDouble(op1);

	        double d2 = Double.parseDouble(op2);

	        if(operator.equals("+")){

	            d1 = d1 + d2 ;

	        }else if(operator.equals("-")){

	            d1 = d1 - d2;

	        }else if(operator.equals("*")){

	            d1 = d1 * d2;

	        }else if(operator.equals("/")){

	            d1 = d1 / d2;

	        }

	        a.setText(d1+"");//显示计算结果

	        append = false;

	    }

	    else if(".".equals(comm))//若是.号继续按

	    {

	        String temp = a.getText();

	        if(temp.indexOf(".")==-1){

	            a.setText(temp+".");

	            append = true;

	        }

	    }


	}

	public static void main(String args[]) {
		new calputer();
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值