package com.Menu;

	

	import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Stack;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextArea;

import com.service.CalculatorImpl;

	
	/**
	 * 显示主类,由Main类替代
	 * @deprecated
	 * @author Administrator
	 * @version 1.0
	 * */
	public class Menu extends JFrame{
	
		//final static char[] buttons = {'1','2','3','4','5','6','7','8','9','0','.','X','/','+','-','c'}; 
		/**
		 * 
		 */
		private static final long serialVersionUID = -7188167484034129975L;
		
		private JButton add;
		private JButton reduce;
		private JButton multiple;
		private JButton divide;
		private JButton equal;
		private JButton num0;
		private JButton delete;
		private JTextArea display;
		private JButton point;
		private JPanel jp;
		private JButton clear;
		
		
		private JMenu jm;
		private JMenuBar jmb;
		private JMenuItem jmi1;
		private JMenuItem jmi2;
		
		public void init(){
			//Jframe的布局,大小,及事件
			this.setSize(350, 570);
			jp = new JPanel();
			display = new JTextArea("");
			display.setEnabled(false);
			add = new JButton("+");
			reduce = new JButton("-");
			multiple = new JButton("X");
			divide = new JButton("/");
			num0 = new JButton("0");
			for(int i = 1; i<10; i++){
				JButton num = new JButton(""+i+"");
				num.setFont(new Font("宋体",Font.BOLD,24));
				jp.add(num);
				num.addActionListener(new ButtonListener(display, num));
			}
			delete = new JButton("删除");
			equal = new JButton("=");
			point = new JButton(".");
			clear = new JButton("c");
			jmi1 = new JMenuItem("帮助");
			jmi2 = new JMenuItem("打开");
			jmb = new JMenuBar();
			jm = new JMenu("关于");
			jm.add(jmi1);
			jm.add(jmi2);
			jmb.add(jm);
			this.setJMenuBar(jmb);
			jp.setLayout(new GridLayout(3,3,20,20));
			jp.setBounds(10, 170, 210, 210);
			delete.setFont(new Font("宋体",Font.BOLD,24));
			equal.setFont(new Font("宋体",Font.BOLD,24));
			point.setFont(new Font("宋体",Font.BOLD,24));
			clear.setFont(new Font("宋体",Font.BOLD,24));
			display.setFont(new Font("宋体",Font.BOLD,24));
			display.setForeground(Color.BLACK);
			//添加监听器
			clear.addActionListener(new ClearListener());
			num0.addActionListener(new ButtonListener(display,num0));
			add.addActionListener(new CalculatorListener(display,add));
			reduce.addActionListener(new CalculatorListener(display,reduce));
			multiple.addActionListener(new CalculatorListener(display,multiple));
			divide.addActionListener(new CalculatorListener(display,divide));
			point.addActionListener(new CalculatorListener(display,point));
			delete.addActionListener(new DeleteListener());
			equal.addActionListener(new ArithmeticListener());
			//设置个组件的位置大小
			display.setBounds(10, 30, 310, 50); 
			add.setBounds(250,	235, 70, 60);
			reduce.setBounds(250, 170, 70, 60);
			multiple.setBounds(87, 100, 55, 55);
			divide.setBounds(164, 100, 55, 55);
			delete.setBounds(250, 100, 70, 60);
			equal.setBounds(250, 320, 70, 125);
			num0.setBounds(10, 390, 135, 55);
			point.setBounds(164,390, 55, 55);
			clear.setBounds(10, 100, 55, 55);
			
			//将组件添加到JFrame
			
			this.add(add);
			this.add(reduce);
			this.add(multiple);
			this.add(divide);
			this.add(display);
			this.add(delete);
			this.add(equal);
			this.add(num0);
			this.add(point);
			this.add(clear);
			this.add(jp);
			
			this.setLayout(null);//设置布局
			this.setLocationRelativeTo(null);//设置相对位置
			this.setDefaultCloseOperation(EXIT_ON_CLOSE);//关闭操作
			this.setVisible(true);//可视化JFrame
			this.setFont(new Font("宋体",Font.BOLD,20));//设置字体
			this.setResizable(false);//不允许用户改变界面大小
			
		}
		/*监听运算符包括点*/
		private class CalculatorListener implements ActionListener{

			JTextArea display;
			JButton str;
			CalculatorListener(JTextArea display, JButton str){
				
				this.display = display;
				this.str = str;
			}
			public void actionPerformed(ActionEvent e) {
				
				StringBuffer s = new StringBuffer(display.getText());
				if(!(s.length()==0)){
					
					if(!((display.getText().endsWith("+"))||(display.getText().endsWith("-"))||(display.getText().endsWith("X"))||(display.getText().endsWith("/")))){
						//当为无穷和非数字型,清空
						if(display.getText().indexOf("Infinity")!=-1||display.getText().indexOf("NaN")!=-1){
							
							display.setText("0");
						//判断前面的数是不是小数,以决定要不要在它后面加小数点
						}else if(str.getText() == "." && Character.isDigit(s.charAt(s.length()-1))){
							int lastIndex = -1;
							Stack<String> stack = new Stack<String>(); 
							for(int i = 0; i<s.length(); i++){
								lastIndex = new CalculatorImpl().readDouble(display.getText(), i);
								stack.push(display.getText().substring(i,lastIndex));
								i = lastIndex;
							}
							String last = stack.pop();
							
							if(last.indexOf(".") == -1){
								s.append(str.getText());
								display.setText(s.toString());
							}
							//普通情况	
						}else{
							s.append(str.getText());
							display.setText(s.toString());
						}
						//当前面是符号时,自动填零
					}else if(str.getText()=="."  &&(s.charAt(s.length()-1) =='+'||
							s.charAt(s.length()-1) =='-'||s.charAt(s.length()-1) =='X'||s.charAt(s.length()-1) =='/') ){
						s.append("0.");
						display.setText(s.toString());
						
					}
				//为空时,按点填零		
				}else if(s.length() == 0 && str.getText() == "."){
					s.append("0.");
					display.setText(s.toString());
				}
			}
			
		}
		//监听按钮点击事件
		private class ButtonListener implements ActionListener{

			JTextArea display;
			JButton str;
			ButtonListener(JTextArea display, JButton str){
				
				this.display = display;
				this.str = str;
			}
			public void actionPerformed(ActionEvent e) {
				
				StringBuffer s = new StringBuffer(display.getText());
				
				if(display.getText().indexOf("Infinity")!=-1||display.getText().indexOf("NaN")!=-1){
					
					display.setText(str.getText());
				}else{
					display.setText(s+str.getText());
				}	
				
				
				
			}
			
		}
		
		//监听清空事件
		private class ClearListener implements ActionListener{

			
			public void actionPerformed(ActionEvent e) {
				System.out.println(display.getText());
				if(!"".equals(display.getText()))
					display.setText("0");
				
			}
			
		}
		
		//监听删除事件
		private class DeleteListener implements ActionListener{

			public void actionPerformed(ActionEvent e) {
				String s = display.getText();
				if(!"".equals(s)){
					StringBuffer sb =new StringBuffer(display.getText());		
					sb.deleteCharAt(sb.length()-1);
					display.setText(sb.toString());
					
				}
				
			}
			
		}
		/*计算的监听器*/
		private class ArithmeticListener implements ActionListener{

			public void actionPerformed(ActionEvent e) {
				if((display.getText().length()!=0)&&!((display.getText().endsWith("+"))||(display.getText().endsWith("-"))||(display.getText().endsWith("X"))||(display.getText().endsWith("/"))||(display.getText().endsWith(".")))){
					
					double result = new CalculatorImpl().toReversePolishNotation(display.getText());
					String restr = new Double(result).toString();
					display.setText(restr);
					
				}	
			}
		}
		public static void main(String[] args) {
			new Menu().init();
		}
	}
最近下载更多
ja12121  LV2 6月17日
hhr1101  LV2 2022年12月12日
msq1126  LV1 2022年11月18日
Demo1111  LV30 2021年12月12日
Augusthao  LV1 2021年12月5日
123456dfy  LV2 2020年12月31日
VICTORYll  LV15 2020年12月10日
liangge2115  LV26 2020年10月14日
wubinbin  LV11 2020年7月9日
忧麦紫  LV18 2020年6月28日
最近浏览更多
lmml2307831267  LV2 6月24日
ja12121  LV2 6月17日
zgxqna 6月1日
暂无贡献等级
1322123 5月31日
暂无贡献等级
香菇肉饼汤  LV7 5月23日
luo110012  LV9 5月15日
llxxtt  LV2 5月14日
win1991  LV6 3月27日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友