初学者实现标准计算机


	import java.awt.Color;
	import java.awt.Font;
	import java.awt.GridLayout;
	import java.awt.Menu;
	import java.awt.MenuBar;
	import java.awt.MenuItem;
	import java.awt.MenuShortcut;
	import java.awt.event.ActionEvent;
	import java.awt.event.ActionListener;
	import java.awt.event.KeyEvent;
	import java.util.ArrayList;
	import java.util.regex.Matcher;
	import java.util.regex.Pattern;

	import javax.swing.JButton;
	import javax.swing.JFrame;
	import javax.swing.JOptionPane;
	import javax.swing.JPanel;
	import javax.swing.JRadioButton;
	import javax.swing.JTextArea;
	import javax.swing.JTextField;


	public class Calculator extends JFrame{
		//定义MC MR M+ M-时需要用到的参数 
		public static double n1;
		public static double n2;
		public static double n3;
		public static double n4;
		
		JFrame window;
		
		public Calculator(){
				//窗口及属性
				window=new JFrame("计算机");
				window.setSize(400,575);
				window.setLayout(null);
				window.setResizable(false);
				window.setDefaultCloseOperation(EXIT_ON_CLOSE);
				window.setLocationRelativeTo(null);
				window.setVisible(true);
				
				//创建历史记录框对应文本
				final JFrame f =new JFrame("历史记录");
				f.setSize(220,350);
				f.setResizable(false);
				f.setLocationRelativeTo(null); 
				final JTextArea ta=new JTextArea(14,15);
				ta.setEditable(false);
				f.add(ta);
				
				//计算机显示窗口文本
				final JTextField tf= new JTextField();	   
				tf.setBounds(5,5,380,125);
				tf.setEditable(false);
				tf.setHorizontalAlignment(JTextField.RIGHT);
				tf.setBackground(new Color(192,192,192));
		        tf.setFont(new Font("Cambria",Font.BOLD,20));//设置字体 粗细 大小 
		        window.add(tf);
				   
				//创建除等于号的按钮
				int x=20,y=145;
				int a; 
				String btAll[]={"MC","M+","M-","MR","C","÷","X","←","7","8","9","-",
						"4","5","6","+","1","2","3","","%","0",".",""};
				for(int i=0;i<btAll.length;i++){
					final JButton button=new JButton(btAll[i]);
						button.setBounds(x,y,70,55);
							x+=90;
							a=i+1;
						if(a%4==0){
							y+=60;
							x=20;
							}
					if(x!=20||y!=445&&y!=505){//排除并且不创建等于号那部分按钮
							window.add(button);
							}
					
						//将按钮数据显示在计算机文本框
						class MyListener implements ActionListener {
						        public void actionPerformed(ActionEvent e) {
						            //获取内容	
						            JButton button = (JButton) e.getSource();
						            String logicStr = button.getText();
						            //显示能够显示的字符串,并响应部分按钮
						            if(logicStr=="0"||logicStr=="1"||logicStr=="2"||logicStr=="3"
						            		||logicStr=="4"||logicStr=="5"||logicStr=="6"||logicStr=="7"
						            		||logicStr=="8"||logicStr=="9"||logicStr=="+"||logicStr=="-"
						            		||logicStr=="X"||logicStr=="÷"||logicStr=="."||logicStr=="%"){
						            			tf.setText(tf.getText()+logicStr);
						            }else if(logicStr=="C"){//点击 C 则清除计算机文本框
						            	tf.setText("");
						            	}else if(logicStr=="←") {//点击 ← 删除一个字符
						            		String text = tf.getText();
						            		text=text.substring( 0 ,text.length()-1 );
						            		tf.setText(text);
						            		}else if(logicStr=="M+"){//点击M+记忆正数增加
						            			try { 
						            			String n1_= tf.getText();
						            			 n1=Double.parseDouble(n1_);
						            			 n2=n2+n1;
						            			 }catch(Exception e1){
						    						JOptionPane.showMessageDialog(null, "有错误,请检查输入","错误",
						    								JOptionPane.INFORMATION_MESSAGE);
						    					   }
						            		}else if(logicStr=="M-") {//点击M—记忆负数增加
						            			try { 
						            			String n3_= tf.getText();
						            			n3=Double.parseDouble(n3_);
						            			n3=-n3;
						            			n4=n4+n3;
						            			}catch(Exception e1){
						    						JOptionPane.showMessageDialog(null, "有错误,请检查输入","错误",
						    								JOptionPane.INFORMATION_MESSAGE);
						    					   }
						            		}else if(logicStr=="MR"){//记忆计算
						            			
						            			tf.setText(""+(n4+n2));
						            			
						            		}else if(logicStr=="MC"){//记忆清除
						            			n2=0;
						            			n4=0;
						            		}
						            		
						            		
						            
						        }
						    }
							MyListener ml=new MyListener();
							button.addActionListener(ml);
						
						
					//所有键的字符串的字体和大小
					if(btAll[i]=="1"||btAll[i]=="2"||btAll[i]=="3"||btAll[i]=="4"
							||btAll[i]=="5"||btAll[i]=="6"||btAll[i]=="7"||btAll[i]=="8"
							||btAll[i]=="9"||btAll[i]=="0"){
						//数字
							 button.setFont(new Font("宋体",Font.PLAIN,30));
							 button.setBorderPainted(false);	
						}else {
							//字符
							button.setFont(new Font("美术字体",Font.BOLD+Font.ITALIC,23));
							button.setForeground(Color.blue);
							button.setBorderPainted(false);
						 
						}
					 
					//创建=
				JButton button_eq = new JButton("=");
				button_eq.setBounds(290,385,70,115);
				button_eq.setFont(new Font("美术字体",Font.BOLD+Font.ITALIC,30));
				button_eq.setForeground(Color.blue);
				button_eq.setBorderPainted(false);
				window.add(button_eq);
				button_eq.addActionListener(new ActionListener(){ 
					public void actionPerformed(ActionEvent e) {
					//提取本次需要计算的写在历史记录中并继承以前的历史记录
					ta.setText(ta.getText()+tf.getText()+"=");
					
					String logicStr = tf.getText();
					//取出需要计算的数字并存入数组
					 Pattern n=Pattern.compile("[0-9.%]+");
					 Matcher nall1=n.matcher(logicStr);
					 ArrayList<String>al=new ArrayList<String>();
					 while(nall1.find()) { 
						al.add(nall1.group(0));
						
					 }
					  
					 //存入数组 
						 for(int i=0;i<al.size();i++) {
						 al.get(i).toString();
						
					 }
					//判断+ - * /并存入数组
					 Pattern s=Pattern.compile("[+\\-X÷]");
					 Matcher nall2=s.matcher(logicStr);
					 ArrayList<String>al1=new ArrayList<String>();
					 while(nall2.find()) { 
						al1.add(nall2.group(0));
					 }
			          double over=0,num1=0,num2=0,num3=0;
			        
			          //再进行计算
				for(int i=0;i<al1.size();i++){
					try {//语法检测错误
					    String operational =al1.get(i);//从运算符数组中挨个取出运算符
					    Pattern bj=Pattern.compile("[%]");//查看是否有%
						String num1_=al.get(i);//根据运算符数组下标位置,从数组中取出前一个数字字符串
						Matcher out1=bj.matcher(num1_);
						//查看num1_是否有%
						if(out1.find()) {
							num1_=num1_.substring( 0 ,num1_.length()-1 );
							num3=Double.parseDouble(num1_);
							num3=num3*0.01;
							String doubleStr = String.valueOf(num3);
							num1_=doubleStr;
						}
						String num2_=al.get(i+1);//根据运算符数组下标位置,从数组中取出后一个数字字符串
						Matcher out2=bj.matcher(num2_);
						//查看num2_是否有%
						if(out2.find()) {
							num2_=num2_.substring( 0 ,num2_.length()-1 );
							num3=Double.parseDouble(num2_);
							num3=num3*0.01;
							String doubleStr = String.valueOf(num3);
							num2_=doubleStr;
						}
						
						//如果出现2个及以上+-*/   进行多项运算
						if(i>=1) {
						num1=over;
						num2=Double.parseDouble(num2_);
						}else {
						 num1=Double.parseDouble(num1_);
						 num2=Double.parseDouble(num2_);
						}
					switch (operational) {
						case "+":
							over=num1+num2;break;
						case "-":
							over=num1-num2;break;
						case "X":
							over=num1*num2;break;
						case "÷":
							over=num1/num2;break;
						default:
							break;
							
							}
					//如果出现输入+ - * / 语法错误,则弹窗
						}catch(Exception e1){
							JOptionPane.showMessageDialog(null, "有错误,请检查输入","错误",
									JOptionPane.INFORMATION_MESSAGE);
							break;
						   }
					}
					tf.setText(""+over);//加“”使over由double转换为string
					ta.setText(ta.getText()+tf.getText()+"\n");//继承以前计算的历史记和本次计算的历史记录,并提取写入本次结果到历史记录窗口
				 }
			});
				//创建菜单
				Menu me=new Menu("...");
				MenuItem history=new MenuItem("历史记录");
				MenuItem kx=new MenuItem("科学计算机");
				MenuItem bz=new MenuItem("标准计算机");
				MenuItem tc=new MenuItem("退出",new MenuShortcut(KeyEvent.VK_X));
				MenuBar mb=new MenuBar();
				
				me.add(history);
				me.add(kx);
				me.add(bz);
				me.add(tc);
				mb.add(me);
				
				window.setMenuBar(mb);
				
				history.addActionListener(new ActionListener(){ 
					public void actionPerformed(ActionEvent e) {
						f.setVisible(true);//点击按钮使得历史记录窗口可见
					 }
				});
				
				//科学计算机
				kx.addActionListener(new ActionListener(){ 
					public void actionPerformed(ActionEvent e) {
							//改变窗口和文本框大小
							window.setSize(800,575);
							tf.setBounds(5,5,780,125);
							//创建科学计算机
							  JPanel algpanel=new JPanel();
							  algpanel.setBounds(420,204,360,290);
							  algpanel.setLayout(new GridLayout(5,5,5,5));
							  String [] titles= {"Rad","Inv","㏑","(",")","Int","sinh","sin","x^2",
									  "n!","dms","cosh","cos","x^n","n^√x","π","tanh","tan","x^3",
									  "3^√x","F-E","Exp","Mod","log","10^n"};
							  JButton[] buttons1=new JButton[25];
							  //创建按钮并放入容器
							  for(int i=0;i<25;i++) {
							   buttons1[i]=new JButton(titles[i]);
							   algpanel.add(buttons1[i]);
							  }
							  algpanel.setVisible(true);
							  window.add(algpanel);
							  //创建按钮并放入容器
							  JPanel radiopanel=new JPanel();
							  radiopanel.setLayout(new GridLayout(1,3));
							  JRadioButton angel=new JRadioButton("度");
							  JRadioButton radian=new JRadioButton("弧度");
							  JRadioButton grad=new JRadioButton("梯度");
							  radiopanel.add(angel);
							  radiopanel.add(radian);
							  radiopanel.add(grad);
							  radiopanel.setBounds(450,140,380,50);
							  radiopanel.setVisible(true);
							  window.add(radiopanel);
					 }
				});
				bz.addActionListener(new ActionListener(){ 
					public void actionPerformed(ActionEvent e) {
						window.setSize(400,560);		
						tf.setBounds(5,5,380,125);
					}
				});
				tc.addActionListener(new ActionListener(){ 
					public void actionPerformed(ActionEvent e){ 
						System.exit(0);
					}
				});
		}
	}
		public static void main(String [] args){
			new Calculator();
		}
	}


学习了一个学期,老师让写一个计算机作为期末作业,虽然写的烂但还好能运行

1.可计算百分比

2.可计算小数(会出现Double精度失真的bug, 用Decimal就不会出现上面的问题了,时间不够没改)

3.记忆功能:
MC清除存储的数字 。
MR在显示栏中显示存储的数字。
M+将显示栏中的数字加到存储的数字。
M-存储的数字减去显示栏中的数字。

如: 300*3 + 400*2
先按 300*3= 再按 m+ ....计算机会记忆住 900 的值
再按 400*2= 再按 m+ ....计算机会记忆住 800 的值
最后按MR得到计算结果1700

4.当加减乘除语法输入不对时弹窗报错功能和记忆功能中弹窗报错功能

加入菜单功能:

5.历史查询功能
点击 历史记录 查询历史记录

6.科学计算机以及标准计算机
点击 科学计算机 可展开科学计算机
ps:并未实现科学计算机相应功能
点击 标准计算机 可回到标准计算机

7.从菜单退出功能

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值