📄 calculator.java~1~
字号:
package bookmanager;import java.awt.*;import javax.swing.*;import java.awt.event.*;public class CalculateFrame extends JFrame //程序框架继承自JFrame类{ private String result = "0"; private String input = ""; private String sText = "0"; private int numOfPoint = 0; private int numOfHistoryOperation = 0; private String operationOfNow = ""; private String operationOfPast = ""; boolean backSpaceLock = true; boolean LockOfOperation = true; JTextArea help; //帮助 JScrollPane scrollHelp; public CalculateFrame() { String[] buttonValue = new String[]{"1","2","3","/","sqrt","4","5","6","*","%","7","8","9", "-","1/x","0","+/-",".","+","="}; Container container = getContentPane(); final JTextField SText = new JTextField(sText); container.setLayout(new BorderLayout()); operatorButtonListener obtListener = new operatorButtonListener(); JPanel pButton = new JPanel(); pButton.setLayout(new GridLayout(4,5)); JButton[] jbutton = new JButton[20]; for(int i = 0 ; i <jbutton.length ; i++) { jbutton[i] = new JButton(buttonValue[i]); pButton.add(jbutton[i]); //添加按钮 if((i==3||i==4||i==8||i==9)||((i>12)&&(i<=19))&&i!=15) jbutton[i].setForeground(new Color(255, 0, 0)); else jbutton[i].setForeground(new Color(0, 0, 255)); //控制按钮字体颜色 if((i%5)<3&&i<=17) { jbutton[i].addActionListener(new ActionListener() //在内部实现监听器的类 { public void actionPerformed(ActionEvent e) { String s = e.getActionCommand(); backSpaceLock = true; if(numOfPoint>=1 && s.compareTo(".")==0) numOfPoint++; else { if(s.equals(".")) numOfPoint++; if(s.equals("+/-")) //数字符号的转变实现 sText = new String(""+(0-Double.parseDouble(SText.getText()))); else if(!sText.equals("0")||sText.equals(".")&&sText.equals("0")) sText += s; else sText = s; SText.setText(sText); } } });//end 实现内部监听器}//end ifelse if((i+1)%5==0&&i!=19) jbutton[i].addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { sText = SText.getText(); String s = e.getActionCommand(); if(s.equals("sqrt")) { sText =new String(""+Math.sqrt(Double.parseDouble(sText))); SText.setText(sText); input = sText; } else if(s.equals("1/x")) { sText = new String(""+ 1/Double.parseDouble(sText)); SText.setText(sText); input = sText; } }//end public }); //end else-if else jbutton[i].addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { operationOfNow = e.getActionCommand(); input =sText; if(!operationOfNow.equals("=")) { if(!sText.equals("")) { SText.setText("0"); numOfHistoryOperation++; if(numOfHistoryOperation > 1) result = operation(operationOfPast,result,input); else if(numOfHistoryOperation == 1 && LockOfOperation)//第一次输入的情况 { result = input; LockOfOperation = true; }//end if sText = "0"; }//end if operationOfPast = operationOfNow; } //end if else { result = operation(operationOfPast,result,input); LockOfOperation = false; numOfHistoryOperation = 0; } numOfPoint = 0; backSpaceLock = false ; if(result.equals("Infinity")) SText.setText("除数不能为0"); else SText.setText(result); }//end public });//end jbutton }//end for JPanel P2 = new JPanel(); P2.setLayout(new FlowLayout(FlowLayout.RIGHT)); JButton jbuttonBackSpace = new JButton("BackSpace"); jbuttonBackSpace.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(backSpaceLock) { if(sText.length() <= 1) sText = "0"; else sText =sText.substring(0,sText.length()-1); SText.setText(sText); } } }); //end jubttonBackSpace P2.add(jbuttonBackSpace); JButton jbuttonCE = new JButton("CE"); jbuttonCE.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { input = "0"; SText.setText(input); } }); //end jbttonCE P2.add(jbuttonCE); JButton jbuttonC = new JButton("C"); jbuttonC.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { result = "0"; input = "" ; operationOfNow = "" ; operationOfPast = "" ; sText = "0" ; numOfPoint = 0; numOfHistoryOperation = 0; LockOfOperation = true ; SText.setText(sText); } }); P2.add(jbuttonC); JPanel p = new JPanel(); p.setLayout(new BorderLayout()); p.add(pButton,BorderLayout.CENTER); p.add(P2,BorderLayout.NORTH); JPanel p3 = new JPanel(); p3.setLayout(new BorderLayout()); SText.setHorizontalAlignment(JTextField.RIGHT); SText.setEditable(false); p3.add(SText,BorderLayout.NORTH); p3.add(p,BorderLayout.CENTER); container.add(p3,BorderLayout.CENTER); //增加面板的菜单栏 JMenuBar mainMenu; JMenu editMenu, viewMenu, helpMenu; JMenuItem copyItem, pasteItem, tItem, sItem, numberGroup, topHelp, aboutCal; mainMenu = new JMenuBar(); editMenu = new JMenu("编辑(E)"); viewMenu = new JMenu("查看(V)"); helpMenu = new JMenu("帮助(H)"); copyItem = new JMenuItem(" 复制(C) Ctrl+C"); pasteItem = new JMenuItem(" 粘贴(V) Ctrl+V"); editMenu.add(copyItem); editMenu.add(pasteItem); tItem = new JMenuItem("●标准型(T)"); sItem = new JMenuItem(" 科学型(S)"); numberGroup = new JMenuItem(" 数字分组(I)"); viewMenu.add(tItem); viewMenu.add(sItem); viewMenu.add(numberGroup); topHelp = new JMenuItem(" 帮助主题(H)"); help = new JTextArea(5, 20); scrollHelp = new JScrollPane(help); help.setEditable(false); help.append("执行简单计算\n"); help.append("1. 键入计算的第一个数字。\n"); help.append("2. 单击“+”执行加、“-”执行减、“*”执行乘或“/”执行除。\n"); help.append("3. 键入计算的下一个数字。\n"); help.append("4. 输入所有剩余的运算符和数字。\n"); help.append("5. 单击“=”。\n"); aboutCal = new JMenuItem(" 关于计算器(A)"); helpMenu.add(topHelp); helpMenu.add(aboutCal); mainMenu.add(editMenu); mainMenu.add(viewMenu); mainMenu.add(helpMenu); p.add(mainMenu, BorderLayout.NORTH);}//end calcuteFramepublic static void main(String[] args){ JFrame.setDefaultLookAndFeelDecorated(true); CalculateFrame frame = new CalculateFrame(); frame.setTitle("The Front View of a Microwave Oven"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(400, 250); frame.setVisible(true);}public static String operation(String operat,String r,String input){ String result = ""; if(operat.compareTo("+")==0) result = new String(""+(Double.parseDouble(r)+Double.parseDouble(input))); else if(operat.compareTo("*")==0) result = new String(""+(Double.parseDouble(r)*Double.parseDouble(input))); else if(operat.compareTo("/")==0) result = new String(""+(Double.parseDouble(r)/Double.parseDouble(input))); else if(operat.compareTo("-")==0) result = new String(""+(Double.parseDouble(r)-Double.parseDouble(input))); /*else if(operat.compareTo("%")==0) result = new String(""+(Double.parseDouble(r)%Double.parseDouble(input))); else if(operat.compareTo("sqrt")==0) result = new String(""+Math.sqrt(Double.parseDouble(r)));*/ return result ;}class operatorButtonListener implements ActionListener{ public void actionPerformed(ActionEvent e) { }}}//end -application
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -