java-计算器


import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;

public class JSQ extends WindowAdapter implements MouseListener{
	Frame fr = new Frame("计算器_2018001483_高梦阳");

	GridBagLayout gbl = new GridBagLayout();
	GridBagConstraints gbc = new GridBagConstraints();

	TextField tf = new TextField("0"); //  建立显示屏

	Button b1 = new Button("1"); //  构建数字键
	Button b2 = new Button("2");
	Button b3 = new Button("3");
	Button b4 = new Button("4");
	Button b5 = new Button("5");
	Button b6 = new Button("6");
	Button b7 = new Button("7");
	Button b8 = new Button("8");
	Button b9 = new Button("9");
	Button b0 = new Button("0");

	Button badd = new Button("+");  //构建符号
	Button bsub = new Button("-");
	Button bmul = new Button("x");
	Button bdiv = new Button("÷");
	Button bequ = new Button("=");
	Button bac = new Button("AC");
	Button bc = new Button("C");
	Button bdot = new Button (".");

	double intNum1 = 0.0;  //设置三个存储空间
	double intNum2 = 0;
	double intTotal = 0;  //得数存储空间

	String strNum1 = "";
	String strNum2 = "";

	String strID = "";

	public void go(){
		fr.setLayout(gbl);  //  设置布局管理器

		tf.setFont(new Font("Times New Roman", Font.PLAIN, 80));  //对显示器设置(定义字体、歪斜程度、字体大小(后续还需要修改))

		gbc.fill = GridBagConstraints.BOTH;

		//第1行 ,显示屏
		gbc.gridwidth = GridBagConstraints.REMAINDER;   //  宽度从头到尾都填充一样的(全大写单词)
		gbc.gridheight = 1;    //  高度一个单元格,向下吃或者向后吃时,1变成2
		gbc.insets = new Insets(0, 0, 0, 0);   //  左上角与单元格左上角紧贴在一起没有缝  左上角对齐
		gbc.weightx = 1;  //  控件与单元格比较大小
		gbc.weighty = 1;   //  同上
		gbc.gridx = 0;    //第1列
		gbc.gridy = 0;    //第1行
		addComponent(tf);  //填充显示器

		//第2行
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		gbc.insets = new Insets(0, 0, 0, 0);
		gbc.weightx = 1;
		gbc.weighty = 1;
		gbc.gridx = 0;    //第1列
		gbc.gridy = 1;    //第2行
		addComponent(b7);
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		gbc.insets = new Insets(0, 0, 0, 0);
		gbc.weightx = 1;
		gbc.weighty = 1;
		gbc.gridx = 1;    //第2列
		gbc.gridy = 1;    //第2行
		addComponent(b8);
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		gbc.insets = new Insets(0, 0, 0, 0);
		gbc.weightx = 1;
		gbc.weighty = 1;
		gbc.gridx = 2;    //第3列
		gbc.gridy = 1;    //第2行
		addComponent(b9);
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		gbc.insets = new Insets(0, 0, 0, 0);
		gbc.weightx = 1;
		gbc.weighty = 1;
		gbc.gridx = 3;    //第4列
		gbc.gridy = 1;    //第2行
		addComponent(badd);
		gbc.gridwidth = GridBagConstraints.REMAINDER;
		gbc.gridheight = 1;
		gbc.insets = new Insets(0, 0, 0, 0);
		gbc.weightx = 1;
		gbc.weighty = 1;
		gbc.gridx = 4;    //第5列
		gbc.gridy = 1;    //第2行
		addComponent(bac);

		//第3行
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		gbc.insets = new Insets(0, 0, 0, 0);
		gbc.weightx = 1;
		gbc.weighty = 1;
		gbc.gridx = 0;    //第1列
		gbc.gridy = 2;    //第3行
		addComponent(b4);
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		gbc.insets = new Insets(0, 0, 0, 0);
		gbc.weightx = 1;
		gbc.weighty = 1;
		gbc.gridx = 1;    //第2列
		gbc.gridy = 2;    //第3行
		addComponent(b5);
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		gbc.insets = new Insets(0, 0, 0, 0);
		gbc.weightx = 1;
		gbc.weighty = 1;
		gbc.gridx = 2;    //第3列
		gbc.gridy = 2;    //第3行
		addComponent(b6);
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		gbc.insets = new Insets(0, 0, 0, 0);
		gbc.weightx = 1;
		gbc.weighty = 1;
		gbc.gridx = 3;    //第4列
		gbc.gridy = 2;    //第3行
		addComponent(bsub);
		gbc.gridwidth = GridBagConstraints.REMAINDER;
		gbc.gridheight = 1;
		gbc.insets = new Insets(0, 0, 0, 0);
		gbc.weightx = 1;
		gbc.weighty = 1;
		gbc.gridx = 4;    //第5列
		gbc.gridy = 2;    //第3行
		addComponent(bc);

		//第4行
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		gbc.insets = new Insets(0, 0, 0, 0);
		gbc.weightx = 1;
		gbc.weighty = 1;
		gbc.gridx = 0;    //第1列
		gbc.gridy = 3;    //第4行
		addComponent(b1);
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		gbc.insets = new Insets(0, 0, 0, 0);
		gbc.weightx = 1;
		gbc.weighty = 1;
		gbc.gridx = 1;    //第2列
		gbc.gridy = 3;    //第4行
		addComponent(b2);
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		gbc.insets = new Insets(0, 0, 0, 0);
		gbc.weightx = 1;
		gbc.weighty = 1;
		gbc.gridx = 2;    //第3列
		gbc.gridy = 3;    //第4行
		addComponent(b3);
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		gbc.insets = new Insets(0, 0, 0, 0);
		gbc.weightx = 1;
		gbc.weighty = 1;
		gbc.gridx = 3;    //第4列
		gbc.gridy = 3;    //第4行
		addComponent(bmul);
		gbc.gridwidth = 1;
		gbc.gridheight = 2;
		gbc.insets = new Insets(0, 0, 0, 0);
		gbc.weightx = 1;
		gbc.weighty = 1;
		gbc.gridx = 4;    //第5列
		gbc.gridy = 3;    //第4行
		addComponent(bequ);

		//第5行
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		gbc.insets = new Insets(0, 0, 0, 0);
		gbc.weightx = 1;
		gbc.weighty = 1;
		gbc.gridx = 0;    //第1列
		gbc.gridy = 4;    //第5行
		addComponent(b0);
		gbc.gridwidth = 2;
		gbc.gridheight = 1;
		gbc.insets = new Insets(0, 0, 0, 0);
		gbc.weightx = 2;
		gbc.weighty = 1;
		gbc.gridx = 1;    //第2列
		gbc.gridy = 4;    //第5行
		addComponent(bdot);
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		gbc.insets = new Insets(0, 0, 0, 0);
		gbc.weightx = 1;
		gbc.weighty = 1;
		gbc.gridx = 3;    //第4列
		gbc.gridy = 4;    //第5行
		addComponent(bdiv);

		fr.addWindowListener(this);
		//设置监听 数字键和运算键
		b1.addMouseListener(this);
		b2.addMouseListener(this);
		b3.addMouseListener(this);
		b4.addMouseListener(this);
		b5.addMouseListener(this);
		b6.addMouseListener(this);
		b7.addMouseListener(this);
		b8.addMouseListener(this);
		b9.addMouseListener(this);
		b0.addMouseListener(this);

		badd.addMouseListener(this);
		bsub.addMouseListener(this);
		bmul.addMouseListener(this);
		bdiv.addMouseListener(this);
		bequ.addMouseListener(this);
		bac.addMouseListener(this);
		bc.addMouseListener(this);
		//小数点点击事件
		bdot.addMouseListener(this);

		fr.setSize(600, 400);
		fr.setVisible(true);
	}

