java 设计 小型计算器 源码 (2)_叛逆年少_新浪博客

//将字符串类型转换为浮点型

  private static float parseNumber(String number) {

    float real_number;

 

    try {

      real_number = Float.parseFloat(number);

    } catch (NumberFormatException e) {

      real_number = Float.NaN;

    }

 

    return real_number;

  }

 

}

// CalculatorGUI.java

import java.awt.*;

import java.awt.event.*;

 

public class CalculatorGUI {

   

  // 定义按钮

  private Button key0, key1, key2, key3, key4;

  private Button key5, key6, key7, key8, key9;

  private Button keyequal, keyplus, keyminus;

  private Button keyperiod, keymult, keydiv;

 

  // 定义存放按钮的区域

  private Panel buttonArea;

 

  // 定义存放运算结果的区域

  private Label answer;

 

  // 用来实现运算功能的类的对象实例化

  private Calculator calculator;

  private boolean    readyForNextNumber;

 

  public CalculatorGUI() {

    calculator = new Calculator();

    readyForNextNumber = true;

 

    answer = new Label("0.0",Label.RIGHT);

 

    key0 = new Button("0");

    key1 = new Button("1");

    key2 = new Button("2");

    key3 = new Button("3");

    key4 = new Button("4");

    key5 = new Button("5");

    key6 = new Button("6");

    key7 = new Button("7");

    key8 = new Button("8");

    key9 = new Button("9");

    keyequal = new Button("=");

    keyplus = new Button("+");

    keyminus = new Button("-");

    keymult = new Button("*");

    keydiv = new Button("/");

    keyperiod = new Button(".");

    buttonArea = new Panel();

  }

 

  public void launchFrame() {

    buttonArea.setLayout(new GridLayout(4,4));

 

    buttonArea.add(key7);

    buttonArea.add(key8);

    buttonArea.add(key9);

    buttonArea.add(keyplus);

    buttonArea.add(key4);

    buttonArea.add(key5);

    buttonArea.add(key6);

    buttonArea.add(keyminus);

    buttonArea.add(key1);

    buttonArea.add(key2);

    buttonArea.add(key3);

    buttonArea.add(keymult);

    buttonArea.add(key0);

    buttonArea.add(keyperiod);

    buttonArea.add(keyequal);

    buttonArea.add(keydiv);

 

    // 设置事件接收句柄

    OpButtonHanlder op_handler = new OpButtonHanlder();

    NumberButtonHanlder number_handler = new NumberButtonHanlder();

    key0.addActionListener(number_handler);

    key1.addActionListener(number_handler);

    key2.addActionListener(number_handler);

    key3.addActionListener(number_handler);

    key4.addActionListener(number_handler);

    key5.addActionListener(number_handler);

    key6.addActionListener(number_handler);

    key7.addActionListener(number_handler);

    key8.addActionListener(number_handler);

    key9.addActionListener(number_handler);

    keyperiod.addActionListener(number_handler);

    keyplus.addActionListener(op_handler);

    keyminus.addActionListener(op_handler);

    keymult.addActionListener(op_handler);

    keydiv.addActionListener(op_handler);

    keyequal.addActionListener(op_handler);

 

   

//July/整理/05.23/。

已投稿到:
郑重声明:资讯 【java 设计 小型计算器 源码 (2)_叛逆年少_新浪博客】由 发布,版权归原作者及其所在单位,其原创性以及文中陈述文字和内容未经(企业库qiyeku.com)证实,请读者仅作参考,并请自行核实相关内容。若本文有侵犯到您的版权, 请你提供相关证明及申请并与我们联系(qiyeku # qq.com)或【在线投诉】,我们审核后将会尽快处理。
—— 相关资讯 ——