java计算器 图形用户界面 升级版

package com.rgy.entity;


import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JTextField;


public class Calculator {
	//声明辅助变量
	private String str="0";//保存显示框中的字符串
	private double num_record=0;//记录当前数据的值
	private int count_cut=0;//记录减号的个数
	private int count_multiplication=0;//记录乘号的个数
	private int count_division=0;//记录除号的个数
	private int operator=0;// 运算符
	//声明并初始化各个组件
	private JTextField text_show=new JTextField("0");
	private Panel panel_up=new Panel();
	private Panel panel_CEandC=new Panel();
	private Panel panel_keys=new Panel();
	private Button button_CE=new Button("CE");
	private Button button_C=new Button("C");
	private Button button_num0=new Button("0");
	private Button button_num1=new Button("1");
	private Button button_num2=new Button("2");
	private Button button_num3=new Button("3");
	private Button button_num4=new Button("4");
	private Button button_num5=new Button("5");
	private Button button_num6=new Button("6");
	private Button button_num7=new Button("7");
	private Button button_num8=new Button("8");
	private Button button_num9=new Button("9");
	private Button button_division=new Button("/");
	private Button button_multiplication=new Button("*");
	private Button button_cut=new Button("-");
	private Button button_add=new Button("+");
	private Button button_equal=new Button("=");
	private Button button_point=new Button(".");
	public void init(){
		Frame f=new Frame("计算器v1.01");
		f.setVisible(true);
		f.setBounds(500,250,270,400);
		f.setBackground(Color.lightGray);
		//text_show属性设置
		text_show.setHorizontalAlignment(JTextField.RIGHT);
		text_show.setBackground(Color.lightGray);
		text_show.setFont(new Font("楷体", 1, 30));
		text_show.setEditable(false);//不可编辑
		//设置按钮的颜色
		button_C.setForeground(Color.red);
		button_CE.setBackground(new Color(71,164,243));
		button_C.setBackground(new Color(71,164,243));
		button_num0.setBackground(new Color(151,234,63));
		button_num1.setBackground(new Color(151,234,63));
		button_num2.setBackground(new Color(151,234,63));
		button_num3.setBackground(new Color(151,234,63));
		button_num4.setBackground(new Color(151,234,63));
		button_num5.setBackground(new Color(151,234,63));
		button_num6.setBackground(new Color(151,234,63));
		button_num7.setBackground(new Color(151,234,63));
		button_num8.setBackground(new Color(151,234,63));
		button_num9.setBackground(new Color(151,234,63));
		button_division.setBackground(new Color(151,234,63));
		button_multiplication.setBackground(new Color(151,234,63));
		button_cut.setBackground(new Color(151,234,63));
		button_add.setBackground(new Color(151,234,63));
		button_equal.setBackground(new Color(151,234,63));
		button_point.setBackground(new Color(151,234,63));
		//设置按钮的字体
		button_CE.setFont(new Font("楷体", 1, 20));
		button_C.setFont(new Font("楷体", 1, 20));
		button_num0.setFont(new Font("楷体", 1, 20));
		button_num1.setFont(new Font("楷体", 1, 20));
		button_num2.setFont(new Font("楷体", 1, 20));
		button_num3.setFont(new Font("楷体", 1, 20));
		button_num4.setFont(new Font("楷体", 1, 20));
		button_num5.setFont(new Font("楷体", 1, 20));
		button_num6.setFont(new Font("楷体", 1, 20));
		button_num7.setFont(new Font("楷体", 1, 20));
		button_num8.setFont(new Font("楷体", 1, 20));
		button_num9.setFont(new Font("楷体", 1, 20));
		button_division.setFont(new Font("楷体", 1, 20));
		button_multiplication.setFont(new Font("楷体", 1, 20));
		button_cut.setFont(new Font("楷体", 1, 20));
		button_add.setFont(new Font("楷体", 1, 20));
		button_equal.setFont(new Font("楷体", 1, 20));
		button_point.setFont(new Font("楷体", 1, 20));
		//设置三个面板的布局
		panel_up.setLayout(new BorderLayout());
		panel_CEandC.setLayout(new GridLayout(1,2,7,7));
		panel_keys.setLayout(new GridLayout(4,4,7,7));
		//面板的嵌套
		panel_up.add(text_show,BorderLayout.NORTH);
		panel_up.add(panel_CEandC,BorderLayout.CENTER);
		f.add(panel_up,BorderLayout.NORTH);
		f.add(panel_keys,BorderLayout.CENTER);
		//在面板上添加按钮
		panel_CEandC.add(button_CE);panel_CEandC.add(button_C);
		panel_keys.add(button_num7);panel_keys.add(button_num8);
		panel_keys.add(button_num9);panel_keys.add(button_division);
		panel_keys.add(button_num4);panel_keys.add(button_num5);
		panel_keys.add(button_num6);panel_keys.add(button_multiplication);
		panel_keys.add(button_num1);panel_keys.add(button_num2);
		panel_keys.add(button_num3);panel_keys.add(button_cut);
		panel_keys.add(button_num0);panel_keys.add(button_point);
		panel_keys.add(button_equal);panel_keys.add(button_add);
		//窗口事件监听
		f.addWindowListener(new MyWindowListen());
		//按钮事件监听
		button_num0.addActionListener(new Button_num0Handler());
		button_num1.addActionListener(new Button_num1Handler());
		button_num2.addActionListener(new Button_num2Handler());
		button_num3.addActionListener(new Button_num3Handler());
		button_num4.addActionListener(new Button_num4Handler());
		button_num5.addActionListener(new Button_num5Handler());
		button_num6.addActionListener(new Button_num6Handler());
		button_num7.addActionListener(new Button_num7Handler());
		button_num8.addActionListener(new Button_num8Handler());
		button_num9.addActionListener(new Button_num9Handler());
		button_point.addActionListener(new Button_pointHandler());
		button_C.addActionListener(new Button_CHandler());
		button_CE.addActionListener(new Button_CEHandler());
		button_add.addActionListener(new Button_addHandler());
		button_cut.addActionListener(new Button_cutHandler());
		button_multiplication.addActionListener(new Button_multiplicationHandler());
		button_division.addActionListener(new Button_divisionHandler());
		button_equal.addActionListener(new Button_equalHandler());
	}
	//用内部类处理按钮的点击事件
	class Button_num0Handler implements ActionListener{
		public void actionPerformed(ActionEvent e) {
			if(str=="0"){
				text_show.setText("");
			}
			str=text_show.getText()+"0";
			text_show.setText(str);
		}
	}
	
