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 (限制位数显示+四舍五入的处理)

时间: 2024-10-10 21:52:07

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

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

jsp页面: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <script type="text/javascript" src="http://code.jquery.com/jquery-1.10.1.m

java编写图形用户界面程序--实例2:母亲节快乐!^_^o~ 努力!

最近时间越来越少了,又要准备省赛,又要备考四级! 作为一个地地道道的"农村娃"."计算机渣",作为一个在外的大学生,没能为母亲在她的节日为送礼物.亲身陪着她,现在只能默默地编个小程序送上祝福, 最近时间很少,学到的东西不多^_^o~ 努力!,每一点的成长对我来说都是有价值的努力! import java.awt.*; import javax.swing.*; import java.awt.event.*; public class 简单实例2 { public

java文本编辑器v2.0 图形用户界面

package 文本编辑器; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.GraphicsEnvironment; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListe

黑马程序员——【Java基础】——GUI(图形用户界面)

一.概述 1.GUI(GraphicalUser Interface):又称图形用户界面,是计算机用户与计算机交互的一种方式. 2.用户与计算机交互的方式有两种:GUI和CLI. (1)GUI(Graphical User Interface):图形用户接口,用图形方式,来显示计算机操作界面,方便直观. (2)CLI(Command LineUser Interface):命令行用户接口,即常见的Dos命令行操作,须记住一些命令,操作不直观. 3.Java提供的GUI对象都存在于java.Awt

【Java】_图形用户界面(GUI)

学习资料来源:斯坦福大学公开课编程方法cs106aJAVA 学习过程中使用的代码.笔记初稿(主要按顺序记录视频教程讲解内容)以及课程讲义已上传至下载中心(有兴趣者可自行下载学习), 学习过程中的大部分程序需要导入acm.jar包(已上传至下载中心,也可自行进入http://jtf.acm.org/ 进行下载), GObject: super()--调用父类构造函数 acm.graphics--添加进画布中的图形是有堆叠顺序的 GCanvas--拼贴画的背景画布 画布(GCanvas)和图形程序(

Java图形用户界面编程

GUI代表图形用户界面(Graphical User Interface),Java API中提供了两套组件用户支持编写图形用户界面,包括AWT和Swing. AWT使用本地操作系统的代码资源,被称为重量级组件. Swing建立在AWT提供的基础之上,同时使用AWT相同的事件处理机制.Swing组件是轻量级的GUI组件,完全有纯Java代码编写,不依赖于任何特定平台,采用MVC架构方式来设计组件类.它将表示层分为三个分离的部分,分别是模型(model).视图(view).控制器(controle

java图形用户界面程序

GUI(Graphical User Interface) 定义:一个程序中,用户可以可以看见和与之交流的部分. 支持图形用户界面的两套组件: 1,AWT 2,Swing 建立步骤: 建立容器-建立组件-将组件添加到容器-设置布局 Swing中的重量级容器: 继承自AWT中的Container类,为其他容器和组件提供绘制位置 有JFrame,JDialog,JWindow,JApplet; Swing中的轻量级容器: 继承自Swing类的JComponent,为了方便其他组件的定位 有JPane

java图形用户界面认知

为了使我们生成的图形用户界面具有良好的平台无关性,Java语言中,提供了布局管理器这个工具来管理 组件在容器中的布局,而不使用直接设置组件位置.组件类是创建用户界面的,例如,JButton,JLabel和JTextField.容器类是用来包含其他组件的,例如,JFrame,JPanel和JApplet.辅助类是用来支持GUI组件的,例如,Graphics,Color,Font,FontMetrics和Dimension. 一.组件类 Component 类的实例可以显示在屏幕上.Componen

基于struts 2.3的家政服务平台V2.0粉色主题-java家政服务平台家政网mysql

基于struts 2.3的家政服务平台V2.0粉色主题-java家政服务平台家政网mysql数据源 1.包含源程序,数据库脚本.2.课题设计仅供参考学习使用,可以在此基础上进行扩展完善.开发环境:Eclipse ,MySQL 5.1,JDK1.7,Tomcat 7涉及技术点:MVC模式.JavaWeb.Struct.JDBC.HTML.CSS.JQUERY.Maven.C3P0.分页.文件上传.购物车等.实现功能:充值.购买歌曲.poi数据导入导出.歌曲上传下载.歌曲播放.用户注册登录注销,管理