用JAVA写的一个只基于awt的计算器---比较简陋,继续加油!

//设计一个简单的计算器,可以关闭窗口,有菜单,能够实现+、-、*、/、百分号、根号、倒数运算


import java.awt.*;
import java.awt.event.*;
import java.lang.Math.*;

public class LittleCalculator implements ActionListener,WindowListener{
Frame f;
TextField t;
Button b[];
String name[]={"7","8","9","sqrt","CE","4","5","6","%","1/x","1","2","3","*","/","0",".","=","-","+"};
Panel p1=new Panel();
    Panel p2=new Panel(); //创建2个面板,用于放文本、按钮
   
    
    Double num1;
    Double num2;
    String tmp;
    Double num;
    int i;

public void tt(){
f=new Frame("计算器");
t=new TextField(24);
Button[] b=new Button[name.length];
f.addWindowListener(this); //关闭窗口的功能

MenuBar m=new MenuBar();
Menu me1=new Menu("查看(V)");
Menu me2=new Menu("编辑(E)");
Menu me3=new Menu("帮助(H)");
MenuItem mt1=new MenuItem("控制台");
MenuItem mt2=new MenuItem("控制台2");
MenuItem mt3=new MenuItem("控制台3"); //设置菜单的功能
mt1.addActionListener(this);
mt2.addActionListener(this);
mt3.addActionListener(this);
me1.add(mt1);
me2.add(mt2);
me3.add(mt3);
m.add(me1);
m.add(me2);
m.add(me3);
f.setMenuBar(m);


p1.add(t);
f.add(p1,BorderLayout.NORTH);
p2.setLayout(new GridLayout(4,5,2,2));
for(int i=0;i<name.length;i++){
b[i]=new Button(name[i]); //这里是按钮的布局
p2.add(b[i]);
}
f.add(p2,BorderLayout.CENTER);


for(int i=0;i<name.length;i++){
b[i].addActionListener(this);}


f.setBackground(Color.blue);
f.setSize(200,200);
f.setVisible(true);
}

public void windowClosing(WindowEvent e)
{System.exit(0);}
public void windowClosed(WindowEvent e){}
public void windowOpened(WindowEvent e){}
public void windowIconified(WindowEvent e){} //实现关闭窗口
public void windowDeiconified(WindowEvent e){}
public void windowActivated(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}


public void actionPerformed(ActionEvent e){ //实现消息映射

if(e.getActionCommand().equals("0")||e.getActionCommand().equals("1")||e.getActionCommand().equals("2")||e.getActionCommand().equals("3")||e.getActionCommand().equals("4")||e.getActionCommand().equals("5")||e.getActionCommand().equals("6")||e.getActionCommand().equals("7")||e.getActionCommand().equals("8")||e.getActionCommand().equals("9"))
t.setText(t.getText()+e.getActionCommand()); //将标签中的数字放入文本框中,并且是累加的,不然就只能存放一位数
   
   
   
    if(e.getActionCommand().equals("CE"))
    {
     t.setText(""); //如果按钮是CE,那么就讲文本中内容置空
    }
    
    if(e.getActionCommand().equals("+")){
    tmp=t.getText();
    num=Double.parseDouble(tmp); //如果为+,把之前文本框中的字符串转换成为数字,然后将文本框清空,以备存放下一个数字
    t.setText(""); //-、*、/同理
    i=0; //做一个标记
    }
    
    if(e.getActionCommand().equals("-")){
    tmp=t.getText();
    num=Double.parseDouble(tmp);
    t.setText("");
    i=1;
    }
    
    if(e.getActionCommand().equals("*")){
    tmp=t.getText();
    num=Double.parseDouble(tmp);
    t.setText("");
    i=2;
    }
    
    if(e.getActionCommand().equals("/")){
    tmp=t.getText();
    num=Double.parseDouble(tmp);
    t.setText("");
    i=3;
    }
    
    if(e.getActionCommand().equals(".")){
    t.setText(t.getText()+".");
    }
    
    //下面几个就是单目运算了啦~
    
    if(e.getActionCommand().equals("sqrt")){
    tmp=t.getText();
    num1=Double.parseDouble(tmp); //把字符串转成数字计算,再把计算出来的数字转成字符串显示到文本框
    num2=Math.sqrt(num1); //%、1/x同理
    t.setText(Double.toString(num2));
    }
    
    if(e.getActionCommand().equals("%")){
    tmp=t.getText();
    num1=Double.parseDouble(tmp);
    num2=num1/100;
    t.setText(Double.toString(num2));
    }
    
    if(e.getActionCommand().equals("1/x")){
    tmp=t.getText();
    num1=Double.parseDouble(tmp);
    num2=1/num1;
    t.setText(Double.toString(num2));
    }
    
  
    //最后是等于号
    
    if(e.getActionCommand().equals("=")||e.getActionCommand().equals("Enter")){
    tmp=t.getText();
    double num_after=Double.parseDouble(tmp); //这是把等于号之前,+号之后的字符串转换成数字,因为在+号的时候已经将前面内容清空了
    if(i==0){
    t.setText(Double.toString(num+num_after));
    }
    if(i==1){
    t.setText(Double.toString(num-num_after));
    }
    if(i==2){
    t.setText(Double.toString(num*num_after));
    }
    if(i==3){
    if(num_after==0){
    t.setText("");
    System.out.println("除数不能为零!");
    }
    else{
    t.setText(Double.toString(num/num_after));
    }
    }
    }
    
  
   
    } 

public static void main(String args[]){
LittleCalculator p=new LittleCalculator();
p.tt();
}

}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值