java小项目--计算器

package calculate;
import java.awt.*;
import java.util.ArrayList;
import java.util.Stack; 
import java.math.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.event.*;
public class calculate extends JFrame {
	/**
	 * 
	 */
	private Stack<Character> priStack = new Stack<Character>();// 操作符栈   
	private Stack<Double> numStack = new Stack<Double>();// 操作数栈       
	private static final long serialVersionUID = 1L;
	JTextField text=new JTextField(10);
	JTextField bin_text = new JTextField(10);//2进制文本框
	JTextField oct_text = new JTextField(10);//8进制文本框
	JTextField hex_text = new JTextField(10);//16进制文本框
	JButton equal1, clear, square;
	JButton log, tan, cos, sin, spot, sqrt, sign;
	StringBuffer hex = new StringBuffer();
	StringBuffer old_hex = new StringBuffer();
	StringBuffer number1 = new StringBuffer();
	public calculate(){
		this.setTitle("Edison的计算器");//创建一个名字为test的窗体
		this.setLayout(new GridLayout(6,1));//设置窗体为6*1的网格布局
		this.setSize(500,350);//设置窗体大小
		this.setLocation(400, 100);//设置窗体位置
		this.setAlwaysOnTop(true);//始终让计算器在最上面
		this.setVisible(true);//设置窗体可见
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置窗体可以关闭
		JPanel change_panel = new JPanel(null); //放置三个进制转换的总面板
		change_panel.setBorder(new LineBorder(Color.black));
		bin_text.setBounds(2, 2, 160, 50);
		change_panel.add(bin_text);
		oct_text.setBounds(165, 2, 160, 50);
		change_panel.add(oct_text);
		hex_text.setBounds(327, 2, 160, 50);
		change_panel.add(hex_text);
		bin_text.setText("二进制:");
		oct_text.setText("八进制:");
		hex_text.setText("十六进制:");
		this.add(change_panel);
		this.setResizable(false);
		
		JPanel one = new JPanel();//进制和清除的容器
		one.setLayout(null);//去掉one容器面板的格式
		one.setBorder(new LineBorder(Color.black));//one面板的边框颜色
		clear = new JButton("C");
		clear.setBounds(413, 6, 70, 40);
		clear.setBackground(Color.black);//改变组件的背景颜色
		clear.setFont(new Font("宋体",Font.BOLD,20));
		clear.setForeground(Color.RED);//改变组件上文字的颜色
		clear.addMouseListener(new MyMouseListener());
		one.add(clear); 
		square = new JButton("x^2");
		square.setBounds(333, 6, 70, 40);
		square.setBackground(Color.black);//改变组件的背景颜色
		square.setFont(new Font("宋体",Font.BOLD,20));
		square.setForeground(Color.RED);//改变组件上文字的颜色
		square.addMouseListener(new MyMouseListener());
		one.add(square);
		text.setBounds(2, 2, 320, 50);
		one.add(text);
		//第一个问题添加到窗体中
		this.add(one);
		
		JPanel two = new JPanel();
		two.setLayout(null);
		log = new JButton("log");	JButton num7 = new JButton("7");		
		num7.setBorder(new LineBorder(Color.PINK));
		JButton num8 = new JButton("8");	JButton num9 = new JButton("9");
		JButton add = new JButton("+");		JButton minus= new JButton("-");
		//为数字和符号添加监听器,获得组件上的内容
		log.addMouseListener(new MyMouseListener());
		num7.addMouseListener(new MyMouseListener());
		num8.addMouseListener(new MyMouseListener());
		num9.addMouseListener(new MyMouseListener());
		add.addMouseListener(new MyMouseListener());
		minus.addMouseListener(new MyMouseListener());
		//组件上字体加粗
		log.setFont(new Font("宋体",Font.BOLD,15));
		num7.setFont(new Font("宋体",Font.BOLD,25));
		num8.setFont(new Font("宋体",Font.BOLD,25));
		num9.setFont(new Font("宋体",Font.BOLD,25));
		add.setFont(new Font("宋体",Font.BOLD,15));
		minus.setFont(new Font("宋体",Font.BOLD,15));
		
		//设置按钮大小及位置
		two.setSize(500,50);
		log.setBounds(11,6,70,40);
		num7.setBounds(94,6,70,40);
		num8.setBounds(173,6,70,40);
		num9.setBounds(252,6,70,40);
		add.setBounds(333,6,70,40);
		minus.setBounds(413,6,70,40);
		
		//改变面板背景设置成粉色,符号组件的设置成黑色,数字组件设置成白色
		two.setBackground(Color.PINK);
		log.setBackground(Color.BLACK);		num7.setBackground(Color.WHITE);
		num8.setBackground(Color.WHITE);	num9.setBackground(Color.WHITE);
		minus.setBackground(Color.BLACK);	add.setBackground(Color.BLACK);
		//将数字组件中文字设置成红色,符号组件上文字的颜色为白色
		num7.setForeground(Color.RED);		num8.setForeground(Color.RED);
		num9.setForeground(Color.RED);
		log.setForeground(Color.WHITE);		add.setForeground(Color.WHITE);
		minus.setForeground(Color.WHITE);
		
		//添加到第二行中
		two.add(log);	two.add(num7);	two.add(num8);
		two.add(num9);	two.add(add);	two.add(minus);
		
		this.add(two);

		
		
		JPanel three = new JPanel();
		three.setLayout(null);
		tan = new JButton("tan");	JButton num4 = new JButton("4");
		JButton num5 = new JButton("5");	JButton num6 = new JButton("6");
		JButton ride = new JButton("*");	JButton division = new JButton("/");
		//为数字和符号添加监听器,获得组件上的内容
		tan.addMouseListener(new MyMouseListener());
		num4.addMouseListener(new MyMouseListener());
		num5.addMouseListener(new MyMouseListener());
		num6.addMouseListener(new MyMouseListener());
		ride.addMouseListener(new MyMouseListener());
		division.addMouseListener(new MyMouseListener());

		//组件上字体加粗
		tan.setFont(new Font("宋体",Font.BOLD,15));
		num4.setFont(new Font("宋体",Font.BOLD,25));
		num5.setFont(new Font("宋体",Font.BOLD,25));
		num6.setFont(new Font("宋体",Font.BOLD,25));
		ride.setFont(new Font("宋体",Font.BOLD,15));
		division.setFont(new Font("宋体",Font.BOLD,15));
		
		//设置按钮大小及位置
		three.setSize(500,50);
		tan.setBounds(11,6,70,40);
		num4.setBounds(94,6,70,40);
		num5.setBounds(173,6,70,40);
		num6.setBounds(252,6,70,40);
		ride.setBounds(333,6,70,40);
		division.setBounds(413,6,70,40);
		
		//改变面板背景设置成粉色,符号组件的设置成黑色,数字组件设置成白色
		three.setBackground(Color.PINK);
		tan.setBackground(Color.BLACK);		num4.setBackground(Color.WHITE);
		num5.setBackground(Color.WHITE);	num6.setBackground(Color.WHITE);
		ride.setBackground(Color.BLACK);	division.setBackground(Color.BLACK);
		//将数字组件中文字设置成红色,符号组件上文字的颜色为白色
		num4.setForeground(Color.RED);		num5.setForeground(Color.RED);
		num6.setForeground(Color.RED);
		tan.setForeground(Color.WHITE);		ride.setForeground(Color.WHITE);
		division.setForeground(Color.WHITE);
		//添加到第三行
		three.add(tan);		three.add(num4);	three.add(num5);
		three.add(num6);	three.add(ride);	three.add(division);
		this.add(three);
		
		
		JPanel four = new JPanel();
		four.setLayout(null);
		cos = new JButton("cos");	JButton num1 = new JButton("1");
		JButton num2 = new JButton("2");	JButton num3 = new JButton("3");
		spot = new JButton(".");	equal1 = new JButton("=");
		//为数字和符号添加监听器,获得组件上的内容
		cos.addMouseListener(new MyMouseListener());
		num1.addMouseListener(new MyMouseListener());
		num2.addMouseListener(new MyMouseListener());
		num3.addMouseListener(new MyMouseListener());
		spot.addMouseListener(new MyMouseListener());
		equal1.addMouseListener(new MyMouseListener());
		
		//设置按钮大小及位置
				four.setSize(500,50);
				cos.setBounds(11,6,70,40);
				num1.setBounds(94,6,70,40);
				num2.setBounds(173,6,70,40);
				num3.setBounds(252,6,70,40);
				spot.setBounds(333,6,70,40);
				equal1.setBounds(413,6,70,40);
				
		//组件上字体加粗
		cos.setFont(new Font("宋体",Font.BOLD,15));
		num1.setFont(new Font("宋体",Font.BOLD,25));
		num2.setFont(new Font("宋体",Font.BOLD,25));
		num3.setFont(new Font("宋体",Font.BOLD,25));
		spot.setFont(new Font("宋体",Font.BOLD,15));
		equal1.setFont(new Font("宋体",Font.BOLD,15));
		//改变面板背景设置成粉色,符号组件的设置成黑色,数字组件设置成白色
		four.setBackground(Color.PINK);
		cos.setBackground(Color.BLACK);		num1.setBackground(Color.WHITE);
		num2.setBackground(Color.WHITE);	num3.setBackground(Color.WHITE);
		spot.setBackground(Color.BLACK);	equal1.setBackground(Color.BLACK);
		//将数字组件中文字设置成红色,符号组件上文字的颜色为白色
		num1.setForeground(Color.RED);		num2.setForeground(Color.RED);
		num3.setForeground(Color.RED);
		cos.setForeground(Color.WHITE);		spot.setForeground(Color.WHITE);
		equal1.setForeground(Color.WHITE);
		//添加到第四行
		four.add(cos);		four.add(num1);		four.add(num2);
		four.add(num3);		four.add(spot);		four.add(equal1);
		this.add(four);
		
		
		JPanel five = new JPanel(null);
		five.setLayout(null);
		sin = new JButton("sin");			JButton left_bracket = new JButton("(");
		JButton num0 = new JButton("0");	JButton right_bracket = new JButton(")");
		sqrt = new JButton("√");			sign = new JButton("%");
		//为数字和符号添加监听器,获得组件上的内容
		sin.addMouseListener(new MyMouseListener());
		left_bracket.addMouseListener(new MyMouseListener());
		num0.addMouseListener(new MyMouseListener());
		right_bracket.addMouseListener(new MyMouseListener());
		sqrt.addMouseListener(new MyMouseListener());
		sign.addMouseListener(new MyMouseListener());
		
		//设置按钮大小及位置
				five.setSize(500,50);
				sin.setBounds(11,6,70,40);
				left_bracket.setBounds(94,6,70,40);
				num0.setBounds(173,6,70,40);
				right_bracket.setBounds(252,6,70,40);
				sqrt.setBounds(333,6,70,40);
				sign.setBounds(413,6,70,40);
				
		//组件上字体加粗
		sin.setFont(new Font("宋体",Font.BOLD,15));
		left_bracket.setFont(new Font("宋体",Font.BOLD,15));
		num0.setFont(new Font("宋体",Font.BOLD,25));
		right_bracket.setFont(new Font("宋体",Font.BOLD,15));
		sqrt.setFont(new Font("宋体",Font.BOLD,15));
		sign.setFont(new Font("宋体",Font.BOLD,15));
		
		//改变面板背景设置成粉色,符号组件的设置成黑色,数字组件设置成白色
		five.setBackground(Color.PINK);
		sin.setBackground(Color.BLACK);		left_bracket.setBackground(Color.BLACK);
		num0.setBackground(Color.WHITE);	right_bracket.setBackground(Color.BLACK);
		sqrt.setBackground(Color.BLACK);	sign.setBackground(Color.BLACK);
		//将数字组件中文字设置成红色,符号组件上文字的颜色为白色
		num0.setForeground(Color.RED);
		sin.setForeground(Color.WHITE);				left_bracket.setForeground(Color.WHITE);
		right_bracket.setForeground(Color.WHITE);	sqrt.setForeground(Color.WHITE);
		sign.setForeground(Color.WHITE);
		//添加到第五行
		five.add(sin);		five.add(left_bracket);		five.add(num0);
		five.add(right_bracket);	five.add(sqrt);		five.add(sign);
		this.add(five);
	}
	public int num_flag = 0;//计算表达式的个数
	class MyMouseListener extends MouseAdapter
	{
		public double operate(String str) {       
	        // 1.判断string当中有没有非法字符       
	        String temp;// 用来临时存放读取的字符       
	        // 2.循环开始解析字符串,当字符串解析完,且符号栈为空时,则计算完成       
	        StringBuffer tempNum = new StringBuffer();// 用来临时存放数字字符串(当为多位数时)       
	        StringBuffer string = new StringBuffer().append(str);// 用来保存,提高效率       
	       
	        while (string.length() != 0) {       
	            temp = string.substring(0, 1);       
	            string.delete(0, 1);       
	            // 判断temp,当temp为操作符时       
	            if (!isNum(temp)) {       
	                // 1.此时的tempNum内即为需要操作的数,取出数,压栈,并且清空tempNum       
	                if (!"".equals(tempNum.toString())) {       
	                    // 当表达式的第一个符号为括号       
	                    double num = Integer.parseInt(tempNum.toString());       
	                    numStack.push(num);   
	                    tempNum.delete(0, tempNum.length());       
	                }       
	                // 用当前取得的运算符与栈顶运算符比较优先级:若高于,则因为会先运算,放入栈顶;若等于,因为出现在后面,所以会后计算,所以栈顶元素出栈,取出操作数运算;       
	                // 若小于,则同理,取出栈顶元素运算,将结果入操作数栈。       
	       
	                // 判断当前运算符与栈顶元素优先级,取出元素,进行计算(因为优先级可能小于栈顶元素,还小于第二个元素等等,需要用循环判断)       
	                while (!compare(temp.charAt(0)) && (!priStack.empty())) {    
	                    double a = (double) numStack.pop();// 第二个运算数       
	                    double b = (double) numStack.pop();// 第一个运算数       
	                    char ope = priStack.pop();       
	                    double result = 0;// 运算结果       
	                    switch (ope) {       
	                    // 如果是加号或者减号,则       
	                    case '+':       
	                        result = b + a;       
	                        // 将操作结果放入操作数栈  
	                        numStack.push(result);       
	                        break;       
	                    case '-':       
	                        result = b - a;       
	                        // 将操作结果放入操作数栈       
	                        numStack.push(result);       
	                        break;       
	                    case '*':       
	                        result = b * a;       
	                        // 将操作结果放入操作数栈       
	                        numStack.push(result);       
	                        break;       
	                    case '/':       
	                        result = b / a;// 将操作结果放入操作数栈       
	                        numStack.push(result);       
	                        break;
	                    case '.':
	                    	int a_flag = 0;                    
	                    	int a_temp = (int)a;
	                    	for(int i=0;i<50;i++){
	                    		if(a_temp/10!=0){
	                    			a_temp = a_temp/10;
	                    			a_flag++;         
	                    		}
	              
	                    	}
	                    	int divisor = 10;
	                    	for(int i=0;i < a_flag;i++)
	                    		divisor = divisor * 10;
	                    	result = b + a / divisor;
	                    	numStack.push(result);
	                    	break;
	                    }       
	       
	                }       
	                // 判断当前运算符与栈顶元素优先级, 如果高,或者低于平,计算完后,将当前操作符号,放入操作符栈       
	               
			if (temp.charAt(0) != '#') {       
	                    priStack.push(new Character(temp.charAt(0)));       
	                    if (temp.charAt(0) == ')') {// 当栈顶为'(',而当前元素为')'时,则是括号内以算完,去掉括号       
	                        priStack.pop();       
	                        priStack.pop();       
	                    }       
	                }

	            } else       
	                // 当为非操作符时(数字)       
	                tempNum = tempNum.append(temp);// 将读到的这一位数接到以读出的数后(当不是个位数的时候)       
	        }       
	        return numStack.pop();       
	    }       
	       
