資源描述:
《java編寫的計(jì)算器》由會員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在行業(yè)資料-天天文庫。
1、packageCalculator;importjava.awt.*;importjavax.swing.*;importjava.awt.Component.*;importjava.awt.TextComponent.*;importjava.awt.event.*;//數(shù)字按鈕classNumBtnextendsJButton{intnumber;Insetsinsets=newInsets(1,1,1,1);publicNumBtn(inti){super(""+i);number=i;setMargin(insets);}publicintgetNum(){retur
2、nnumber;}}//運(yùn)算按鈕classOperBtnextendsJButton{Insetsinsets=newInsets(1,1,1,1);Stringoperator;publicOperBtn(Stringstr){super(str);operator=str;setMargin(insets);}publicStringgetOper(){returnoperator;}}//記憶按鈕classFunBtnextendsJButton{Insetsinsets=newInsets(1,1,1,1);Stringfunction;publicFunBtn(Str
3、ingstr){super(str);function=str;setMargin(insets);}publicStringgetFun(){returnfunction;}}//數(shù)學(xué)按鈕classMathBtnextendsJButton{Insetsinsets=newInsets(1,1,1,1);Stringmath;publicMathBtn(Stringstr){super(str);math=str;setMargin(insets);}publicStringgetMath(){returnmath;}}publicclassCalculatorextends
4、JFrameimplementsActionListener{Insetsinsets=newInsets(1,1,1,1);NumBtnnumbtn[];OperBtnoperbtn[];FunBtnfunbtn[];MathBtnmathbtn[];Stringoper[]={"/","*","-","+"};Stringfun[]={"MC","MR","MS","M+"};Stringmath[]={"sqrt","%","1/x","="};JButtonpotbtn,pmbtn;JPanelpanel,part1,part2,part3;JTextFieldresu
5、lttf,memtf;JButtonbackspace;JButtonce;JButtonc;booleanisNew=true;booleanisNextNum=false;StringnewResult=null;StringnewOperator=null;StringsaveValue=null;booleanisMem;Calculator(){super("計(jì)算器");//第一部分:顯示結(jié)果part1=newJPanel();FlowLayoutflowLayout1=newFlowLayout();flowLayout1.setVgap(0);part1.setL
6、ayout(flowLayout1);resulttf=newJTextField(22);resulttf.setEditable(false);resulttf.setHorizontalAlignment(JTextField.TRAILING);resulttf.setEnabled(true);part1.add(resulttf);part1.setBounds(10,25,250,45);//第二部分:標(biāo)記是否有存儲值以及backspace,ce,cpart2=newJPanel();FlowLayoutflowLayout2=newFlowLayout(Flow
7、Layout.LEFT,10,6);flowLayout2.setVgap(0);part2.setLayout(flowLayout2);memtf=newJTextField(2);memtf.setEditable(false);memtf.setHorizontalAlignment(JTextField.CENTER);memtf.setVisible(true);backspace=newJButton("Backspace");backspace.addActionListen