java编写计算器

/*缺点:计算根号时只能计算一个数的,不能再表达式里面计算
                     不能一个输入符号,"-"除外
                     正能处理部分错误输入
      优点:键盘操作更爽啊, 可以用键盘输入部分功能
                      可以进行进制转换
                      文本框不可编辑,并且只接收数字和运算符避免误输入造成的错误
                      增加退格键,在用户输错是不必清空StingBuilder
                      在除法运算中当用户将除数输入成0时,会人性化的弹出错误提示
                      在求平方根时,如果被开方数为负数,会人性化的弹出错误提示
                     像普通计算器一样能在 在文本右边对其*/
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.*;

import java.util.*;
import java.util.concurrent.SynchronousQueue;

public class Cal extends JFrame
{
	JPanel tfPanel,btnPanel;
	JTextField tf;
	JButton btn0,btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9;
	JButton btnAdd,btnSub,btnMul,btnDiv,btnClr,btnEq,btnPoint,
			btnc,btng,btnz,btnb,btno,btnox,btnf;
	Font font,font1;
	
	StringBuilder strbuf;
	String str;
	Scanner sc;
	int calSymbol;
	
	Cal()
	{
		setTitle("计算器(标准)");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLocation(650,300);
	
		font = new Font("微软雅黑",0,30);
		font1 = new Font("微软雅黑",0,50);
		tfPanel = new JPanel();
		tf = new JTextField("", 11);
		tf.setFont(font1);
		tf.setEditable(false);
		tf.setHorizontalAlignment(JTextField.RIGHT);
		tfPanel.add(tf);
		
		add(tfPanel,BorderLayout.NORTH);
	
		btnPanel = new JPanel();
		btn0 = new JButton("0");btn1 = new JButton("1");btn2 = new JButton("2");
		btn3 = new JButton("3");btn4 = new JButton("4");btn5 = new JButton("5");
		btn6 = new JButton("6");btn7 = new JButton("7");btn8 = new JButton("8");
		btn9 = new JButton("9");btnAdd = new JButton("+");btnSub = new JButton("-");
		btnMul = new JButton("*");btnDiv = new JButton("/");btnClr = new JButton("C");
		btnEq = new JButton("=");btnPoint = new JButton(".");btnc=new JButton("←");
		btng=new JButton("√");btnz=new JButton("±");btnb=new JButton("B");
		btno=new JButton("O");btnox=new JButton("OX");btnf=new JButton("1/x");
		
		btnEq.setBackground(Color.orange);
		
		btn0.setFont(font);btn1.setFont(font);btn2.setFont(font);btn3.setFont(font);
		btn4.setFont(font);btn5.setFont(font);btn6.setFont(font);btn7.setFont(font);
		btn8.setFont(font);btn9.setFont(font);btnAdd.setFont(font);btnSub.setFont(font);
		btnMul.setFont(font);btnDiv.setFont(font);btnClr.setFont(font);btnEq.setFont(font);
		btnPoint.setFont(font);btnc.setFont(font);btnz.setFont(font);btnb.setFont(font);
		btno.setFont(font);btnox.setFont(font);btnf.setFont(font);
	
		btnPanel.setLayout(new GridLayout(6,4));
		btnPanel.add(btnb);btnPanel.add(btno);btnPanel.add(btnox);btnPanel.add(btnf);
		btnPanel.add(btnClr);btnPanel.add(btnc);btnPanel.add(btnz);btnPanel.add(btnDiv);
		btnPanel.add(btn7);btnPanel.add(btn8);btnPanel.add(btn9);btnPanel.add(btnMul);
		btnPanel.add(btn4);btnPanel.add(btn5);btnPanel.add(btn6);btnPanel.add(btnSub);
		btnPanel.add(btn1);btnPanel.add(btn2);btnPanel.add(btn3);btnPanel.add(btnAdd);
		btnPanel.add(btng);btnPanel.add(btn0);btnPanel.add(btnPoint);btnPanel.add(btnEq);
		
		add(btnPanel,BorderLayout.CENTER);
		
		strbuf = new StringBuilder();
		
		pack();  //以其组建定义适合大小的窗口
		MyEvent();
	}
	
