java计算器 图形用户界面 升级版v2.0 (限制位数显示+四舍五入的处理)

package com.rgy.entity;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

@SuppressWarnings("serial")
public class Calculator2 extends JFrame{
	// 声明辅助变量
	private String str = "0";// 保存显示框中的字符串
	private String show_str="";// 保存副显示框中的字符串
	private double num_record = 0;// 记录当前数据的值
	private int operator = 0;// 记录当前运算符类型
	private int count_point=0;// 记录小数点个数,避免重复输入小数点
	// 声明并初始化各个组件
	private JTextField text_show = new JTextField("0");
	private JTextField text_show1 = new JTextField();
	private JPanel panel_up = new JPanel();
	private JPanel panel_CEandC = new JPanel();
	private JPanel panel_keys = new JPanel();
	private JButton button_CE = new JButton("CE");
	private JButton button_C = new JButton("C");
	private JButton button_num0 = new JButton("0");
	private JButton button_num1 = new JButton("1");
	private JButton button_num2 = new JButton("2");
	private JButton button_num3 = new JButton("3");
	private JButton button_num4 = new JButton("4");
	private JButton button_num5 = new JButton("5");
	private JButton button_num6 = new JButton("6");
	private JButton button_num7 = new JButton("7");
	private JButton button_num8 = new JButton("8");
	private JButton button_num9 = new JButton("9");
	private JButton button_division = new JButton("/");
	private JButton button_multiplication = new JButton("*");
	private JButton button_cut = new JButton("-");
	private JButton button_add = new JButton("+");
	private JButton button_equal = new JButton("=");
	private JButton button_point = new JButton(".");