	class Button_num1Handler implements ActionListener{
		public void actionPerformed(ActionEvent e) {
			if(str=="0"){
				text_show.setText("");
			}
			str=text_show.getText()+"1";
			text_show.setText(str);
		}
	}
	
	class Button_num2Handler implements ActionListener{
		public void actionPerformed(ActionEvent e) {
			if(str=="0"){
				text_show.setText("");
			}
			str=text_show.getText()+"2";
			text_show.setText(str);
		}
	}
	
	class Button_num3Handler implements ActionListener{
		public void actionPerformed(ActionEvent e) {
			if(str=="0"){
				text_show.setText("");
			}
			str=text_show.getText()+"3";
			text_show.setText(str);
		}
	}
	
	class Button_num4Handler implements ActionListener{
		public void actionPerformed(ActionEvent e) {
			if(str=="0"){
				text_show.setText("");
			}
			str=text_show.getText()+"4";
			text_show.setText(str);
		}
	}
	
	class Button_num5Handler implements ActionListener{
		public void actionPerformed(ActionEvent e) {
			if(str=="0"){
				text_show.setText("");
			}
			str=text_show.getText()+"5";
			text_show.setText(str);
		}
	}
	
	class Button_num6Handler implements ActionListener{
		public void actionPerformed(ActionEvent e) {
			if(str=="0"){
				text_show.setText("");
			}
			str=text_show.getText()+"6";
			text_show.setText(str);
		}
	}
	
	class Button_num7Handler implements ActionListener{
		public void actionPerformed(ActionEvent e) {
			if(str=="0"){
				text_show.setText("");
			}
			str=text_show.getText()+"7";
			text_show.setText(str);
		}
	}
	
	class Button_num8Handler implements ActionListener{
		public void actionPerformed(ActionEvent e) {
			if(str=="0"){
				text_show.setText("");
			}
			str=text_show.getText()+"8";
			text_show.setText(str);
		}
	}
	
	class Button_num9Handler implements ActionListener{
		public void actionPerformed(ActionEvent e) {
			if(str=="0"){
				text_show.setText("");
			}
			str=text_show.getText()+"9";
			text_show.setText(str);
		}
	}
	