	private void tool0()
	{
		strbuf.append("0");
		str = strbuf.toString();
		tf.setText(str);
	}
	private void tool1()
	{
		strbuf.append("1");
		str = strbuf.toString();
		tf.setText(str);
	}
	private void tool2()
	{
		strbuf.append("2");
		str = strbuf.toString();
		tf.setText(str);
	}
	private void tool3()
	{
		strbuf.append("3");
		str = strbuf.toString();
		tf.setText(str);
	}
	private void tool4()
	{
		strbuf.append("4");
		str = strbuf.toString();
		tf.setText(str);
	}
	private void tool5()
	{
		strbuf.append("5");
		str = strbuf.toString();
		tf.setText(str);
	}
	private void tool6()
	{
		strbuf.append("6");
		str = strbuf.toString();
		tf.setText(str);
	}
	private void tool7()
	{
		strbuf.append("7");
		str = strbuf.toString();
		tf.setText(str);
	}
	private void tool8()
	{
		strbuf.append("8");
		str = strbuf.toString();
		tf.setText(str);
	}
	private void tool9()
	{
		strbuf.append("9");
		str = strbuf.toString();
		tf.setText(str);
	}
	private void toolpoint()
	{
		strbuf.append(".");
		str = strbuf.toString();
		tf.setText(str);
	}
	private void tooladd()
	{
		strbuf.append("+");
		str = strbuf.toString();
		tf.setText(str);
		calSymbol = 1;
	}
	private void toolsub()
	{
		strbuf.append("-");
		str = strbuf.toString();
		tf.setText(str);
		calSymbol = 2;
	}
	private void toolmul()
	{
		strbuf.append("*");
		str = strbuf.toString();
		tf.setText(str);
		calSymbol = 3;
	}
	private void tooldiv()
	{
		strbuf.append("/");
		str = strbuf.toString();
		tf.setText(str);
		calSymbol = 4;
	}
	private void toolb()
	{
		str=strbuf.toString();
		strbuf.delete(0,strbuf.length());
		strbuf.append(Integer.toBinaryString(Integer.parseInt(str)));
		str=strbuf.toString();
		tf.setText(str);
	}
	private void toolo()
	{
		str=strbuf.toString();
		strbuf.delete(0,strbuf.length());
		strbuf.append(Integer.toOctalString(Integer.parseInt(str)));
		str=strbuf.toString();
		tf.setText(str);
	}
	private void toolox()
	{
		str=strbuf.toString();
		strbuf.delete(0,strbuf.length());
		strbuf.append(Integer.toHexString(Integer.parseInt(str)));
		str=strbuf.toString();
		tf.setText(str);
	}
	private void toolz()
	{
		str=strbuf.toString();
		if(str.charAt(0) == '-')
		{
			strbuf.deleteCharAt(0);
			str=strbuf.toString();
		}
		else 
		{
			str="-"+strbuf.toString();
			strbuf.delete(0,strbuf.length());
			strbuf.append(str);
		}
		tf.setText(str);
	}
	private void toolclear()
	{
		strbuf.delete(0,strbuf.length());
		tf.setText("");
	}
	private void toolg()
	{
		str = strbuf.toString();
		strbuf.delete(0,strbuf.length());
		if(Double.parseDouble(str)<=0)
		{
			JOptionPane.showMessageDialog(null,"被开方数不能为负数!","标题",JOptionPane.ERROR_MESSAGE);
			strbuf.delete(0,strbuf.length());
			tf.setText("");
		}
		else
		{
			strbuf.append(Math.sqrt(Double.parseDouble(str)));
			str = strbuf.toString();
			tf.setText(str);
		}
	}
	private void toolf()
	{
		str = strbuf.toString();
		strbuf.delete(0,strbuf.length());
		if(Double.parseDouble(str)==0)
		{
			JOptionPane.showMessageDialog(null,"除数不能为0!","标题",JOptionPane.ERROR_MESSAGE);
			strbuf.delete(0,strbuf.length());
			tf.setText("");
		}
		else
		{
			strbuf.append(1/Double.parseDouble(str));
			str = strbuf.toString();
			tf.setText(str);
		}
	}
	private void toolentert()
	{
		str = strbuf.toString();
		if(str.isEmpty())
		{
			JOptionPane.showMessageDialog(null,"请合法输入!");
			strbuf.append("0+0");
			str=strbuf.toString();
		}
		char[] arr=str.toCharArray();
		int i;
		for(i=0;(arr[i]<'0'||arr[i]>'9')&&arr[i]!='-';i++);
		str=str.substring(i,str.length());
		sc = new Scanner(str);
		sc.useDelimiter("[+-]|[*]|[/]");
		
		double d1,d2,result=0;
		d1 = sc.nextDouble();
		d2 = sc.nextDouble();
		
		if(str.charAt(0) == '-')
			d1 = -d1;
		
		if(calSymbol == 1)
			result = d1+d2;
		else if(calSymbol == 2)
			result = d1-d2;
		else if(calSymbol == 3)
			result = d1*d2;
		else if(calSymbol == 4)
		{
			if(d2==0)
			{
				JOptionPane.showMessageDialog(null,"除数不能为0!","标题",JOptionPane.ERROR_MESSAGE);
				strbuf.delete(0,strbuf.length());
				tf.setText("");
			}
			else
				result = d1/d2;
		}
			
		strbuf.delete(0,strbuf.length());
		tf.setText(""+result);
		strbuf.append(result);
	}
	private void toolback()
	{
		strbuf.delete(strbuf.length()-1,strbuf.length());
		str=strbuf.toString();
		tf.setText(str);
	}
	private void MyEvent()
	{
		btn0.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				tool0();
		    }
		});
		btn1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				tool1();
		    }
		});
		btn2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				tool2();
		    }
		});
		btn3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				tool3();
		    }
		});
		btn4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				tool4();
		    }
		});
		btn5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				tool5();
		    }
		});
		btn6.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				tool6();
		    }
		});
		btn7.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				tool7();
		    }
		});
		btn8.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				tool8();
		    }
		});
		btn9.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				tool9();
		    }
		});
		btnAdd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				tooladd();
		    }
		});
		btnSub.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				toolsub();
		    }
		});
		btnMul.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				toolmul();
		    }
		});
		btnDiv.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				tooldiv();
		    }
		});
		btng.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				toolg();
		    }
		});
		btnf.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				toolf();
		    }
		});
		btnb.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				toolb();
		    }
		});
		btno.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				toolo();
		    }
		});
		btnox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				toolox();
		    }
		});
		btnz.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				toolz();
		    }
		});
		btnPoint.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				toolpoint();
		    }
		});
		btnClr.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				toolclear();
		    }
		});
		btnc.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				toolback();
		    }
		});
		btnEq.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				toolentert();
		    }
		});
		
		tf.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e)
			{
				if(e.getKeyCode()==KeyEvent.VK_0)
					tool0();
				else if(e.getKeyCode()==KeyEvent.VK_1)
					tool1();
				else if(e.getKeyCode()==KeyEvent.VK_2)
					tool2();
				else if(e.getKeyCode()==KeyEvent.VK_3)
					tool3();
				else if(e.getKeyCode()==KeyEvent.VK_4)
					tool4();
				else if(e.getKeyCode()==KeyEvent.VK_5)
					tool5();
				else if(e.getKeyCode()==KeyEvent.VK_6)
					tool6();
				else if(e.getKeyCode()==KeyEvent.VK_7)
					tool7();
				else if(e.getKeyCode()==KeyEvent.VK_8)
					tool8();
				else if(e.getKeyCode()==KeyEvent.VK_9)
					tool9();
				else if(e.getKeyCode()==KeyEvent.VK_PERIOD)
					toolpoint();
				else if(e.getKeyCode()==KeyEvent.VK_BACK_SPACE)
					toolback();
				else if(e.getKeyCode()==KeyEvent.VK_ENTER)
					toolentert();
				else if(e.getKeyCode()==KeyEvent.VK_ADD)
					tooladd();
				else if(e.getKeyCode()==KeyEvent.VK_SUBTRACT)
					toolsub();
				else if(e.getKeyCode()==KeyEvent.VK_MULTIPLY)
					toolmul();
				else if(e.getKeyCode()==KeyEvent.VK_DIVIDE)
					tooldiv();
				else if(e.getKeyCode()==KeyEvent.VK_B)
					toolb();
				else if(e.getKeyCode()==KeyEvent.VK_O)
					toolo();
				else if(e.getKeyCode()==KeyEvent.VK_X)
					toolox();
				else if(e.getKeyCode()==KeyEvent.VK_C)
					toolclear();
				else if(e.getKeyCode()==KeyEvent.VK_G)
					toolg();
				else if(e.getKeyCode()==KeyEvent.VK_F)
					toolf();
				else if(e.getKeyCode()==KeyEvent.VK_Z)
					toolz();
				else if(e.getKeyCode()==KeyEvent.VK_ESCAPE)
					System.exit(0);
			}
		});
	}
	
	public static void main(String args[])
	{
		Cal frame = new Cal();
		frame.setVisible(true);
	}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值