	public Calculator2(String s) {
		super(s);
		this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		this.setBounds(500,250,270,400);
		this.setVisible(true);
		//设置显示框
		text_show.setHorizontalAlignment(JTextField.RIGHT);
		text_show.setFont(new Font("Default", 0, 30));
		text_show.setEditable(false);// 不可编辑
		text_show1.setHorizontalAlignment(JTextField.RIGHT);
		text_show1.setFont(new Font("Default", 0, 16));
		text_show1.setEditable(false);
		// 设置布局及边框
		panel_up.setLayout(new BorderLayout());
		button_CE.setBorder(BorderFactory.createRaisedBevelBorder());
		button_C.setBorder(BorderFactory.createRaisedBevelBorder());
		button_CE.setFont(new Font("Default", 1, 20));
		button_C.setFont(new Font("Default", 1, 20));
		panel_up.setBorder(BorderFactory.createEtchedBorder ());
		panel_CEandC.setLayout(new GridLayout(1, 2, 7, 7));
		panel_keys.setLayout(new GridLayout(4, 4, 7, 7));
		// 面板的嵌套
		panel_up.add(text_show1, BorderLayout.NORTH);
		panel_up.add(text_show, BorderLayout.CENTER);
		panel_up.add(panel_CEandC, BorderLayout.SOUTH);
		this.add(panel_up, BorderLayout.NORTH);
		this.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);
		//添加事件(匿名内部类)
		button_num0.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
					if (text_show.getText().equals("0")) {
						text_show.setText("");
					}
					str = text_show.getText() + "0";
					text_show.setText(str);
			}
		});

		button_num1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (operator != 5) {
					if (text_show.getText().equals("0")) {
						text_show.setText("");
					}
					str = text_show.getText() + "1";
					text_show.setText(str);
				} 
				else {
					setClear();
					str = text_show.getText() + "1";
					text_show.setText(str);
				}
			}
		});
		
		button_num2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (operator != 5) {
					if (text_show.getText().equals("0")) {
						text_show.setText("");
					}
					str = text_show.getText() + "2";
					text_show.setText(str);
				} 
				else {
					setClear();
					str = text_show.getText() + "2";
					text_show.setText(str);
				}
			}
		});
		
		button_num3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (operator != 5) {
					if (text_show.getText().equals("0")) {
						text_show.setText("");
					}
					str = text_show.getText() + "3";
					text_show.setText(str);
				} 
				else {
					setClear();
					str = text_show.getText() + "3";
					text_show.setText(str);
				}
			}
		});
		
		button_num4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (operator != 5) {
					if (text_show.getText().equals("0")) {
						text_show.setText("");
					}
					str = text_show.getText() + "4";
					text_show.setText(str);
				} 
				else {
					setClear();
					str = text_show.getText() + "4";
					text_show.setText(str);
				}
			}
		});
		
		button_num5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (operator != 5) {
					if (text_show.getText().equals("0")) {
						text_show.setText("");
					}
					str = text_show.getText() + "5";
					text_show.setText(str);
				} 
				else {
					setClear();
					str = text_show.getText() + "5";
					text_show.setText(str);
				}
			}
		});
		
		button_num6.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (operator != 5) {
					if (text_show.getText().equals("0")) {
						text_show.setText("");
					}
					str = text_show.getText() + "6";
					text_show.setText(str);
				} 
				else {
					setClear();
					str = text_show.getText() + "6";
					text_show.setText(str);
				}
			}
		});
		
		button_num7.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (operator != 5) {
					if (text_show.getText().equals("0")) {
						text_show.setText("");
					}
					str = text_show.getText() + "7";
					text_show.setText(str);
				} 
				else {
					setClear();
					str = text_show.getText() + "7";
					text_show.setText(str);
				}
			}
		});
		
		button_num8.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (operator != 5) {
					if (text_show.getText().equals("0")) {
						text_show.setText("");
					}
					str = text_show.getText() + "8";
					text_show.setText(str);
				} 
				else {
					setClear();
					str = text_show.getText() + "8";
					text_show.setText(str);
				}
			}
		});
		
		button_num9.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (operator != 5) {
					if (text_show.getText().equals("0")) {
						text_show.setText("");
					}
					str = text_show.getText() + "9";
					text_show.setText(str);
				} 
				else {
					setClear();
					str = text_show.getText() + "9";
					text_show.setText(str);
				}
			}
		});
		
		button_point.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(count_point==0){
					str=text_show.getText()+".";
					text_show.setText(str);
					count_point++;
				}
				else{//如果小数点个数超过一个就不再输入小数点
					return;
				}
			}
		});
		
		button_C.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {//全部清空,包括数据和运算符
				setClear();
				text_show.setText("0");
			}
		});
		
		button_CE.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(operator==5){//对于按过等号的数据,则全部清空
					num_record=0;
					str="";
					text_show.setText("0");
				}
				else{//没按过等号的从低到高位依次减少位数
					if(str.length()!=0){//如果显示框中的数据长度不为0
						str=text_show.getText();
						str=str.substring(0,str.length()-1);
						if(str.length()>1){
							text_show.setText(str);
						}
						else{
							str="";
							text_show.setText("0");
						}
					}
				}
			}
		});
		
		button_add.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (str.equals("")) {} 
				else {
					switch (operator) {
					case 0:
						num_record = Double.parseDouble(str);break;
					case 1:
						num_record = num_record + Double.parseDouble(str);break;
					case 2:
						num_record = num_record - Double.parseDouble(str);break;
					case 3:
						num_record = num_record * Double.parseDouble(str);break;
					case 4:
						num_record = num_record / Double.parseDouble(str);break;
					case 5:break;
					}
					show_str=show_str+text_show.getText()+"+";
					text_show1.setText(show_str);
					str="";
					count_point=0;
					text_show.setText("");
					operator=1;
				}
			}
		});
		
		button_cut.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(text_show.getText().equals("0")){
					if(operator==0){//负号的功能
						str="-";
						text_show.setText(str);
					}
					else{
						return;
					}
				}
				else{
					if (str.equals("")) {} 
					else {
						switch (operator) {
						case 0:
							num_record = Double.parseDouble(str);break;
						case 1:
							num_record = num_record + Double.parseDouble(str);break;
						case 2:
							num_record = num_record - Double.parseDouble(str);break;
						case 3:
							num_record = num_record * Double.parseDouble(str);break;
						case 4:
							num_record = num_record / Double.parseDouble(str);break;
						case 5:break;
						}
						show_str=show_str+text_show.getText()+"-";
						text_show1.setText(show_str);
						str="";
						count_point=0;
						text_show.setText("");	
						operator=2;
					}
				}
			}
		});
		
		button_multiplication.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (str.equals("")) {} 
				else {
					switch (operator) {
					case 0:
						num_record = Double.parseDouble(str);break;
					case 1:
						num_record = num_record + Double.parseDouble(str);break;
					case 2:
						num_record = num_record - Double.parseDouble(str);break;
					case 3:
						num_record = num_record * Double.parseDouble(str);break;
					case 4:
						num_record = num_record / Double.parseDouble(str);break;
					case 5:break;
					}
					show_str = show_str + text_show.getText() + "*";
					text_show1.setText(show_str);
					str="";
					count_point=0;
					text_show.setText("");
					operator=3;
				}
			}
		});
		
		button_division.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (str.equals("")) {} 
				else {
					switch (operator) {
					case 0:
						num_record = Double.parseDouble(str);break;
					case 1:
						num_record = num_record + Double.parseDouble(str);break;
					case 2:
						num_record = num_record - Double.parseDouble(str);break;
					case 3:
						num_record = num_record * Double.parseDouble(str);break;
					case 4:
						num_record = num_record / Double.parseDouble(str);break;
					case 5:break;
					}
				}
				show_str = show_str + text_show.getText()+"/";
				text_show1.setText(show_str);
				str="";
				count_point=0;
				text_show.setText("");
				operator=4;
			}
		});
		
		button_equal.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (str.equals("")) {} 
				else {
					switch (operator) {
					case 0:
						num_record = Double.parseDouble(str);break;
					case 1:
						num_record = num_record + Double.parseDouble(str);break;
					case 2:
						num_record = num_record - Double.parseDouble(str);break;
					case 3:
						num_record = num_record * Double.parseDouble(str);break;
					case 4:
						num_record = num_record / Double.parseDouble(str);break;
					case 5:break;
					}
				}
				show_str="";
				count_point=0;
				//输出显示
				text_show1.setText("");
				if(num_record-(int)num_record==0){
					text_show.setText(""+(int)num_record); 
				}
				else{//保留总共15位数字,确保显示框能装下
					if((""+num_record).length()>16){
						String s=(""+num_record).substring(0,16);
						text_show.setText(round(s));//四舍五入处理
					}
					else{
						text_show.setText(""+num_record);
					}
				}
				operator=5;
			}
		});
	}
	
	public String round(String s){//四舍五入
		double k;
		String s1;
		int len;//小数的位数(15位以内)
		k=Double.parseDouble(s);
		s1=""+(int)k;
		len=16-s1.length()-1;
		k=k*Math.pow(10,len-1);
		k=Math.round(k);
		k=k/Math.pow(10,len-1);
		return ""+k;
	}
	
	public void setClear(){//清除
		num_record=0;
		operator=0;
		count_point=0;
		str="";
		text_show.setText("");
		show_str="";
		text_show1.setText("");
	}

	public static void main(String args[]) {
		new Calculator2("计算器v2.0");
	}
}
java计算器  图形用户界面  升级版v2.0  (限制位数显示+四舍五入的处理)

java计算器 图形用户界面 升级版v2.0 (限制位数显示+四舍五入的处理),布布扣,bubuko.com

java计算器 图形用户界面 升级版v2.0 (限制位数显示+四舍五入的处理)

上一篇:学习jQuery的on事件


下一篇:Javascript的一些小知识点