JAVA模拟科学计算器.docx
《JAVA模拟科学计算器.docx》由会员分享,可在线阅读,更多相关《JAVA模拟科学计算器.docx(36页珍藏版)》请在冰豆网上搜索。
![JAVA模拟科学计算器.docx](https://file1.bdocx.com/fileroot1/2022-12/15/9eeab62f-be64-4b4e-8f99-dd5fb6b21ad3/9eeab62f-be64-4b4e-8f99-dd5fb6b21ad31.gif)
JAVA模拟科学计算器
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
publicclassCaextendsJFrameimplementsActionListener
{
//***********************************************************************
//标准型计算器控件键钮
staticJFramef1=newJFrame("计算器");
privateStringKEYS[]={"7","8","9","/","sqrt","4","5","6","*","%",
"1","2","3","-","1/x","0","+/-",".","+","="};
privateStringM[]={"","MC","MR","MS","M+"};
privateStringCOM[]={"Backspace","CE","C"};
privateJButtonkeys[]=newJButton[KEYS.length];
privateJButtoncom[]=newJButton[COM.length];
privateJButtonm[]=newJButton[M.length];
privateJTextFieldresult,hh,jj;
MenuBarmenubar1=newMenuBar();
Menuedit=newMenu("编辑(E)"),
view=newMenu("查看(V)"),
help=newMenu("帮助(H)");
MenuItemcopy=newMenuItem("复制(C)"),
paste=newMenuItem("粘贴(P)"),
standar=newMenuItem("标准型(T)"),
science=newMenuItem("科学型(S)"),
numgroup=newMenuItem("数字分组(I)"),
helpt=newMenuItem("帮助主题(H)"),
about=newMenuItem("关于计算器(A)");
privatebooleanfirstDigit=true;//判断第一个输入的字符是否是数字
privatedoubleresultNum=0.0,resultNum2=0.0;//中间结果
privateStringoperator="=",co,str,operator2="=";
privatebooleanoperateValidFlag=true;
//***********************************************************************
//科学性计算器控件及按钮
staticJFramef2=newJFrame("科学计算器");
privateJTextFieldresult2;
privateString[]KON={"sta","F-E","(",")","MC","Ave","dms","Exp",
"In","MR","Sum","sin","x^y","log","MS","s","cos","x^3","n!
",
"M+","Dat","tan","x^2","1/x","pi"};
privateString[]KO={"7","8","9","/","Mod","And",
"4","5","6","*","Or","Xor",
"1","2","3","-","Lsh","Not",
"0","+/-",".","+","=","Int",
"A","B","C","D","E","F"};
privateStringITM[]={"标准型","科学型","十六进制数(H)F5","十进制(D)F6",
"八进制(O)F7","二进制(B)F8","四字(Q)F12","DWORD值(R)F2","单字(W)F3","字节(Y)F4"
"数字分组(I)"};
privatefinalString[]COMM={"Backspace","CE","C"};
privateJButtoncomm[]=newJButton[COMM.length];
privateJButtonkon[]=newJButton[KON.length];
privateJButtonko[]=newJButton[KO.length];
privateMenuItemitm[]=newMenuItem[ITM.length];
privateJPanelpanel0,panel1,panel2,panel3,panel4,panel5,panel6;
Checkboxcheckbox1,checkbox2,checkbox3,checkbox4;
MenuBarmenubar2=newMenuBar();//添加菜单条
Menuedit2=newMenu("编辑(E)"),
view2=newMenu("查看(V)"),
help2=newMenu("帮助(H)");
MenuItemcopy2=newMenuItem("复制(C)Ctrl+C"),
past2=newMenuItem("粘贴(P)Ctrl+V"),
standar2=newMenuItem("标准型(T)"),
science2=newMenuItem("科学型(S)"),
numgroup2=newMenuItem("数字分组(I)"),
menuitem1=newMenuItem("十六进制数(H)F5"),
menuitem2=newMenuItem("十进制(D)F6"),
menuitem3=newMenuItem("八进制(O)F7"),
menuitem4=newMenuItem("二进制(B)F8"),
menuitem5=newMenuItem("四字(Q)F12"),
menuitem6=newMenuItem("DWORD值(R)F2"),
menuitem7=newMenuItem("单字(W)F3"),
menuitem8=newMenuItem("字节(Y)F4"),
helpt2=newMenuItem("帮助主题(H)"),
about2=newMenuItem("关于计算器(A)");
//***********************************************************************
//构造函数
publicCa()
{
//标准型开始初始化********************
result=newJTextField("0.");
result.setHorizontalAlignment(JTextField.RIGHT);//文字右对齐
result.setEditable(false);//设置为不可编辑
result.setBackground(Color.WHITE);//设置颜色为白色
JPanelk0=newJPanel();
k0.setLayout(newGridLayout(4,5,5,5));
for(inti=0;i{
keys[i]=newJButton(KEYS[i]);
k0.add(keys[i]);
keys[i].setForeground(Color.blue);
}
keys[3].setForeground(Color.red);
keys[8].setForeground(Color.red);
keys[13].setForeground(Color.red);
keys[18].setForeground(Color.red);
keys[19].setForeground(Color.red);
JPanelc0=newJPanel();
c0.setLayout(newGridLayout(1,3,5,5));
for(inti=0;i{
com[i]=newJButton(COM[i]);
c0.add(com[i]);
com[i].setForeground(Color.red);
}
JPanelm0=newJPanel();
m0.setLayout(newGridLayout(5,1,5,5));
for(inti=0;i{
m[i]=newJButton(M[i]);
m0.add(m[i]);
m[i].setForeground(Color.red);
}
m[0].setForeground(Color.black);
//f1的监视器
for(inti=0;i{
keys[i].addActionListener(this);
}
for(inti=0;i{
com[i].addActionListener(this);
}
for(inti=0;i{
m[i].addActionListener(this);
}
//复制事件
copy.addActionListener(newActionListener()
{
publicvoidactionPerformed(ActionEvente)
{
intcount;
count=str.length();
if(count!
=0)
co=result.getText();
else
System.out.println("没有可复制的对象");
}
});
//粘贴事件
paste.addActionListener(newActionListener()
{
publicvoidactionPerformed(ActionEvente)
{
str=co;
result.setText(str);
}
});
//复制事件
copy2.addActionListener(newActionListener()
{
publicvoidactionPerformed(ActionEvente)
{
intcount;
count=str.length();
if(count!
=0)
co=result2.getText();
else
System.out.println("没有可复制的对象");
}
});
//粘贴事件
past2.addActionListener(newActionListener()
{
publicvoidactionPerformed(ActionEvente)
{
str=co;
result2.setText(str);
}
});
edit.add(copy);
edit.add(paste);
view.add(standar);
view.add(science);
view.addSeparator();
view.add(numgroup);
help.add(helpt);
help.addSeparator();
help.add(about);
menubar1.add(edit);
menubar1.add(view);
menubar1.add(help);
//整体布局
JPanelp1=newJPanel();
p1.setLayout(newBorderLayout());
p1.add("North",c0);
p1.add("Center",k0);
JPanelp2=newJPanel();
p2.setLayout(newBorderLayout());
p2.add("Center",result);
f1.setLayout(newBorderLayout(3,5));
f1.add("North",p2);
f1.add("Center",p1);
f1.add("West",m0);
f1.setBackground(Color.LIGHT_GRAY);
f1.setLocation(200,200);
f1.setMenuBar(menubar1);
f1.setVisible(true);
f1.setResizable(false);//不可最大化
f1.pack();//紧凑
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//标准型初始化结束
//****************************************************
standar2.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
f1.setVisible(true);
f2.setVisible(false);//按钮不可操作
}});
science.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
f1.setVisible(false);
f2.setVisible(true);//按钮不可操作
}});
//****************************************************
//科学性初始化开始
panel0=newJPanel();
panel1=newJPanel();
panel2=newJPanel();
panel3=newJPanel();
panel4=newJPanel();
panel5=newJPanel();
panel6=newJPanel();
panel0.setLayout(newGridLayout(1,4));//设置网格型布局管理器
panel1.setLayout(newGridLayout(1,3,30,0));
panel2.setLayout(newGridLayout(1,2));
panel3.setLayout(newGridLayout(1,3,5,0));
panel4.setLayout(newGridLayout(5,5,5,5));
panel5.setLayout(newGridLayout(5,6,5,5));
panel6.setLayout(newFlowLayout(FlowLayout.LEFT));//按钮左对齐
result2=newJTextField("0.",100);
result2.setHorizontalAlignment(JTextField.RIGHT);//文字右对齐
result2.setEditable(false);//设置为文本不可编辑
result2.setBackground(Color.WHITE);//设置文本框颜色为白色
CheckboxGroupcbg1=newCheckboxGroup();
checkbox1=newCheckbox("十六进制",false,cbg1);
checkbox2=newCheckbox("十进制",true,cbg1);
checkbox3=newCheckbox("八进制",false,cbg1);
checkbox4=newCheckbox("二进制",false,cbg1);
panel0.add(checkbox1);
panel0.add(checkbox2);
panel0.add(checkbox3);
panel0.add(checkbox4);
CheckboxGroupcbg2=newCheckboxGroup();
panel1.add(newCheckbox("角度",true,cbg2));
panel1.add(newCheckbox("弧度",false,cbg2));
panel1.add(newCheckbox("梯度",false,cbg2));
JLabelHH=newJLabel("");
panel2.add(newCheckbox("Inv",false));
panel2.add(newCheckbox("Hyp",false));
hh=newJTextField();
jj=newJTextField();
hh.setText("");
hh.setEditable(false);
jj.setText("");
jj.setEditable(false);
panel2.add(hh);panel2.add(HH);panel2.add(jj);
panel6.add(result2);
edit2.add(copy2);
edit2.add(past2);
view2.add(standar2);
view2.add(science2);
view2.addSeparator();
view2.add(menuitem1);
view2.add(menuitem2);
view2.add(menuitem3);
view2.add(menuitem4);
view2.addSeparator();
view2.add(menuitem5);
view2.add(menuitem6);
view2.add(menuitem7);
view2.add(menuitem8);
view2.addSeparator();
view2.add(numgroup2);
help2.add(helpt2);
help2.addSeparator();
help2.add(about2);
menubar2.add(edit2);
menubar2.add(view2);
menubar2.add(help2);
for(inti=0;i{
comm[i]=newJButton(COMM[i]);
panel3.add(comm[i]);
comm[i].setForeground(Color.red);
}
for(inti=0;i{
kon[i]=newJButton(KON[i]);
panel4.add(kon[i]);
kon[i].setForeground(Color.magenta);
}
kon[4].setForeground(Color.red);
kon[9].setForeground(Color.red);
kon[14].setForeground(Color.red);
kon[19].setForeground(Color.red);
kon[0].setForeground(Color.blue);
kon[24].setForeground(Color.blue);
kon[5].setForeground(Color.blue);
kon[10].setForeground(Color.blue);
kon[15].setForeground(Color.blue);
kon[20].setForeground(Color.blue);
for(inti=0;i{
ko[i]=newJButton(KO[i]);
panel5.add(ko[i]);
ko[i].setForeground(Color.red);
}
ko[0].setForeground(Color.blue);
ko[1].setForeground(Color.blue);
ko[2].setForeground(Color.blue);
ko[6].setForeground(Color.blue);
ko[7].setForeground(Color.blue);
ko[8].setForeground(Color.blue);
ko[12].setForeground(Color.blue);
ko[13].setForeground(Color.blue);
ko[14].setForeground(Color.blue);
ko[18].setForeground(Color.blue);
ko[19].setForegrou