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 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 } 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); }
① 凡本网注明稿件来源为"原创"的所有文字、图片和音视频稿件,版权均属本网所有。任何媒体、网站或个人转载、链接转贴或以其他方式复制发表时必须注明"稿件来源:我考网",违者本网将依法追究责任;
② 本网部分稿件来源于网络,任何单位或个人认为我考网发布的内容可能涉嫌侵犯其合法权益,应该及时向我考网书面反馈,并提供身份证明、权属证明及详细侵权情况证明,我考网在收到上述法律文件后,将会尽快移除被控侵权内容。