首页>代码>Java swing实现计算器程序>/Calculator/src/calculator/Calculator.java
package calculator;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*; 


public class Calculator extends JFrame implements ActionListener {
	StringBuffer str; 									//显示屏所显示的字符串 
	double x,y; 		 								//x和y都是运算数 
	int z,i;											//z表示单击了哪一个运算符
		
	private final String[] b = { "7", "8", "9", "/", "4", "5", "6", "*",
		    "1","2", "3", "-", "0", "+/-", ".", "+" ,"1/x","%","sqrt","="};  
	 
	JButton[] buttons = new JButton[b.length];			//创建20个按钮   

	JButton reset = new JButton("C");					//创建重置键C
	 
	JButton backpace = new JButton("Backpace");			//创建删除键Backpace
	 
	JTextField t1 = new JTextField("0");				//文本显示器
		
	JMenu write = new JMenu("编辑(E)");					//菜单:编辑(E)
	JMenuItem copy = new JMenuItem("复制");				//子菜单:复制
	JMenuItem paste = new JMenuItem("粘贴");				//子菜单:粘贴
	JMenuBar bar = new JMenuBar();						//菜单栏组件
  
	public Calculator() { 
		super("计算器"); 								//窗口名称  
		str=new StringBuffer(); 						//创建一个空字符串缓冲区
		
		JPanel panel1 = new JPanel();					//创建一个panel1容器   	
    	panel1.setLayout(new GridLayout(5,4));    		//在容器中设置布局为GridLayout,并创建一个5*4的区域 
    	for (i = 0; i < b.length; i++) {				//将字符串中的常量赋值给按钮
    		buttons[i] = new JButton(b[i]);
    		panel1.add(buttons[i]);						//将按钮添加到panel1容器中
    	}
    	
    	JPanel panel3 = new JPanel();					//创建一个panel3容器   	
    	panel3.setLayout(new GridLayout(1,2));			//在容器中设置布局为GridLayout,并创建一个1*2的区域 
    	panel3.add(backpace);							//在按钮容器中显示删除键和重置键
    	panel3.add(reset);
    	
    	JPanel panel2 = new JPanel();					//创建一个panel2容器
    	panel2.setLayout(new BorderLayout());			//在panel2的容器中设置的布局为BorderLayout
    	panel2.add("North",panel3);						//在按钮容器中显示panel3和panel1容器
    	panel2.add("South",panel1);
   
    	getContentPane().setLayout(new BorderLayout());	//初始化一个容器,设置布局格式为BorderLayout
    	getContentPane().add("South",panel2);			//该容器包含panel2和菜单栏和显示屏,设置他们的位置
    	getContentPane().add("North",write);
    	getContentPane().add("Center",t1);
    	
    	write.add(copy);								//菜单添加子菜单复制
    	write.add(paste);								//菜单添加子菜单粘贴
    	bar.add(write);									//菜单栏组件添加菜单编辑
    	setJMenuBar(bar);								//将菜单栏组件放到窗口
    	
    	for (i = 0; i < b.length; i++) {				//为panel1中的每个按钮添加监听器
    		buttons[i].addActionListener(this);
    	}
    	reset.addActionListener(this);					//为重置键C添加监听器
    	t1.addActionListener(this);						//为文本显示器添加监听器
    	backpace.addActionListener(this);				//为删除键backpace添加监听器
    	copy.addActionListener(this);					//为复制键copy添加监听器
    	paste.addActionListener(this);					//为粘贴键paste添加监听器
    	
    	
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //设置窗口关闭动作 
		setVisible(true); 								//设置窗口为可见 
		pack();											//设定窗口的大小正好能容纳所有组件
	} 	  