	    /**    
	     * 判断传入的字符是不是0-9的数字    
	     *     
	     * @param str    
	     *            传入的字符串    
	     * @return    
	     */       
	    private boolean isNum(String temp) {       
	        return temp.matches("[0-9]");       
	    }       
	       
	    /**    
	     * 比较当前操作符与栈顶元素操作符优先级,如果比栈顶元素优先级高,则返回true,否则返回false    
	     *     
	     * @param str 需要进行比较的字符    
	     * @return 比较结果 true代表比栈顶元素优先级高,false代表比栈顶元素优先级低    
	     */       
	    private boolean compare(char str) {       
	        if (priStack.empty()) {       
	            // 当为空时,显然 当前优先级最低,返回高       
	            return true;       
	        }       
	        char last = (char) priStack.lastElement();       
	        // 如果栈顶为'('显然,优先级最低,')'不可能为栈顶。       
	        if (last == '(') {       
	            return true;       
	        }       
	        switch (str) {       
	        case '#':       
	            return false;// 结束符       
	        case '(':       
	            // '('优先级最高,显然返回true       
	            return true;       
	        case ')':       
	            // ')'优先级最低,       
	            return false;       
	        case '*': {       
	            // '*/'优先级只比'+-'高       
	            if (last == '+' || last == '-')       
	                return true;       
	            else       
	                return false;       
	        }       
	        case '/': {       
	            if (last == '+' || last == '-')       
	                return true;       
	            else       
	                return false;       
	        }       
	            // '+-'为最低,一直返回false       
	        case '+':       
	            return false;       
	        case '-':       
	            return false;       
	        }       
	        return true;       
	    } 
		public void mouseClicked(MouseEvent e){
			if(e.getSource() == clear)
			{
				text.setText("0"); //将输入的文本域清空
				number1.delete(0, number1.length());
				hex.delete(0, hex.length());
				bin_text.setText("二进制:");
				oct_text.setText("八进制:");
				hex_text.setText("十六进制:");
				num_flag = 0;
			}
			else if(e.getSource() == log){
				if(number1.length()!=0){
					String temp = number1.toString();
					double result = Double.parseDouble(temp);
					int result_int = (int)(Math.log(result)/Math.log(10));
					if((Math.log(result)/Math.log(10)) - result_int != 0){//小数结果
						String string1_result = String.valueOf(Math.log(result)/Math.log(10));
						StringBuffer stringbuffer_result = new StringBuffer(string1_result);
						stringbuffer_result.delete(10, stringbuffer_result.length());
						String string2_result = stringbuffer_result.toString();
						text.setText(string2_result);
						number1.delete(0, number1.length());
						number1.append(string2_result);
						num_flag = string2_result.length();
						
						hex.delete(0, hex.length());
						bin_text.setText("二进制:");
						oct_text.setText("八进制:");
						hex_text.setText("十六进制:");
					}
					else//整数结果
					{
						String string1_result = Math.log(result)/Math.log(10) + "";
						int str = (int)Double.parseDouble(string1_result);
						String string2_result = str + "";
						text.setText(string2_result);
						number1.delete(0, number1.length());
						number1.append(string2_result);
						num_flag = string2_result.length();
						
						hex.delete(0, hex.length());
						hex.append(result_int);
						String hex_ss = hex + "";
						int a = Integer.parseInt(hex_ss);
						bin_text.setText("二进制:"+Integer.toBinaryString(a));//二进制
						oct_text.setText("八进制:"+Integer.toOctalString(a));//八进制
						hex_text.setText("十六进制:"+Integer.toHexString(a));//十六进制
					}
				}
				else{
					text.setText("输入无效");
					text.setHorizontalAlignment(JTextField.RIGHT);
					text.setForeground(Color.RED);
				}
			}
			else if(e.getSource() == tan){
				if(number1.length()!=0){
					String temp = number1.toString();
					double result = Double.parseDouble(temp);
					String string1_result = String.valueOf(Math.tan(result));
					StringBuffer stringbuffer_result = new StringBuffer(string1_result);
					stringbuffer_result.delete(10, stringbuffer_result.length());
					String string2_result = stringbuffer_result.toString();
					text.setText(string2_result);
					number1.delete(0, number1.length());
					number1.append(string2_result);
					num_flag = string2_result.length();
					
					hex.delete(0, hex.length());
					bin_text.setText("二进制:");
					oct_text.setText("八进制:");
					hex_text.setText("十六进制:");
				}
				else{
					text.setText("输入无效");
					text.setHorizontalAlignment(JTextField.RIGHT);
					text.setForeground(Color.RED);
				}
			}
			else if(e.getSource() == sin){
				if(number1.length()!=0){
					String temp = number1.toString();
					double result = Double.parseDouble(temp);
					String string1_result = String.valueOf(Math.sin(result));
					StringBuffer stringbuffer_result = new StringBuffer(string1_result);
					stringbuffer_result.delete(10, stringbuffer_result.length());
					String string2_result = stringbuffer_result.toString();
					text.setText(string2_result);
					number1.delete(0, number1.length());
					number1.append(string2_result);
					num_flag = string2_result.length();
					
					hex.delete(0, hex.length());
					bin_text.setText("二进制:");
					oct_text.setText("八进制:");
					hex_text.setText("十六进制:");
				}
				else{
					text.setText("输入无效");
					text.setHorizontalAlignment(JTextField.RIGHT);
					text.setForeground(Color.RED);
				}
			}
			else if(e.getSource() == cos){
				if(number1.length()!=0){
					String temp = number1.toString();
					double result = Double.parseDouble(temp);
					String string1_result = String.valueOf(Math.cos(result));
					StringBuffer stringbuffer_result = new StringBuffer(string1_result);
					stringbuffer_result.delete(10, stringbuffer_result.length());
					String string2_result = stringbuffer_result.toString();
					text.setText(string2_result);
					number1.delete(0, number1.length());
					number1.append(string2_result);
					num_flag = string2_result.length();
					
					hex.delete(0, hex.length());
					bin_text.setText("二进制:");
					oct_text.setText("八进制:");
					hex_text.setText("十六进制:");
				}
				else{
					text.setText("输入无效");
					text.setHorizontalAlignment(JTextField.RIGHT);
					text.setForeground(Color.RED);
				}
			}
			else if(e.getSource() == sqrt){
				if(number1.length()!=0){	
					String temp = number1.toString();//将数字转换为String类型		
					int result = Integer.parseInt(temp);//将String类型转换为int类型
					String string1_result = Math.sqrt(result)+"";
					StringBuffer stringbuffer_result = new StringBuffer(string1_result);
					int a =  (int)Math.sqrt(result);
					if(Math.sqrt(result) - a != 0){	//小数
						stringbuffer_result.delete(10, stringbuffer_result.length());//取一定的精度
						String string2_result = stringbuffer_result.toString();
						text.setText(string2_result);
						number1.delete(0, number1.length());
						number1.append(string2_result);
						num_flag = string2_result.length();
						
						hex.delete(0, hex.length());
						bin_text.setText("二进制:");
						oct_text.setText("八进制:");
						hex_text.setText("十六进制:");
					}
					else{//整数
						String string2_result = stringbuffer_result.toString();
						text.setText(string2_result);
						number1.delete(0, number1.length());
						number1.append(string2_result);
						num_flag = string2_result.length();
						
						hex.delete(0, hex.length());
						hex.append(a);
						String hex_ss = hex + "";
						int aa = Integer.parseInt(hex_ss);
						bin_text.setText("二进制:"+Integer.toBinaryString(aa));//二进制
						oct_text.setText("八进制:"+Integer.toOctalString(aa));//八进制
						hex_text.setText("十六进制:"+Integer.toHexString(aa));//十六进制
					}
				}
				else{
					text.setText("输入无效");
					text.setHorizontalAlignment(JTextField.RIGHT);
					text.setForeground(Color.RED);
				}
			}
			else if(e.getSource() == sign)//%的监听
			{
				if(number1.length()!=0){
					String temp = number1+"";
					double result = Double.parseDouble(temp);	
					String string_result = result/100 +"";
					text.setText(string_result);
					number1.delete(0, number1.length());
					number1.append(string_result);
					num_flag = string_result.length();
					
					hex.delete(0, hex.length());
					bin_text.setText("二进制:");
					oct_text.setText("八进制:");
					hex_text.setText("十六进制:");
				}
				else{
					text.setText("输入无效");
					text.setHorizontalAlignment(JTextField.RIGHT);
					text.setForeground(Color.RED);
				}
					
			}
			else if(e.getSource() == square)
			{
				if(number1.length()!=0){
					String temp = number1+"";
					double result = Double.parseDouble(temp);
					if(result > 40000){
						text.setText("平方太大,无法计算");
						number1.delete(0, number1.length());
						num_flag = 0;
						
						bin_text.setText("二进制:");//二进制
						oct_text.setText("八进制:");//八进制
						hex_text.setText("十六进制:");//十六进制
					}
					else{
						int result_int = (int)result;
						if(result - result_int != 0){//有小数
							String string_result = result * result +"";
							text.setText(string_result);
							number1.delete(0, number1.length());
							number1.append(string_result);
							num_flag = string_result.length();
							
							bin_text.setText("二进制:");//二进制
							oct_text.setText("八进制:");//八进制
							hex_text.setText("十六进制:");//十六进制
						}
						else
						{
							String string_result = result_int * result_int +"";
							text.setText(string_result);
							number1.delete(0, number1.length());
							number1.append(string_result);
							num_flag = string_result.length();
							
							hex.delete(0, hex.length());
							hex.append(string_result);
							String hex_ss = hex + "";
							int aa = Integer.parseInt(hex_ss);
							bin_text.setText("二进制:"+Integer.toBinaryString(aa));//二进制
							oct_text.setText("八进制:"+Integer.toOctalString(aa));//八进制
							hex_text.setText("十六进制:"+Integer.toHexString(aa));//十六进制
						}
					}
				}
				else{
					text.setText("输入无效");
					text.setHorizontalAlignment(JTextField.RIGHT);
					text.setForeground(Color.RED);
				}
			}
			else if(e.getSource() == equal1)//等号的监听
			{
				if(number1.length() != 0){
					number1.append("#");
					String expression = number1.toString();
					double result = operate(expression);//四则运算的函数
					int result_a = (int)result;
					if(result_a != result){//判断结果是否为整数
						String string_result = String.valueOf(result);
						text.setText(string_result);//在屏幕上显示结果
						number1.delete(0, number1.length());
						num_flag = 0;
					}
					else{
						String string_result = String.valueOf(result_a);
						text.setText(string_result);//在屏幕上显示结果
						number1.delete(0, number1.length());
						num_flag = 0;
					}
						
					
					int result_int = (int)result;
					if(result - result_int == 0 && result_int != 0){
						hex.delete(0, hex.length());//清空进制转换的内容
						String result_s = result_int + "";
						hex.append(result_s);
						String hex_ss = hex + "";
						int a = Integer.parseInt(hex_ss);
						bin_text.setText("二进制:"+Integer.toBinaryString(a));//二进制
						oct_text.setText("八进制:"+Integer.toOctalString(a));//八进制
						hex_text.setText("十六进制:"+Integer.toHexString(a));//十六进制
						hex.delete(0, hex.length());
					}
				}
				else
					text.setText("0");
					
			}
			else 
			{
				String temp = ((JButton)e.getSource()).getText();
				if(temp.equals("0") || temp.equals("1") || temp.equals("2") || temp.equals("3")
					|| temp.equals("4") || temp.equals("5") || temp.equals("6") 
						|| temp.equals("7") || temp.equals("8")	|| temp.equals("9"))
				{
					hex.append(temp);
					String hex_ss = hex + "";
					int a = Integer.parseInt(hex_ss);
					bin_text.setText("二进制:"+Integer.toBinaryString(a));//二进制
					oct_text.setText("八进制:"+Integer.toOctalString(a));//八进制
					hex_text.setText("十六进制:"+Integer.toHexString(a));//十六进制
					old_hex.append(temp);
				}
				else
				{
					hex.delete(0, hex.length());
				}
				number1.insert(num_flag++, temp);
				String zc = number1.toString();
				text.setText("0"); //将输入的文本域清空
				text.setHorizontalAlignment(JTextField.RIGHT);	//设置居右下对齐
				text.setForeground(Color.RED);	//设置显示的字体颜色
				text.setText(zc);	//设置新内容				
			}
			
		}
	}

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

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值