java多功能计算器.docx
《java多功能计算器.docx》由会员分享,可在线阅读,更多相关《java多功能计算器.docx(44页珍藏版)》请在冰点文库上搜索。
![java多功能计算器.docx](https://file1.bingdoc.com/fileroot1/2023-5/1/ef496bea-68b4-433b-950a-0b22dcd1f1f3/ef496bea-68b4-433b-950a-0b22dcd1f1f31.gif)
java多功能计算器
importjava.awt.*;
importjava.lang.*;
importjavax.swing.*;
importjavax.swing.event.*;
importjava.awt.event.*;
importjava.text.DecimalFormat;
publicclassCalculatorimplementsActionListener{
intmid;
intfchange;
longianswer;
longivard;
JFrameframe;
JTextFieldtextAnswer;
JPanelpanel,panel1,panel2,panel3,p4;
JMenuBarmainMenu;
JTextFieldtextMemory;
JLabellabelMemSpace;
JButtonbuttonBk,buttonCe,buttonC;
JButtonbutton[];
JButtonbuttonMC,buttonMR,buttonMS,buttonMAdd;
JButtonbsin,bcos,bpi,bmod,bxy,bx3,bln,btan,bx2,bn;
JButtonba,bb,bc,bd,be,bf;
JButtonbuttonDot,buttonAddAndSub,buttonAdd,buttonSub,buttonMul,
buttonDiv,buttonmod;
JButtonbuttonsqrt,buttonDao,buttonEqual;
ButtonGroupgroup;
JMenueditMenu,viewMenu,helpMenu;
JMenuItemcopyItem,pasteItem,tItem,sItem,numberGroup,topHelp,
aboutCal;
DecimalFormatdf;
JRadioButtonrb1,rb2,rb3,rb4;
booleanclickable;
booleanfrist;
doublememoryd;
intmemoryi;
doublevard,answerd;
shortkey=-1,prekey=-1;
Stringcopy;
JTextAreahelp;
JScrollPanescrollHelp;
//构造函数
publicCalculator(){
rb1=newJRadioButton("16进制");
rb1.addActionListener(this);
rb2=newJRadioButton("10进制");
rb2.addActionListener(this);
rb3=newJRadioButton("2进制");
rb3.addActionListener(this);
rb4=newJRadioButton("8进制");
rb4.addActionListener(this);
group=newButtonGroup();
group.add(rb1);
group.add(rb2);
group.add(rb3);
group.add(rb4);
rb2.setSelected(true);
p4=newJPanel();
p4.setLayout(newGridLayout(1,3));
p4.add(rb1);
p4.add(rb2);
p4.add(rb4);
p4.add(rb3);
clickable=true;
answerd=0;
frist=true;
frame=newJFrame("计算器");
df=newDecimalFormat("0.##############");
textAnswer=newJTextField(15);
textAnswer.setEditable(false);
textAnswer.setBackground(newColor(255,255,255));
panel=newJPanel();
frame.getContentPane().add(panel);
panel1=newJPanel();
panel2=newJPanel();
panel.setLayout(newBorderLayout());
mainMenu=newJMenuBar();
editMenu=newJMenu("编辑(E)");
viewMenu=newJMenu("查看(V)");
helpMenu=newJMenu("帮助(H)");
copyItem=newJMenuItem("复制(C)Ctrl+C");
copyItem.addActionListener(this);
pasteItem=newJMenuItem("粘贴(V)Ctrl+V");
pasteItem.addActionListener(this);
editMenu.add(copyItem);
editMenu.add(pasteItem);
tItem=newJMenuItem("标准型(T)");
tItem.addActionListener(this);
sItem=newJMenuItem("科学型(S)");
sItem.addActionListener(this);
numberGroup=newJMenuItem("数字分组(I)");
numberGroup.addActionListener(this);
viewMenu.add(tItem);
viewMenu.add(sItem);
viewMenu.add(numberGroup);
topHelp=newJMenuItem("帮助主题(H)");
topHelp.addActionListener(this);
help=newJTextArea(5,20);
scrollHelp=newJScrollPane(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=newJMenuItem("关于计算器(A)");
aboutCal.addActionListener(this);
helpMenu.add(topHelp);
helpMenu.add(aboutCal);
mainMenu.add(editMenu);
mainMenu.add(viewMenu);
mainMenu.add(helpMenu);
panel.add(mainMenu,BorderLayout.NORTH);
panel.add(textAnswer,BorderLayout.CENTER);
panel.add(panel1,BorderLayout.SOUTH);
panel1.setLayout(newBorderLayout());
textMemory=newJTextField(3);
textMemory.setEditable(false);
textMemory.setBackground(newColor(217,217,217));
labelMemSpace=newJLabel("");
buttonBk=newJButton("Backspace");
buttonBk.setForeground(newColor(255,0,0));
buttonCe=newJButton("ce");
buttonCe.setForeground(newColor(255,0,0));
buttonC=newJButton("c");
buttonC.setForeground(newColor(255,0,0));
buttonBk.addActionListener(this);
buttonCe.addActionListener(this);
buttonC.addActionListener(this);
panel1.add(panel2,BorderLayout.NORTH);
panel2.setLayout(newFlowLayout(FlowLayout.LEFT));
panel2.add(textMemory);
panel2.add(buttonBk);
panel2.add(buttonCe);
panel2.add(buttonC);
panel2.add(p4);
panel3=newJPanel();
panel1.add(panel3,BorderLayout.CENTER);
button=newJButton[10];
for(inti=0;ibutton[i]=newJButton(Integer.toString(i));
button[i].setForeground(newColor(0,0,255));
}
bsin=newJButton("sin");
bsin.setForeground(newColor(0,0,255));
bcos=newJButton("cos");
bcos.setForeground(newColor(0,0,255));
btan=newJButton("tan");
btan.setForeground(newColor(0,0,255));
bpi=newJButton("pi");
bpi.setForeground(newColor(0,0,255));
bxy=newJButton("x^y");
bxy.setForeground(newColor(0,0,255));
bx3=newJButton("x^3");
bx3.setForeground(newColor(0,0,255));
bln=newJButton("ln");
bln.setForeground(newColor(0,0,255));
bx2=newJButton("x^2");
bx2.setForeground(newColor(0,0,255));
bmod=newJButton("mod");
bmod.setForeground(newColor(0,0,255));
bn=newJButton("n!
");
bn.setForeground(newColor(0,0,255));
ba=newJButton("a");
ba.setForeground(newColor(0,0,255));
bb=newJButton("b");
bb.setForeground(newColor(0,0,255));
bc=newJButton("c");
bc.setForeground(newColor(0,0,255));
bd=newJButton("d");
bd.setForeground(newColor(0,0,255));
be=newJButton("e");
be.setForeground(newColor(0,0,255));
bf=newJButton("f");
bf.setForeground(newColor(0,0,255));
buttonMC=newJButton("MC");
buttonMC.setForeground(newColor(255,0,0));
buttonMR=newJButton("MR");
buttonMR.setForeground(newColor(255,0,0));
buttonMS=newJButton("MS");
buttonMS.setForeground(newColor(255,0,0));
buttonMAdd=newJButton("M+");
buttonMAdd.setForeground(newColor(255,0,0));
buttonDot=newJButton(".");
buttonDot.setForeground(newColor(0,0,255));
buttonAddAndSub=newJButton("+/-");
buttonAddAndSub.setForeground(newColor(0,0,255));
buttonAdd=newJButton("+");
buttonAdd.setForeground(newColor(255,0,0));
buttonSub=newJButton("-");
buttonSub.setForeground(newColor(255,0,0));
buttonMul=newJButton("*");
buttonMul.setForeground(newColor(255,0,0));
buttonDiv=newJButton("/");
buttonDiv.setForeground(newColor(255,0,0));
buttonmod=newJButton("%");
buttonmod.setForeground(newColor(0,0,255));
buttonsqrt=newJButton("sqrt");
buttonsqrt.setForeground(newColor(0,0,255));
buttonDao=newJButton("1/x");
buttonDao.setForeground(newColor(0,0,255));
buttonEqual=newJButton("=");
buttonEqual.setForeground(newColor(255,0,0));
buttonMC=newJButton("MC");
buttonMC.setForeground(newColor(255,0,0));
//将所有行为与监听绑定
panel3.setLayout(newGridLayout(5,8));
panel3.add(buttonMC);
buttonMC.addActionListener(this);
panel3.add(button[7]);
button[7].addActionListener(this);
panel3.add(button[8]);
button[8].addActionListener(this);
panel3.add(button[9]);
button[9].addActionListener(this);
panel3.add(buttonDiv);
buttonDiv.addActionListener(this);
panel3.add(buttonsqrt);
buttonsqrt.addActionListener(this);
panel3.add(bsin);
bsin.addActionListener(this);
panel3.add(bcos);
bcos.addActionListener(this);
panel3.add(buttonMR);
buttonMR.addActionListener(this);
panel3.add(button[4]);
button[4].addActionListener(this);
panel3.add(button[5]);
button[5].addActionListener(this);
panel3.add(button[6]);
button[6].addActionListener(this);
panel3.add(buttonMul);
buttonMul.addActionListener(this);
panel3.add(buttonmod);
buttonmod.addActionListener(this);
panel3.add(btan);
btan.addActionListener(this);
panel3.add(bpi);
bpi.addActionListener(this);
panel3.add(buttonMS);
buttonMS.addActionListener(this);
panel3.add(button[1]);
button[1].addActionListener(this);
panel3.add(button[2]);
button[2].addActionListener(this);
panel3.add(button[3]);
button[3].addActionListener(this);
panel3.add(buttonSub);
buttonSub.addActionListener(this);
panel3.add(buttonDao);
buttonDao.addActionListener(this);
panel3.add(bmod);
bmod.addActionListener(this);
panel3.add(bxy);
bxy.addActionListener(this);
panel3.add(buttonMAdd);
buttonMAdd.addActionListener(this);
panel3.add(button[0]);
button[0].addActionListener(this);
panel3.add(buttonAddAndSub);
buttonAddAndSub.addActionListener(this);
panel3.add(buttonDot);
buttonDot.addActionListener(this);
panel3.add(buttonAdd);
buttonAdd.addActionListener(this);
panel3.add(buttonEqual);
buttonEqual.addActionListener(this);
panel3.add(bn);
bn.addActionListener(this);
panel3.add(bx2);
bx2.addActionListener(this);
panel3.add(ba);
ba.addActionListener(this);
panel3.add(bb);
bb.addActionListener(this);
panel3.add(bc);
bc.addActionListener(this);
panel3.add(bd);
bd.addActionListener(this);
panel3.add(be);
be.addActionListener(this);
panel3.add(bf);
bf.addActionListener(this);
panel3.add(bx3);
bx3.addActionListener(this);
panel3.add(bln);
bln.addActionListener(this);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
visi();
textAnswer.setText("0");
fsix();
mid=2;
frame.pack();
frame.show();
fchange=2;
}
publicvoidactionPerformed(ActionEventevent){
booleansign=false;
Objecttemp=event.getSource();
try{
for(inti=0;i<=9;i++)
if(temp==button[i]&&clickable==true){
Strings=textAnswer.getText();
if(s.charAt(0)=='0')
frist=false;
if(frist==true)
textAnswer.setText(textAnswer.getText()
+String.valueOf(i));
else{