package com.Menu;

	import java.awt.EventQueue;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.SwingConstants;

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

import javax.swing.JMenu;
import javax.swing.JMenuItem;

import com.service.CalculatorImpl;

import javax.swing.UIManager;
import javax.swing.JMenuBar;

import java.awt.SystemColor;
import java.awt.Color;
import java.awt.Toolkit;

import javax.swing.JLabel;
import javax.swing.ImageIcon;

	/**
	 *  有JFrame工具创建的swing界面
	 *  
	 * */
	public class Main {
	
		private JFrame frame;
		private JTextField display;
		private JPanel panel;
		private JButton clear;
		private JButton divide;
		private JButton multiple;
		private JButton delete;
		private JButton btnMc;
		private JButton num7;
		private JButton num8;
		private JButton num9;
		private JButton reduce;
		private JButton btnM;
		private JButton num1;
		private JButton num2;
		private JButton num3;
		private JButton equal;
		private JButton point;
		private JButton num0;
		private JButton num4;
		private JButton num5;
		private JButton num6;
		private JButton add;
		private JButton btnM_2;
		private JButton btnMr;
		private JMenuBar menuBar;
		private JMenu mnNewMenu;
		private JMenuItem mntmNewMenuItem;
		private JMenu mnNewMenu_1;
		private JMenuItem menuItem;
		private JMenuItem menuItem_1;
		private JTextField operate;
		private boolean flag = false;
	
		/**
		 * Launch the application.
		 * 
		 * 
		 */
		public static void main(String[] args) {
			EventQueue.invokeLater(new Runnable() {
				public void run() {
					try {
						Main window = new Main();
						window.frame.setVisible(true);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			});
		}
	
		/**
		 * Create the application.
		 */
		public Main() {
			initialize();
		}
	
		/**
		 * 初始化框架及组件,并添加监听事件
		 */
		private void initialize() {
			frame = new JFrame();
			frame.setIconImage(Toolkit.getDefaultToolkit().getImage("E:\\myeclipse\\workspace\\mychat\\calculator\\img\\ms.ico"));
			
			frame.getContentPane().setBackground(UIManager.getColor("Button.darkShadow"));
			frame.setTitle("\u8BA1\u7B97\u5668");
			frame.setBounds(100, 100, 794, 1000);
			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			frame.getContentPane().setLayout(null);
			
			display = new JTextField();
			display.setText("0");
			display.setAlignmentX(800);
			display.setFont(new Font("宋体", Font.PLAIN, 24));
			display.setEditable(false);
			display.setBounds(15, 94, 748, 91);
			frame.getContentPane().add(display);
			display.setColumns(2);
			
			panel = new JPanel();
			panel.setBounds(15, 222, 748, 604);
			frame.getContentPane().add(panel);
			panel.setLayout(null);
			
			clear = new JButton("C");
			clear.setFont(new Font("宋体", Font.PLAIN, 24));
			clear.setBackground(SystemColor.activeCaptionBorder);
			clear.setForeground(SystemColor.window);
			clear.setBounds(15, 15, 237, 100);
			panel.add(clear);
			
			divide = new JButton("/");
			divide.setFont(new Font("宋体", Font.PLAIN, 24));
			divide.setForeground(SystemColor.window);
			divide.setBackground(SystemColor.activeCaptionBorder);
			divide.setBounds(456, 265, 100, 100);
			panel.add(divide);
			
			multiple = new JButton("X");
			multiple.setFont(new Font("宋体", Font.PLAIN, 24));
			multiple.setForeground(SystemColor.window);
			multiple.setBackground(SystemColor.activeCaptionBorder);
			multiple.setBounds(456, 495, 100, 100);
			panel.add(multiple);
			
			delete = new JButton("\u5220\u9664");
			delete.setFont(new Font("宋体", Font.PLAIN, 24));
			delete.setForeground(SystemColor.window);
			delete.setBackground(SystemColor.activeCaptionBorder);
			delete.setBounds(604, 15, 100, 100);
			panel.add(delete);
			
			btnMc = new JButton("MC");
			btnMc.setFont(new Font("宋体", Font.PLAIN, 24));
			btnMc.setBackground(SystemColor.activeCaptionBorder);
			btnMc.setForeground(SystemColor.window);
			btnMc.setBounds(604, 145, 100, 100);
			panel.add(btnMc);
			
			num7 = new JButton("7");
			num7.setFont(new Font("宋体", Font.PLAIN, 24));
			num7.setForeground(SystemColor.window);
			num7.setBackground(SystemColor.activeCaptionBorder);
			num7.setBounds(15, 145, 100, 100);
			panel.add(num7);
			
			num8 = new JButton("8");
			num8.setFont(new Font("宋体", Font.PLAIN, 24));
			num8.setForeground(SystemColor.window);
			num8.setBackground(SystemColor.activeCaptionBorder);
			num8.setBounds(152, 145, 100, 100);
			panel.add(num8);
			
			num9 = new JButton("9");
			num9.setFont(new Font("宋体", Font.PLAIN, 24));
			num9.setForeground(SystemColor.window);
			num9.setBackground(SystemColor.activeCaptionBorder);
			num9.setBounds(302, 145, 100, 100);
			panel.add(num9);
			
			reduce = new JButton("-");
			reduce.setFont(new Font("宋体", Font.PLAIN, 24));
			reduce.setForeground(SystemColor.window);
			reduce.setBackground(SystemColor.activeCaptionBorder);
			reduce.setBounds(456, 145, 100, 100);
			panel.add(reduce);
			
			btnM = new JButton("M+");
			btnM.setFont(new Font("宋体", Font.PLAIN, 24));
			btnM.setBackground(SystemColor.activeCaptionBorder);
			btnM.setForeground(SystemColor.window);
			btnM.setBounds(604, 265, 100, 100);
			panel.add(btnM);
			
			num1 = new JButton("1");
			num1.setFont(new Font("宋体", Font.PLAIN, 24));
			num1.setBackground(SystemColor.activeCaptionBorder);
			num1.setForeground(SystemColor.window);
			
			num1.setBounds(15, 380, 100, 100);
			panel.add(num1);
			
			num2 = new JButton("2");
			num2.setFont(new Font("宋体", Font.PLAIN, 24));
			num2.setForeground(SystemColor.window);
			num2.setBackground(SystemColor.activeCaptionBorder);
			num2.setBounds(152, 380, 100, 100);
			panel.add(num2);
			
			num3 = new JButton("3");
			num3.setFont(new Font("宋体", Font.PLAIN, 24));
			num3.setBackground(SystemColor.activeCaptionBorder);
			num3.setForeground(SystemColor.window);
			num3.setBounds(302, 380, 100, 100);
			panel.add(num3);
			
			equal = new JButton("=");
			equal.setFont(new Font("宋体", Font.PLAIN, 24));
			equal.setForeground(SystemColor.window);
			equal.setBackground(SystemColor.activeCaptionBorder);
			equal.setBounds(604, 380, 100, 215);
			panel.add(equal);
			
			point = new JButton(".");
			point.setFont(new Font("宋体", Font.PLAIN, 24));
			point.setBackground(SystemColor.activeCaptionBorder);
			point.setForeground(SystemColor.window);
			point.setBounds(302, 495, 100, 100);
			panel.add(point);
			
			num0 = new JButton("0");
			num0.setFont(new Font("宋体", Font.PLAIN, 24));
			num0.setForeground(SystemColor.window);
			num0.setBackground(SystemColor.activeCaptionBorder);
			num0.setBounds(15, 495, 238, 100);
			panel.add(num0);
			
			num4 = new JButton("4");
			num4.setFont(new Font("宋体", Font.PLAIN, 24));
			num4.setForeground(SystemColor.window);
			num4.setBackground(SystemColor.activeCaptionBorder);
			num4.setBounds(15, 265, 100, 100);
			panel.add(num4);
			
			num5 = new JButton("5");
			num5.setFont(new Font("宋体", Font.PLAIN, 24));
			num5.setForeground(SystemColor.window);
			num5.setBackground(SystemColor.activeCaptionBorder);
			num5.setBounds(152, 265, 100, 100);
			panel.add(num5);
			
			num6 = new JButton("6");
			num6.setFont(new Font("宋体", Font.PLAIN, 24));
			num6.setForeground(SystemColor.window);
			num6.setBackground(SystemColor.activeCaptionBorder);
			num6.setBounds(302, 265, 100, 100);
			panel.add(num6);
			
			add = new JButton("+");
			add.setFont(new Font("宋体", Font.PLAIN, 24));
			add.setForeground(SystemColor.window);
			add.setBackground(SystemColor.activeCaptionBorder);
			add.setBounds(456, 380, 100, 100);
			panel.add(add);
			
			btnM_2 = new JButton("M-");
			btnM_2.setFont(new Font("宋体", Font.PLAIN, 24));
			btnM_2.setForeground(SystemColor.window);
			btnM_2.setBackground(SystemColor.activeCaptionBorder);
			btnM_2.setBounds(456, 19, 100, 93);
			panel.add(btnM_2);
			
			btnMr = new JButton("MR");
			btnMr.setFont(new Font("宋体", Font.PLAIN, 24));
			btnMr.setForeground(SystemColor.window);
			btnMr.setBackground(SystemColor.activeCaptionBorder);
			btnMr.setBounds(302, 15, 100, 100);
			panel.add(btnMr);
			//添加监听器
			btnMr.addActionListener(new ForFun());
			btnM_2.addActionListener(new ForFun());
			btnMc.addActionListener(new ForFun());
			btnM.addActionListener(new ForFun());
			clear.addActionListener(new ClearListener());
			num0.addActionListener(new ButtonListener(display,num0));
			num1.addActionListener(new ButtonListener(display,num1));
			num2.addActionListener(new ButtonListener(display,num2));
			num3.addActionListener(new ButtonListener(display,num3));
			num4.addActionListener(new ButtonListener(display,num4));
			num5.addActionListener(new ButtonListener(display,num5));
			num6.addActionListener(new ButtonListener(display,num6));
			num7.addActionListener(new ButtonListener(display,num7));
			num8.addActionListener(new ButtonListener(display,num8));
			num9.addActionListener(new ButtonListener(display,num9));
			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.setHorizontalAlignment(SwingConstants.RIGHT);
			
			operate = new JTextField();
			operate.setForeground(Color.BLACK);
			operate.setHorizontalAlignment(SwingConstants.RIGHT);
			operate.setFont(new Font("宋体", Font.PLAIN, 24));
			operate.setEditable(false);
			operate.setBounds(15, 27, 748, 68);
			frame.getContentPane().add(operate);
			operate.setColumns(10);
			
			JLabel label = new JLabel("");
			label.setHorizontalAlignment(SwingConstants.CENTER);
			label.setIcon(new ImageIcon("E:\\myeclipse\\workspace\\mychat\\calculator\\img\\wait.gif"));
			label.setBounds(232, 831, 322, 83);
			frame.getContentPane().add(label);
			frame.setLocationRelativeTo(null);
			frame.setResizable(false);//不允许用户改变界面大小
			
			menuBar = new JMenuBar();
			frame.setJMenuBar(menuBar);
			
			mnNewMenu = new JMenu("\u9009\u62E9");
			menuBar.add(mnNewMenu);
			
			mntmNewMenuItem = new JMenuItem("\u79D1\u5B66\u8BA1\u7B97\u5668");
			mntmNewMenuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					frame.dispose();
					frame.setVisible(false);
					JOptionPane.showMessageDialog(null, "待跟进");
				}
			});
			mnNewMenu.add(mntmNewMenuItem);
			
			menuItem_1 = new JMenuItem("\u5173\u95ED");
			menuItem_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					System.exit(0);
				}
			});
			mnNewMenu.add(menuItem_1);
			
			mnNewMenu_1 = new JMenu("\u5173\u4E8E");
			menuBar.add(mnNewMenu_1);
			
			menuItem = new JMenuItem("\u5E2E\u52A9");
			menuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					JOptionPane.showMessageDialog(null, "涂伟俊,17年,深圳,忘记了");
				}
			});
			mnNewMenu_1.add(menuItem);
		}
		/*调戏罢了*/
		private class ForFun implements ActionListener{

			@Override
			public void actionPerformed(ActionEvent e) {
				display.setText("哇,这个按键都被你发现了,厉害了!(╯▽╰)(╯▽╰)(╯▽╰)");
				equal.setEnabled(false);
				clear.setBackground(Color.RED);
			}
			
		}
		/*监听运算符包括点*/
		private class CalculatorListener implements ActionListener{
	
			JTextField display;
			JButton str;
			CalculatorListener(JTextField display, JButton str){
				
				this.display = display;
				this.str = str;
			}
			public void actionPerformed(ActionEvent e) {
				
				StringBuffer s = new StringBuffer(display.getText());
				if(flag){
					display.setText("0");
					flag = false;
				} else 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 if(!display.getText().endsWith(".")){
							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{
	
			JTextField display;
			JButton str;
			ButtonListener(JTextField display, JButton str){
				
				this.display = display;
				this.str = str;
			}
			public void actionPerformed(ActionEvent e) {
				System.out.println(display.getText());
				StringBuffer s = new StringBuffer(display.getText());
				if(flag ){
					display.setText(str.getText());
					System.out.println("!!");
					flag = false;
					
				}else if(display.getText().indexOf("Infinity")!=-1||display.getText().indexOf("NaN")!=-1){
					System.out.println("!1!");
				}else if("0".equals(display.getText())){
					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()))
					operate.setText("");
					display.setText("0");
					clear.setBackground(SystemColor.activeCaptionBorder);
					equal.setEnabled(true);
			}
			
		}
		
		//监听删除事件
		private class DeleteListener implements ActionListener{
	
			public void actionPerformed(ActionEvent e) {
				
				String s = display.getText();
				if(s.length()==1){
					display.setText("0");
				}else if(!"".equals(s)&&!"0".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(".")))){
					flag = true;
					operate.setText(display.getText());
					try{
						double result = new CalculatorImpl().toReversePolishNotation(display.getText());
						String restr = new Double(result).toString();
						display.setText(restr);
						//使用转换获取js的eval方法计算,做不了太复杂的混合运算例如:8-6/3+6X3+2.056-96/6
//						ScriptEngineManager manager = new ScriptEngineManager();
//						ScriptEngine se = manager.getEngineByName("js");
//						Object result = null;
//						try {
//							result = se.eval(display.getText());
//						} catch (ScriptException e1) {
//							e1.printStackTrace();
//						}
//						display.setText((String)result);
					} catch(NumberFormatException e1){
						display.setText("错误");
					}
					
					
				}	
			}
		}
		
		public JFrame getJFrame(){
			return this.frame;
		}
		
	}
最近下载更多
guyutian  LV2 4月21日
芋妮奶茶  LV1 4月20日
1053001914  LV1 2023年12月27日
mutouhh  LV5 2023年10月9日
ja12121  LV2 2023年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日
最近浏览更多
xlqb999  LV3 4月29日
guyutian  LV2 4月21日
芋妮奶茶  LV1 4月20日
1053001914  LV1 2023年12月27日
一起加油  LV5 2023年12月27日
clumsy191 2023年12月17日
暂无贡献等级
gk_xmj  LV1 2023年12月6日
1112WHQ  LV7 2023年11月3日
EFWAGGFAWGR 2023年10月19日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友