设为首页 加入收藏

TOP

2012年计算机二级java计算器综合实例学习教程
2014-11-03 13:15:08 】 浏览:6198
Tags:2012年 计算机 二级 java 计算器 综合 实例 学习教程

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[i] = new JButton(buttons[i]);


  pMain_Center.add(buttonMain[i]);


  }


  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[i].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);


  }


  相关推荐:


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇2011年计算机二级考试JAVA知识点.. 下一篇2012年计算机二级Java设置Swing界..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目