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

Posted ldxsuanfa

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java计算器 图形用户界面 升级版 v1.02相关的知识,希望对你有一定的参考价值。

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="";
					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="";
						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="";
					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="";
				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="";
				//输出显示
				text_show1.setText("");
				if(num_record-(int)num_record==0){
					text_show.setText(""+(int)num_record); 
				}
				else{//保留总共15位数字,确保显示框能装下
					if((""+num_record).length()>15){
						text_show.setText((""+num_record).substring(0,15));
					}
					else{
						text_show.setText(""+num_record);
					}
				}
				operator=5;
			}
		});
	}
	
	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("计算器v1.02");
	}
}
技术图片

以上是关于java计算器 图形用户界面 升级版 v1.02的主要内容,如果未能解决你的问题,请参考以下文章

用Java设计一个图形界面(GUI)的计算器应用程序,完成简单的算术运算

java图形界面编程及计算器

Java课程设计图形界面程序计算器

CS184.1X 计算机图形学导论 第3讲L3V1

魔众问答系统 v1.3.0 系统内核升级,界面显示优化

魔众相册系统 v1.2.0 系统内核升级,界面显示优化