	class Button_pointHandler implements ActionListener{
		public void actionPerformed(ActionEvent e) {
			str=text_show.getText()+".";
			text_show.setText(str);
		}
	}
	
	class Button_CHandler implements ActionListener{
		public void actionPerformed(ActionEvent e) {//全部数据初始化
				num_record=0;
				operator=0;
				count_cut=0;
				count_multiplication=0;
				count_division=0;
				str="";
				text_show.setText(str);
		}
	}
	
	class Button_CEHandler implements ActionListener{
		public void actionPerformed(ActionEvent e) {
			if(operator==5){
				num_record=0;
				str="";
				text_show.setText(str);
			}
			else{
				if(str.length()!=0){
					str=text_show.getText();
					str=str.substring(0,str.length()-1);
					text_show.setText(str);
				}
				else{
					return;
				}
			}
		}
	}
	
	class Button_addHandler implements ActionListener{
		public void actionPerformed(ActionEvent e) {
			if(operator==5){}
			else{
				if(check(str)==true){
					num_record=num_record+Double.parseDouble(str);
				}
				else{
					return;
				}
			}
			str="";
			text_show.setText(str);
			operator=1;
		}
	}
	
	class Button_cutHandler implements ActionListener{
		public void actionPerformed(ActionEvent e) {
			if(operator==0){//负号的功能
				if(str.equals("")||str.equals("0")){
					str="-";
					text_show.setText(str);
				}
			}
			else{
				if(operator==5){}
				else{
					if(count_cut==0){
						if(check(str)==true){
							num_record=Double.parseDouble(str);
							count_cut++;
						}
						else{
							return;
						}
					}
					else{
						if(check(str)==true){
							num_record=num_record-Double.parseDouble(str);
						}
						else{
							return;
						}
					}
				}
				str="";
				text_show.setText(str);	
			}
			operator=2;
		}
	}
	
	class Button_multiplicationHandler implements ActionListener{
		public void actionPerformed(ActionEvent e) {
			if(operator==5){}
			else{
				if(count_multiplication==0){
					if(check(str)==true){
						num_record=Double.parseDouble(str);
						count_multiplication++;
					}
					else{
						return;
					}
				}
				else{
					if(check(str)==true){
						num_record=num_record*Double.parseDouble(str);
					}
					else{
						return;
					}
				}
			}
			str="";
			text_show.setText(str);
			operator=3;
		}
	}
	
	class Button_divisionHandler implements ActionListener{
		public void actionPerformed(ActionEvent e) {
			if(operator==5){}
			else{
				if(count_division==0){
					if(check(str)==true){
						num_record=Double.parseDouble(str);
						count_division++;
					}
					else{
						return;
					}
				}
				else{
					if(check(str)==true){
						num_record=num_record/Double.parseDouble(str);
					}
					else{
						return;
					}
				}
			}
			str="";
			text_show.setText(str);
			operator=4;
		}
	}
	
	class Button_equalHandler implements ActionListener{
		public void actionPerformed(ActionEvent e) {
			if(operator==0){
				return;
			}
			if(operator==1){
				if(check(str)==true){
					num_record=num_record+Double.parseDouble(str);
				}
			}
			if(operator==2){
				if(check(str)==true){
					num_record=num_record-Double.parseDouble(str);
				}
			}
			if(operator==3){
				if(check(str)==true){
					num_record=num_record*Double.parseDouble(str);
				}
			}
			if(operator==4){
				if(check(str)==true){
					num_record=num_record/Double.parseDouble(str);
				}
			}
			
			if(num_record-(int)num_record==0){
				text_show.setText(""+(int)num_record);
			}
			else{
				text_show.setText(""+num_record);
			}
			operator=5;
		}
	}
	//判断字符串是否能够转化为double型
	public boolean check(String s){
		boolean flag=true;
        try {
            Double.parseDouble(s);
        } catch (Exception e) {
            flag=false;
        }
        
        if (flag) {
            return true;
        } 
        else {
            return false;
        }
    }
	
	//关闭(运用了多态)
	class MyWindowListen extends WindowAdapter{
		public void windowClosing(WindowEvent e) {
			System.exit(0);
		}
	}
	//主方法
	public static void main(String args[]){
		new Calculator().init();
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值