Applet写的小计算器

在学习Applet时写了一个小的计算器,功能不是很完善,好像是不能进行多次按运算符吧,记的不太清楚了,在这个例子中我使用Panel子类定义数字按钮和远算符按钮,使用它们可以轻松的进行布局.具体代码如下:

欢迎您给出好的意见:

 

/**
 * mengfeng
 * 2009-05-11
 */
package calculator;

import java.applet.*;
imp
ort java.awt.*;
imp
ort java.awt.event.ActionEvent;
imp
ort java.awt.event.ActionListener;
imp
ort javax.swing.JOptionPane;

class operationPanel extends Panel {
 /**
  * 
  */
 private static final long serialVersionUID = 1L;
 public Button[] buto;
 Font fnt1 = new Font("楷体", Font.ITALIC, 24);
 public operationPanel(String str[], GridLayout grd, Color c) {
  buto = new Button[str.length];
  setLayout(grd);
  for (int i = 0; i < str.length; i++) {
   buto[i] = new Button(str[i]);
   buto[i].setBackground(c);
   buto[i].setFont(fnt1);
   add(buto[i]);
  }
 }
}

public class Jisuanqi extends Applet implements ActionListener {
 /**
  * 
  */
 private static final long serialVersionUID = 1L;
 // 运算按钮
 public String stro[] = { "+", "sqrt", "-", "x^", "*", "1/x", "/", "+/-" };
 GridLayout grdo = new GridLayout(4, 2, 3, 3);
 operationPanel ope = new operationPanel(stro, grdo, Color.PINK);
 // 数字按钮及等于按钮
 String strn[] = { "7", "8", "9", "4", "5", "6", "1", "2", "3", "0", ".",
   "  =  " };
 GridLayout grdn = new GridLayout(4, 3, 3, 3);
 operationPanel num = new operationPanel(strn, grdn, Color.lightGray);
 // 控制按钮(C按钮清楚以前所有操作、CE按钮清楚当前输入的数字、
 // DEL按钮如果text文本框中是运算结果清楚结果,否则回删)
 public String strc[] = { "DEL", "CE", "C" };
 GridLayout grdc = new GridLayout(1, 3, 6, 3);
 operationPanel bcc = new operationPanel(strc, grdc, Color.lightGray);

 TextField text = new TextField("0", 20);
 Label prompt = new Label("简单计算器");
 public double first = 0, two = 0, dianyi = 0;    //存放操作数
 public int yunsuanfu=0, yunsuanfuji=0;         //判断是否为单一操作数
 boolean end = false,yunsuanjieguo=false,lianxuyunsuan=false;//判断是否为连续按等号键
 Font fnt = new Font("楷体", Font.BOLD, 24);
 
 public void init() {//布局
  text.setForeground(Color.red);
  setFont(fnt);
  Panel p = new Panel();
  p.setBackground(Color.green);
  p.add(num);
  p.add(ope);
  for (int i = 0; i < num.buto.length; i++) {
   num.buto[i].addActionListener(this);
  }
  for (int j = 0; j < ope.buto.length; j++) {
   ope.buto[j].addActionListener(this);
  }
  for (int k = 0; k < bcc.buto.length; k++) {
   bcc.buto[k].addActionListener(this);
  }
  GridBagLayout gbl = new GridBagLayout();
  GridBagConstraints gbc = new GridBagConstraints();
  setLayout(gbl);

  gbc.gridx = 1;
  gbc.gridy = 1;
  gbc.gridwidth = GridBagConstraints.REMAINDER;
  gbc.gridheight = 1;
  gbc.fill = GridBagConstraints.NONE;
  gbc.anchor = GridBagConstraints.CENTER;
  gbc.weightx = 1;
  gbc.weighty = 0;
  gbc.insets = new Insets(5, 5, 5, 5);
  gbl.setConstraints(prompt, gbc);
  prompt.setBackground(Color.lightGray);
  add(prompt);

  gbc.gridx = 1;
  gbc.gridy = 2;
  gbl.setConstraints(text, gbc);
  add(text);

  gbc.gridx = 1;
  gbc.gridy = 3;
  gbl.setConstraints(bcc, gbc);
  add(bcc);

  gbc.gridx = 1;
  gbc.gridy = 4;
  gbl.setConstraints(p, gbc);
  add(p);
 }
 //两操作数运算
 public void Yunsuano() {
  if (!end == true) {
   two = (Double.valueOf(text.getText())).doubleValue();
  }
  if (yunsuanfu == 0) {
   first = first + two;
  } else if (yunsuanfu == 2) {
   first = first - two;
  } else if (yunsuanfu == 4) {
   first = first * two;
  } else if (yunsuanfu == 6) {
   first = first / two;
  }
  yunsuanjieguo=true;
  text.setText(String.valueOf(first));// 将数值型转换为字符串
 }
 //单一操作数运算
 public void Yunsuanji() {
  if (yunsuanfuji == 1) {
   dianyi = Math.sqrt(dianyi);
  } else if (yunsuanfuji == 3) {
   dianyi = dianyi * dianyi;
  } else if (yunsuanfuji == 5) {
   dianyi = 1 / dianyi;
  } else if (yunsuanfuji == 7) {
   dianyi = -dianyi;
  }
  two = dianyi;
  end = true;
  yunsuanjieguo=true;
  text.setText(String.valueOf(dianyi));// 将数值型转换为字符串
 }

 public void actionPerformed(ActionEvent e) {
  // TODO Auto-generated method stub
  if (e.getActionCommand().equals("DEL")) {
   if (end == false && text.getText().length() > 1) {
    text.setText(text.getText().substring(0,
      text.getText().length() - 1));
   } else
    text.setText("0");
  } else if (e.getActionCommand().equals("CE")) {    
   text.setText("0");
  } else if (e.getActionCommand().equals("C")) {
   first = 0;
   two = 0;
   dianyi = 0;
   yunsuanfu = 0;
   end = false;
   yunsuanjieguo=false;
   lianxuyunsuan=false;
   text.setText("0");
  }
  for (int ic = 0; ic < num.buto.length; ic++) {
   if (e.getSource() == num.buto[ic]) {    
    if(yunsuanjieguo==true){
     text.setText("0");
     yunsuanjieguo=false;
    }
    if (e.getActionCommand().equals(".")) {
     if (!text.getText().contains("."))
      text.setText(text.getText() + ".");
     else
      JOptionPane.showMessageDialog(null, "已经有小数点了");
     break;
    } else if (e.getActionCommand().equals("  =  ")) {
     Yunsuano();
     end=true;
     yunsuanjieguo=true;
     lianxuyunsuan=false;
     break;
    } else {
     if(text.getText().equals("0"))
      text.setText(e.getActionCommand());
     else
      text.setText(text.getText() + e.getActionCommand());
     break;
    }
   }   
  }
  for (int ic = 0; ic < ope.buto.length; ic++) {
   if (e.getSource() == ope.buto[ic]) {
    end = false;
    if (ic % 2 != 0) {
     yunsuanfuji = ic;
     dianyi = (Double.valueOf(text.getText())).doubleValue();
     Yunsuanji();
    } else {     
     if(lianxuyunsuan==false){
      yunsuanfu = ic;
      first = (Double.valueOf(text.getText())).doubleValue();     
      yunsuanjieguo=true;
      lianxuyunsuan=true;
      end=false;
     }else{      
      end=false;      
      Yunsuano(); 
      yunsuanfu = ic;
     }     
    }   
    break;
   }
  }
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值