计算器小程序javaWord下载.docx
《计算器小程序javaWord下载.docx》由会员分享,可在线阅读,更多相关《计算器小程序javaWord下载.docx(5页珍藏版)》请在冰豆网上搜索。
importjava.awt.event.WindowListener;
publicclassExam1_1extendsFrameimplementsActionListener,WindowListener{
TextFieldtext;
Stringa[];
Stringnames[]={"
<
-"
"
CE"
C"
cos"
sin"
7"
8"
9"
/"
sqrt"
4"
5"
6"
×
"
%"
1"
2"
3"
1/x"
0"
+/-"
."
+"
="
};
Buttonbut[]=newButton[names.length];
Stringss=newString();
Strings=newString();
StringBuffersb=newStringBuffer();
Exam1_1(){
super("
计算器"
);
inti;
text=newTextField();
Panelp=newPanel();
p.setLayout(newGridLayout(5,5,5,5));
for(i=0;
i<
names.length;
i++){
but[i]=newButton(names[i]);
but[i].addActionListener(this);
but[i].setForeground(Color.red);
p.add(but[i]);
}
setSize(300,300);
Fontf=newFont("
楷体"
Font.BOLD,25);
setFont(f);
setLayout(newBorderLayout(10,10));
add(text,BorderLayout.NORTH);
add(p,BorderLayout.CENTER);
setVisible(true);
addWindowListener(this);
}
publicvoidactionPerformed(ActionEventarg0){
ss=arg0.getActionCommand();
if(ss.equals("
)){s=sb.substring(0,s.length()-1);
text.setText(s);
}
)){sb=newStringBuffer();
s=sb.toString();
text.setText("
"
)){floatf=Float.valueOf(text.getText()).floatValue();
doublem=Math.cos(f);
sb=newStringBuffer();
;
text.setText(String.valueOf(m));
doublem=Math.sin(f);
)){sb.append("
doublem=Math.sqrt(f);
doublem=1/f;
doublem=-f;
)){
if(s.indexOf("
)>
=1){
a=s.split("
2);
doublei1=Double.valueOf(a[0]).doubleValue();
doublei2=Double.valueOf(a[1]).doubleValue();
doublei3=i1+i2;
text.setText(String.valueOf(i3));
}
elseif(s.indexOf("
a=s.split("
doublej1=Double.valueOf(a[0]).doubleValue();
doublej2=Double.valueOf(a[1]).doubleValue();
doublej3=j1-j2;
text.setText(String.valueOf(j3));
doublei3=i1*i2;
doublei3=i1/i2;
doublei3=i1%i2;
publicvoidwindowActivated(WindowEventarg0){
//TODOAuto-generatedmethodstub
publicvoidwindowClosed(WindowEventarg0){
publicvoidwindowClosing(WindowEventarg0){
Windoww=(Window)arg0.getSource();
w.setVisible(false);
w.dispose();
publicvoidwindowDeactivated(WindowEventarg0){
publicvoidwindowDeiconified(WindowEventarg0){
publicvoidwindowIconified(WindowEventarg0){
publicvoidwindowOpened(WindowEventarg0){