	public void addComponent(Component c){
		gbl.setConstraints(c, gbc);
		fr.add(c);
	}

	public void windowClosing(WindowEvent e){
		fr.dispose();
	}
	// 设置五个鼠标动作
	public void mousePressed(MouseEvent e){}
	public void mouseReleased(MouseEvent e){}
	public void mouseEntered(MouseEvent e){}  //进入
	public void mouseExited(MouseEvent e){}   //退出

	public void mouseClicked(MouseEvent e){
		if(e.getSource() == b1){
			if(strID.equals("")){
				strNum1 += 1;
				strNum1 = strNum1.substring(0,Math.min(strNum1.length(),10));
				tf.setText(strNum1);
			}else{
				strNum2 += 1;
				strNum2 = strNum2.substring(0,Math.min(strNum2.length(),10));
				tf.setText(strNum2);
			}
		}
		if(e.getSource() == b2){
			if(strID.equals("")){
				strNum1 += 2;
				strNum1 = strNum1.substring(0,Math.min(strNum1.length(),10));
				tf.setText(strNum1);
			}else{
				strNum2 += 2;
				strNum2 = strNum2.substring(0,Math.min(strNum2.length(),10));
				tf.setText(strNum2);
			}
		}
		if(e.getSource() == b3){
			if(strID.equals("")){
				strNum1 += 3;
				strNum1 = strNum1.substring(0,Math.min(strNum1.length(),10));
				tf.setText(strNum1);
			}else{
				strNum2 += 3;
				strNum2 = strNum2.substring(0,Math.min(strNum2.length(),10));
				tf.setText(strNum2);
			}
		}
		if(e.getSource() == b4){
			if(strID.equals("")){
				strNum1 += 4;
				strNum1 = strNum1.substring(0,Math.min(strNum1.length(),10));
				tf.setText(strNum1);
			}else{
				strNum2 += 4;
				strNum2 = strNum2.substring(0,Math.min(strNum2.length(),10));
				tf.setText(strNum2);
			}
		}
		if(e.getSource() == b5){
			if(strID.equals("")){
				strNum1 += 5;
				strNum1 = strNum1.substring(0,Math.min(strNum1.length(),10));
				tf.setText(strNum1);
			}else{
				strNum2 += 5;
				strNum2 = strNum2.substring(0,Math.min(strNum2.length(),10));
				tf.setText(strNum2);
			}
		}
		if(e.getSource() == b6){
			if(strID.equals("")){
				strNum1 += 6;
				strNum1 = strNum1.substring(0,Math.min(strNum1.length(),10));
				tf.setText(strNum1);
			}else{
				strNum2 += 6;
				strNum2 = strNum2.substring(0,Math.min(strNum2.length(),10));
				tf.setText(strNum2);
			}
		}
		if(e.getSource() == b7){
			if(strID.equals("")){
				strNum1 += 7;
				strNum1 = strNum1.substring(0,Math.min(strNum1.length(),10));
				tf.setText(strNum1);
			}else{
				strNum2 += 7;
				strNum2 = strNum2.substring(0,Math.min(strNum2.length(),10));
				tf.setText(strNum2);
			}
		}
		if(e.getSource() == b8){
			if(strID.equals("")){
				strNum1 += 8;
				strNum1 = strNum1.substring(0,Math.min(strNum1.length(),10));
				tf.setText(strNum1);
			}else{
				strNum2 += 8;
				strNum2 = strNum2.substring(0,Math.min(strNum2.length(),10));
				tf.setText(strNum2);
			}
		}
		if(e.getSource() == b9){
			if(strID.equals("")){
				strNum1 += 9;
				strNum1 = strNum1.substring(0,Math.min(strNum1.length(),10));
				tf.setText(strNum1);
			}else{
				strNum2 += 9;
				strNum2 = strNum2.substring(0,Math.min(strNum2.length(),10));
				tf.setText(strNum2);
			}
		}
		if(e.getSource() == b0){
			if(strID.equals("")){
				strNum1 += 0;
				strNum1 = strNum1.substring(0,Math.min(strNum1.length(),10));
				tf.setText(strNum1);
			}else{
				strNum2 += 0;
				strNum2 = strNum2.substring(0,Math.min(strNum2.length(),10));
				tf.setText(strNum2);
			}
		}

		if(e.getSource() == badd){
			if(!strID.equals("") && !"".equals(strNum1) && !"".equals(strNum2)){
				Double num1 = Double.parseDouble(strNum1);
				Double num2 = Double.parseDouble(strNum2);
				Double total = 0.0;

				if(strID.equals("+")){
					total = num1 + num2;
				}else if(strID.equals("-")){
					total = num1 - num2;
				}else if(strID.equals("*")){
					total = num1 * num2;
				}else if(strID.equals("/")){
					total = num1 / num2;
				}
				// 处理double失真
				DecimalFormat df = new DecimalFormat("0.000000");
				String temp = df.format(total);
				while(true){
					if( temp.lastIndexOf("0") > temp.lastIndexOf(".")
							&& temp.lastIndexOf(".") != -1
							&& (temp.lastIndexOf("0") == temp.length() -1 || temp.lastIndexOf(".") == temp.length() -1)){
						temp = temp.substring(0,temp.length()-1);
					}else{
						break;
					}
				}
				strNum1 = temp.substring(0, Math.min(temp.length(), 10));
				tf.setText(strNum1);
				strNum2="";
				strID = "";
			}
			strID = "+";
		}
		if(e.getSource() == bsub){
			if(!strID.equals("") && !"".equals(strNum1) && !"".equals(strNum2)){
				Double num1 = Double.parseDouble(strNum1);
				Double num2 = Double.parseDouble(strNum2);
				Double total = 0.0;

				if(strID.equals("+")){
					total = num1 + num2;
				}else if(strID.equals("-")){
					total = num1 - num2;
				}else if(strID.equals("*")){
					total = num1 * num2;
				}else if(strID.equals("/")){
					total = num1 / num2;
				}
				// 处理double失真
				DecimalFormat df = new DecimalFormat("0.000000");
				String temp = df.format(total);
				while(true){
					if( temp.lastIndexOf("0") > temp.lastIndexOf(".")
							&& temp.lastIndexOf(".") != -1
							&& (temp.lastIndexOf("0") == temp.length() -1 || temp.lastIndexOf(".") == temp.length() -1)){
						temp = temp.substring(0,temp.length()-1);
					}else{
						break;
					}
				}
				strNum1 = temp.substring(0, Math.min(temp.length(), 10));

				tf.setText(strNum1);
				strNum2="";
				strID = "";
			}
			strID = "-";
		}
		if(e.getSource() == bmul){
			if(!strID.equals("") && !"".equals(strNum1) && !"".equals(strNum2)){
				Double num1 = Double.parseDouble(strNum1);
				Double num2 = Double.parseDouble(strNum2);
				Double total = 0.0;

				if(strID.equals("+")){
					total = num1 + num2;
				}else if(strID.equals("-")){
					total = num1 - num2;
				}else if(strID.equals("*")){
					total = num1 * num2;
				}else if(strID.equals("/")){
					total = num1 / num2;
				}
				// 处理double失真
				DecimalFormat df = new DecimalFormat("0.000000");
				String temp = df.format(total);
				while(true){
					if( temp.lastIndexOf("0") > temp.lastIndexOf(".")
							&& temp.lastIndexOf(".") != -1
							&& (temp.lastIndexOf("0") == temp.length() -1 || temp.lastIndexOf(".") == temp.length() -1)){
						temp = temp.substring(0,temp.length()-1);
					}else{
						break;
					}
				}
				strNum1 = temp.substring(0, Math.min(temp.length(), 10));

				tf.setText(strNum1);
				strNum2="";
				strID = "";
			}
			strID = "*";
		}
		if(e.getSource() == bdiv){
			if(!strID.equals("") && !"".equals(strNum1) && !"".equals(strNum2)){
				Double num1 = Double.parseDouble(strNum1);
				Double num2 = Double.parseDouble(strNum2);
				Double total = 0.0;

				if(strID.equals("+")){
					total = num1 + num2;
				}else if(strID.equals("-")){
					total = num1 - num2;
				}else if(strID.equals("*")){
					total = num1 * num2;
				}else if(strID.equals("/")){
					total = num1 / num2;
				}
				// 处理double失真
				DecimalFormat df = new DecimalFormat("0.000000");
				String temp = df.format(total);
				while(true){
					if( temp.lastIndexOf("0") > temp.lastIndexOf(".")
							&& temp.lastIndexOf(".") != -1
							&& (temp.lastIndexOf("0") == temp.length() -1 || temp.lastIndexOf(".") == temp.length() -1)){
						temp = temp.substring(0,temp.length()-1);
					}else{
						break;
					}
				}
				strNum1 = temp.substring(0, Math.min(temp.length(), 10));

				tf.setText(strNum1);
				strNum2="";
				strID = "";
			}
			strID = "/";
		}
		//设置等于运算
		if(e.getSource() == bequ && !"".equals(strNum1) && !"".equals(strNum2)){
			Double num1 = Double.parseDouble(strNum1);
			Double num2 = Double.parseDouble(strNum2);
			Double total = 0.0;

			if(strID.equals("+")){
				total = num1 + num2;
			}else if(strID.equals("-")){
				total = num1 - num2;
			}else if(strID.equals("*")){
				total = num1 * num2;
			}else if(strID.equals("/")){
				total = num1 / num2;
			}
			// 处理double失真
			DecimalFormat df = new DecimalFormat("0.000000");
			String temp = df.format(total);
			while(true){
				if( temp.lastIndexOf("0") > temp.lastIndexOf(".")
						&& temp.lastIndexOf(".") != -1
						&& (temp.lastIndexOf("0") == temp.length() -1 || temp.lastIndexOf(".") == temp.length() -1)){
					temp = temp.substring(0,temp.length()-1);
				}else{
					break;
				}
			}
			strNum1 = temp.substring(0, Math.min(temp.length(), 10));

			tf.setText(strNum1);
			strNum2="";
			strID = "";
		}
		if(e.getSource() == bac){
			strNum1 = "";
			strNum2 = "";
			strID = "";
			tf.setText("0");
		}
		if(e.getSource() == bc){
			if(strID.equals("")){
				strNum1 = "";
				tf.setText("0");
			}else{
				strNum2 = "";
				tf.setText("0");
			}
		}
		if(e.getSource() == bdot){
			if(strID.equals("")){
				if(!strNum1.contains(".")){
					if("".equals(strNum1)){
						strNum1 += 0 ;
					}
					strNum1+=".";
					strNum1 = strNum1.substring(0, Math.min(strNum1.length(), 10));
					tf.setText(strNum1);
				}
			}else{
				if(!strNum2.contains(".")){
					if("".equals(strNum2)){
						strNum2 += 0;
					}
					strNum2+=".";
					strNum2 = strNum2.substring(0, Math.min(strNum2.length(), 10));
					tf.setText(strNum2);
				}
			}
		}
	}

	public static void main(String args[]){
		new JSQ().go();
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

啊森的代码园

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值