	public void actionPerformed(ActionEvent e) {				//按钮的事件处理				 
		try{ 
			if(e.getSource()==reset) {							//选择"C"清零 
				t1.setText("0"); 								//把文本框清零  
				str.setLength(0);	 							//清空字符串缓冲区以准备接收新的输入运算数 
			} 
			else if(e.getSource()==buttons[13]){				//单击"+/-"取反 
				x=Double.parseDouble(t1.getText().trim());		//trim函数作用是去掉字符串中的空格 
				t1.setText(""+(-x)); 							//把运算数取反
				str.setLength(0);	 							//清空字符串缓冲区以准备接收新的输入运算数
			} 
			else if(e.getSource()==buttons[16]) {				//单击"1/x"求倒数
				x=Double.parseDouble(t1.getText().trim());		//去掉字符串中的空格
				t1.setText(""+(1/x));							//将运算数求倒
				str.setLength(0);	 							//清空字符串缓冲区以准备接收新的输入运算数
			}
			else if (e.getSource()==buttons[15]){				//单击加号按钮获得x的值和z的值并清空y的值  
				x=Double.parseDouble(t1.getText().trim()); 
				str.setLength(0);								//清空字符串缓冲区以准备接收新的输入运算数 
				y=0d; 
				z=0;											//相加 
			} 
			else if(e.getSource()==buttons[11]){				//单击减号按钮获得x的值和z的值并清空y的值 
				x=Double.parseDouble(t1.getText().trim()); 
				str.setLength(0);								//清空字符串缓冲区以准备接收新的输入运算数 
				y=0d; 
				z=1; 											//相减 
			} 
			else if(e.getSource()==buttons[7]){					//单击乘号按钮获得x的值和z的值并清空y的值 
				x=Double.parseDouble(t1.getText().trim()); 
				str.setLength(0);								//清空字符串缓冲区以准备接收新的输入运算数 
				y=0d; 
				z=2;											//相乘 
			} 
			else if(e.getSource()==buttons[3]){					//单击除号按钮获得x的值和z的值并清空y的值  
				x=Double.parseDouble(t1.getText().trim()); 
				str.setLength(0);								//清空字符串缓冲区以准备接收新的输入运算数 
				y=0d; 
				z=3;											//相除  
			}
			else if(e.getSource()==buttons[17]) {				//单击加号按钮获得x的值和z的值并清空y的值
				x=Double.parseDouble(t1.getText().trim()); 
				str.setLength(0);								//清空字符串缓冲区以准备接收新的输入运算数 
				y=0d; 
				z=4;											//取余
			}
			else if(e.getSource()==buttons[19]) {				//单击等号按钮输出计算结果  
				str.setLength(0);								//清空字符串缓冲区以准备接收新的输入运算数 
				switch(z) {
					case 0: 									//z=0相加
						t1.setText(""+(x+y));
						break; 
					case 1:										//z=1相减;
						t1.setText(""+(x-y));
						break; 
					case 2:										//z=2相乘
						t1.setText(""+(x*y));
						break; 
					case 3:										//z=3相除
						t1.setText(""+(x/y));
						break;
					case 4:										//z=4取余
						t1.setText(""+(x%y));
						break;
				} 
			} 
			else if(e.getSource()==buttons[14]) {				//单击"."按钮输入小数  
				if(t1.getText().trim().indexOf('.')!=-1) {		//判断字符串中是否已经包含了小数点 
				} 
				else { 											//如果没有小数点 { 
					if(t1.getText().trim().equals("0")) {		//如果初时显示为0则进行操作 
						t1.setText(str.append(e.getActionCommand()).toString()); 
					} 
					else if(t1.getText().trim().equals("")){	//如果初时显示为空则不做任何操作 
					} 
					else{ 										//如果初时显示为其他数字则进行操作
						t1.setText(str.append(e.getActionCommand()).toString()); 
					} 
				} 
				y=0d; 
			} 
			else if(e.getSource()==buttons[18]) {				 //单击"sqrt"求平方根  
				x=Double.parseDouble(t1.getText().trim()); 
				if(x<0) { 										 //如果小于0则出现异常
					t1.setText("数字格式异常"); 
				} 
				else{ 											 //如果为其他则进行平方根操作
					t1.setText(""+Math.sqrt(x)); 
				} 
				str.setLength(0);								 //清空字符串缓冲区以准备接收新的输入运算数 
				y=0d; 
			}			 
			else if(e.getSource() == copy) {					 //单击"复制"键进行文本显示器选中数值复制
				t1.copy(); 
				t1.requestFocus(); 
			}
			else if(e.getSource() == paste) {					 //单击"粘贴"将复制的内容粘贴至文本显示器
				t1.setText("");
				t1.paste(); 
				t1.requestFocus(); 			
			}			 
		    else{ 
		    	if(e.getSource()==buttons[12]) {				 //如果选择的是"0"这个数字键 { 
		    		if(t1.getText().trim().equals("0")) {		 //如果显示屏显示的为零不做操作  
					}
					else
						t1.setText(str.append(e.getActionCommand()).toString()); 
		    			y=Double.parseDouble(t1.getText().trim()); 
		    	} 
				else if (e.getSource()==backpace) {	 			 //选择的是backpace键 { 
					if(!t1.getText().trim().equals("0")) {		 //如果显示屏显示的不是零 { 
						if(str.length()!=1) { 					 //且不为1个时进行删除操作
							t1.setText(str.delete(str.length()-1,str.length()).toString()); 
						} 
						else{ 
							t1.setText("0"); 					 //文本显示器显示 0 
							str.setLength(0);					 //清空字符串缓冲区以准备接收新的输入运算数 
						} 
					} 
					y=Double.parseDouble(t1.getText().trim()); 
				} 
				else{ 
					t1.setText(str.append(e.getActionCommand()).toString()); 
					y=Double.parseDouble(t1.getText().trim()); 
				} 
			}
		}           
		catch(NumberFormatException e1){ 						 //异常处理
			t1.setText("数字格式异常"); 
		}  
		catch(StringIndexOutOfBoundsException e1){
			t1.setText("字符串索引越界"); 
		}
	}
	 

	 public static void main(String[] args) {					 //主方法实现创建一个窗口
		 new Calculator();
	 } 
} 
最近下载更多
lilong007  LV23 2024年12月23日
鲁一宇  LV5 2024年12月23日
yushibujue321  LV2 2024年9月2日
15883335181  LV1 2024年7月1日
ranagul  LV2 2024年6月28日
鬼屋报道  LV3 2024年6月1日
966942  LV1 2024年4月16日
19397872306  LV2 2024年2月22日
一起加油  LV5 2023年12月21日
000000a  LV1 2023年12月21日
最近浏览更多
sunnylqk 2024年12月31日
暂无贡献等级
1665168649 2024年12月27日
暂无贡献等级
1521661758 2024年12月25日
暂无贡献等级
lilong007  LV23 2024年12月23日
鲁一宇  LV5 2024年12月23日
2379165217a 2024年12月18日
暂无贡献等级
暂无贡献等级
jinglillll 2024年11月27日
暂无贡献等级
ls121383 2024年11月19日
暂无贡献等级
PHARAO486468 2024年11月17日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友