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(); } }
最近下载更多
最近浏览更多
微信网友_7008818781589504 LV1
5月25日
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日
暂无贡献等级