问题描述
packagecom.core;importjavax.swing.*;importjava.awt.*;importstaticcom.js.util.SwingConsole.*;publicclassMyCalextendsJFrame{staticTextPaneltp=newTextPanel(newBorderLayout());privateButtonPanelbp=newButtonPanel(newGridLayout(4,4,2,2));publicMyCal(){add(tp,BorderLayout.NORTH);add(bp,BorderLayout.CENTER);}publicstaticvoidmain(String[]args){run(newMyCal(),400,350);}}packagecom.core;importjava.awt.event.*;importjava.util.ArrayList;importcom.js.util.*;importjavax.swing.JButton;importjavax.swing.JOptionPane;publicclassManyListeners{privateStringBuildertotalNum=newStringBuilder();privateBooleanflagNum=false;privateBooleanflagPoint=false;privateStack<Character>stackErrCom=newStack<Character>();privateArrayList<Float>af=newArrayList<Float>();classNumberListenerimplementsActionListener{publicvoidactionPerformed(ActionEvente){if(false==flagNum)totalNum.delete(0,20);flagNum=true;Stringname=((JButton)e.getSource()).getText();totalNum.append(name);MyCal.tp.text.setText(totalNum.toString());}}privatevoidoperatorLogic(Characterc,Strings){try{af.add(newFloat(totalNum.toString()));}catch(NumberFormatExceptionnfe){JOptionPane.showMessageDialog(null,"先按数字键并且不要连续按","吓死你",JOptionPane.ERROR_MESSAGE);return;}stackErrCom.push(c);MyCal.tp.text.setText(s);totalNum.delete(0,20);flagPoint=false;}classPlusListenerimplementsActionListener{publicvoidactionPerformed(ActionEvente){operatorLogic('+',"+");}}classDivideListenerimplementsActionListener{publicvoidactionPerformed(ActionEvente){operatorLogic('/',"/");}}classMultiplyListenerimplementsActionListener{publicvoidactionPerformed(ActionEvente){operatorLogic('*',"*");}}classMinusListenerimplementsActionListener{publicvoidactionPerformed(ActionEvente){operatorLogic('-',"-");}}classEqualsListenerimplementsActionListener{privateStringfiltrate(Floatf1){Integern1=f1.intValue();Floatfn1=n1.floatValue();if(f1-fn1==0){returnn1.toString();}else{returnf1.toString();}}publicvoidactionPerformed(ActionEvente){CharacterADD='+';CharacterDIVIDE='/';CharacterMUL='*';CharacterMIN='-';FloattotalF=0.0f;Stringresult=null;af.add(newFloat(totalNum.toString()));totalNum.delete(0,20);while(!stackErrCom.empty()){if((stackErrCom.peek()).equals(ADD)){totalF=af.get(0)+af.get(1);af.set(1,totalF);af.remove(0);}if((stackErrCom.peek()).equals(DIVIDE)){totalF=af.get(0)/af.get(1);af.set(1,totalF);af.remove(0);}if((stackErrCom.peek()).equals(MUL)){totalF=af.get(0)*af.get(1);af.set(1,totalF);af.remove(0);}if((stackErrCom.peek()).equals(MIN)){totalF=af.get(0)-af.get(1);af.set(1,totalF);af.remove(0);}stackErrCom.pop();}result=filtrate(totalF);MyCal.tp.text.setText(result);flagPoint=false;af.clear();totalNum.append(result);flagNum=false;}}classPointListenerimplementsActionListener{publicvoidactionPerformed(ActionEvente){if(flagNum&&!flagPoint){totalNum.append(".");MyCal.tp.text.setText(totalNum.toString());flagPoint=true;}else{try{thrownewPointException("小数点的烦恼");}catch(PointExceptione1){JOptionPane.showMessageDialog(null,"先按数字键并不能按下多个小数点","吓死你",JOptionPane.ERROR_MESSAGE);}}}}privateclassPointExceptionextendsException{publicPointException(){}publicPointException(Stringstr){}}}packagecom.core;importjavax.swing.*;importjava.awt.*;publicclassButtonPanelextendsJPanel{privateJButtonb1=newJButton("1"),b2=newJButton("2"),b3=newJButton("3"),b4=newJButton("4"),b5=newJButton("5"),b6=newJButton("6"),b7=newJButton("7"),b8=newJButton("8"),b9=newJButton("9"),b0=newJButton("0"),bPlus=newJButton("+"),bMinus=newJButton("-"),bMultiply=newJButton("*"),bDivide=newJButton("/"),bEquals=newJButton("="),bPoint=newJButton(".");privateFontfont=newFont("ArialBold",Font.BOLD,16);privateFontfont1=newFont("ArialBold",Font.BOLD,22);privateManyListenersml=newManyListeners();privateManyListeners.NumberListenernl=ml.newNumberListener();privateManyListeners.PlusListenerpl=ml.newPlusListener();privateManyListeners.EqualsListenerel=ml.newEqualsListener();privateManyListeners.PointListenerpol=ml.newPointListener();privateManyListeners.DivideListenerdl=ml.newDivideListener();privateManyListeners.MultiplyListenermul=ml.newMultiplyListener();privateManyListeners.MinusListenermil=ml.newMinusListener();publicButtonPanel(GridLayoutgl){super(gl);add(b1);add(b2);add(b3);add(b4);add(b5);add(b6);add(b7);add(b8);add(b9);add(b0);add(bPlus);add(bMinus);add(bMultiply);add(bDivide);add(bEquals);add(bPoint);b1.setFont(font);b2.setFont(font);b3.setFont(font);b4.setFont(font);b5.setFont(font);b6.setFont(font);b7.setFont(font);b8.setFont(font);b9.setFont(font);b0.setFont(font);bPlus.setFont(font1);bMinus.setFont(font1);bDivide.setFont(font1);bEquals.setFont(font1);bPoint.setFont(font1);bMultiply.setFont(font1);b1.addActionListener(nl);b2.addActionListener(nl);b3.addActionListener(nl);b4.addActionListener(nl);b5.addActionListener(nl);b6.addActionListener(nl);b7.addActionListener(nl);b8.addActionListener(nl);b9.addActionListener(nl);b0.addActionListener(nl);bPlus.addActionListener(pl);bEquals.addActionListener(el);bPoint.addActionListener(pol);bDivide.addActionListener(dl);bMultiply.addActionListener(mul);bMinus.addActionListener(mil);}}packagecom.core;importjavax.swing.*;importjava.awt.*;publicclassTextPanelextendsJPanel{staticJTextFieldtext=newJTextField();privateFontfont2=newFont("ArialBold",Font.BOLD,22);publicTextPanel(BorderLayoutbl){super(bl);text.setEditable(false);text.setHorizontalAlignment(JTextField.RIGHT);text.setFont(font2);add(text);}}packagecom.js.util;importjava.util.LinkedList;publicclassStack<T>{privateLinkedList<T>storage=newLinkedList<T>();publicvoidpush(Tv){storage.addFirst(v);}publicTpeek(){returnstorage.getLast();}publicTpop(){returnstorage.removeLast();}publicbooleanempty(){returnstorage.isEmpty();}publicStringtoString(){returnstorage.toString();}}///:~packagecom.js.util;importjavax.swing.*;publicclassSwingConsole{publicstaticvoidrun(finalJFramef,finalintwidth,finalintheight){SwingUtilities.invokeLater(newRunnable(){@Overridepublicvoidrun(){//TODOAuto-generatedmethodstubf.setTitle(f.getClass().getSimpleName());f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);f.setSize(width,height);f.setVisible(true);f.setResizable(false);}});}}
解决方案
解决方案二:
请帮我看看,不足和改进之处
解决方案三:
来人吧,来人吧
解决方案四:
我勒个去,估计很少有人这么看的
解决方案五:
自己看看,自己检查,。
解决方案六:
解决方案七:
解决方案八:
我不应该去捕获runtimeException
解决方案九:
没注释啊
解决方案十:
你可以把你整个项目发上来,这么一段段的代码谁都看不出来