import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.MenuElement;
public class Calculator{
public static void main(String agrs[]){
myframe mf=new myframe();
mf.init();
mf.setVisible(true);
mf.setBounds(220, 200, 200, 220);
}
}
class myframe extends Frame implements ActionListener,MouseListener,WindowListener{
/**
*
*/
//private static final long serialVersionUID = 1L;
TextField text;
String str="0", //存放当前数据;
str1="0", //存放计算的第一个数据;
str2="0"; //存放操作符;
double d1=0,d2=0;
boolean flag=false;
String names[]={"9","8","7","6","5","4","3"
,"2","1","0",".","+","-","*","/","√","+/-","=","CE"};
Button b[]=new Button[names.length];
myframe(){
super("简单计算器");
setBackground(Color.gray);
this.setResizable(false);
Panel pa1,pa2;
pa1=new Panel();
pa2=new Panel();
pa2.setLayout(new GridLayout(4,4,6,9));
for(int m=0;m<names.length;m++){
b[m]=new Button(names[m]);
b[m].addActionListener(this);
pa2.add(b[m]);
}
text=new TextField("0",30);
text.setEditable(false);
text.addActionListener(this);
pa1.add(text);
add(pa2,BorderLayout.CENTER);
add(text,BorderLayout.NORTH);
addMouseListener(this);
addWindowListener(this);
pack();
show();
}
public void init(){
MenuBar myBar=new MenuBar();
setMenuBar(myBar);
Menu m1=new Menu("File");
m1.add(new MenuItem("Open"));
MenuItem m11=new MenuItem("Save");
m11.setEnabled(false);
m1.add(m11);
m1.add("Print");
m1.addSeparator();
m1.add("Exit");
m1.addActionListener(this);
myBar.add(m1);
Menu m2=new Menu("Edit");
m2.add("Copy");
Menu m21=new Menu("Color");
m21.add("Red");
m21.add("Blue");
m21.add("Green");
m2.add("Delete");
m2.add("Paste");
m21.addActionListener(this);
m2.add(m21);
//m2.addSeparator();
m2.addActionListener(this);
myBar.add(m2);
Menu m3=new Menu("Help");
m3.add("Abort");
m3.addActionListener(this);
myBar.setHelpMenu(m3);
}
public void actionPerformed(ActionEvent e){
for(int i=0;i<11;i++){
if(e.getSource()==b[i]){
if(str=="0")
str=names[i];
else
str+=names[i];
flag=false;
}
text.setText(str);
}
for(int j=11;j<15;j++){
if(e.getSource()==b[j]){
if(flag==true){
text.setText(str1);
str="0";
str2=names[j];
}
else{
str1=str;
str="0";
str2=names[j];
}
}
}
if(e.getSource()==b[15]){
if(flag==true)
d2=Math.sqrt(Double.valueOf(str1));
else
d2=Math.sqrt(Double.valueOf(str));
str="0";
str1=String.valueOf(d2);
text.setText(str1);
flag=true;
}
if(e.getSource()==b[16]){
d1=Double.valueOf(str);
d1=-d1;
str=String.valueOf(d1);
text.setText(str);
}
if(e.getSource()==b[17]){
if(str2=="+"){
d2=Double.valueOf(str1)+Double.valueOf(str);
str="0";
text.setText(String.valueOf(d2));
}
if(str2=="-"){
d2=Double.valueOf(str1)-Double.valueOf(str);
str="0";
text.setText(String.valueOf(d2));
}
if(str2=="*"){
d2=Double.valueOf(str1)*Double.valueOf(str);
str="0";
text.setText(String.valueOf(d2));
}
if(str2=="/"){
d2=Double.valueOf(str1)/Double.valueOf(str);
str="0";
text.setText(String.valueOf(d2));
}
str1=String.valueOf(d2);
flag=true;
}
if(e.getSource()==b[18]){
str="0";
str1="0";
str2="0";
d1=0;
d2=0;
text.setText(str);
flag=false;
}
if(e.getActionCommand()=="Exit")
System.exit(0);
}
public void mousePressed(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void windowClosing(WindowEvent e){
dispose();
System.exit(0);
}
public void windowOpened(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
public void windowActivated(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}
}
Java编写的简单计算器
需积分: 0 181 浏览量
2010-01-03
21:37:24
上传
评论
收藏 6KB RAR 举报
yw_azrael
- 粉丝: 11
- 资源: 4