灯火互联
管理员
管理员
  • 注册日期2011-07-27
  • 发帖数41778
  • QQ
  • 火币41290枚
  • 粉丝1086
  • 关注100
  • 终身成就奖
  • 最爱沙发
  • 忠实会员
  • 灌水天才奖
  • 贴图大师奖
  • 原创先锋奖
  • 特殊贡献奖
  • 宣传大使奖
  • 优秀斑竹奖
  • 社区明星
阅读:2987回复:0

[二级考试]2012年计算机二级java计算器综合实例学习教程(1)

楼主#
更多 发布于:2012-08-22 13:50



2012年计算机二级java计算器综合实例学习教程(1)
8.8 java计算器综合实例   本节给出了一个计算器的综合实例,该实例主要利用了本章及上一章所涉及的SWING组件及组件事件的处理来实现的,在实现的过程中还使用到了异常处理的知识。
  [例8-14]
  import java.awt.*;
  import java.awt.event.*;
  import javax.swing.*;
  class Calculator extends JFrame{
  String[] buttons =
  new String[]{"7","8","9","/","4","5","6","*","1","2","3","-","0","+/-",".","+"};
  JPanel pWest = new JPanel();
  JPanel pMain = new JPanel();
  JPanel pNorth = new JPanel();
  JPanel pMain_North = new JPanel();
  JPanel pMain_Center = new JPanel();
  JPanel pMain_East = new JPanel();
  JButton buttonMain[];//包括数字0-9以及'+','-','*','+'/'.','+/-'
  JButton buttonNorth[];//包括Back,CE,C
  JButton buttonEast[];//包括sqrt,%,1/x,=
  JTextField fieldResult = new JTextField();
  //
  static private String value1="";//第一个操作数
  static private String value2="";//第二个操作数
  static private String oper="";//操作符
  static private String result="";//运算结果
  static private String lastChar="";//相比当前,其上一个按下按钮的字符
  static private boolean value1Input=true;//处于第一个操作数输入情况
  static private boolean value2Input=false;//出于第二个操作数输入情况
  static private boolean start = false;//是否开始计算,主要是用于检测刚开始时按下非数字键的情况。
  public Calculator(String title){
  super(title);
  Container contentPane = this.getContentPane();
  fieldResult.setHorizontalAlignment(JTextField.RIGHT);//右对齐
  pMain_Center.setLayout(new GridLayout(4,4,5,5));
  buttonMain= new JButton[buttons.length];
  for(int i=0;i  buttonMain = new JButton(buttons);
  pMain_Center.add(buttonMain);
  }
  buttonNorth = new JButton[3];
  buttonNorth[0] = new JButton("Back");
  buttonNorth[1] = new JButton("CE");
  buttonNorth[2] = new JButton("C");
  pMain_North.setLayout(new GridLayout(1,3,5,5));
  pMain_North.add(buttonNorth[0]);
  pMain_North.add(buttonNorth[1]);
  pMain_North.add(buttonNorth[2]);
  buttonEast = new JButton[4];
  buttonEast[0] = new JButton("sqrt");
  buttonEast[1] = new JButton("%");
  buttonEast[2] = new JButton("1/x");
  buttonEast[3] = new JButton("=");
  pMain_East.setLayout(new GridLayout(4,1,5,5));
  pMain_East.add(buttonEast[0]);
  pMain_East.add(buttonEast[1]);
  pMain_East.add(buttonEast[2]);
  pMain_East.add(buttonEast[3]);
  contentPane.add(fieldResult,BorderLayout.NORTH);
  pMain.setLayout(new BorderLayout(5,5));
  pMain.add(pMain_Center,BorderLayout.WEST);
  pMain.add(pMain_East,BorderLayout.EAST);
  pMain.add(pMain_North,BorderLayout.NORTH);
  contentPane.add(pMain,BorderLayout.CENTER);
  pack();
  setVisible(true);
  //注册监听器
  MyActionListener listener = new MyActionListener();
  for(int i=0;i  buttonMain.addActionListener(listener);
  }
  buttonNorth[0].addActionListener(listener);//Back
  buttonEast[3].addActionListener(listener);//"="
  buttonNorth[2].addActionListener(listener);//"C"
  buttonMain[13].setEnabled(false);//设置+/-不可用
  buttonMain[14].setEnabled(false);//设置.不可用
  buttonNorth[1].setEnabled(false);//设置CE不可用
  buttonEast[0].setEnabled(false);//设置sqrt不可用
  buttonEast[1].setEnabled(false);//设置%不可用
  buttonEast[2].setEnabled(false);//设置1/x不可用
  this.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
  }
  private class MyActionListener implements ActionListener{
  public void actionPerformed(ActionEvent e) {
  String str = e.getActionCommand();
  //数字键
  if(Character.isDigit(str.charAt(0))){
  handalDigit(str);
  }
  if(start == true){
  if(str.equals("=")){//"="键
  handalEqual(str);
  return;
  }
  //Back、C键处理,完善本程序时可在下面的语句中添加对其他按钮的处理
  if(str.equals("Back")||str.equals("C")){
  handalNonOper(str);
  }
  //'+','-','*','+'/'的处理,完善本程序时可添加sqrt,%等操作符的处理
  if(str.equals("+")||str.equals("-")||str.equals("*")||str.equals("/")){
  handalOper(str);
  }
  }
  }
  //处理数字,str表示按钮上的文字
  private void handalDigit(String str){
  char currentChar=str.charAt(0);//获得按下的字符;
  if(!lastChar.equals("=")){
  if(value2Input == false){
  value1+=String.valueOf(currentChar);
  if(value1.charAt(0)=='0'){//第一个数字为0
  value1="0";
  }
  fieldResult.setText(value1);
  }
#g_kclist{font-size:12px;width:570px;float:none; margin-top:5px; clear:right}#g_kclist a{color:#000; text-decoration:none}#g_kclist h2{margin:0px;padding:0px;font-size:14px; text-align:center;background:url(
) no-repeat;line-height:31px;color:#fff}#g_kclist table{line-height:25px;background:#B0DA90;margin-top:8px}#g_kclist table td{ text-align:center;background:#fff}#g_kclist table td.td1 a{color:#f00}#g_kclist table th{background:#F2F7ED;color:#525F46}

喜欢0 评分0
游客

返回顶部