下面是本站小编带来的Java计算器综合实例学习教程,欢迎学习!
本节给出了一个计算器的综合实例,该实例主要利用了本章及上一章所涉及的SWING组件及组件事件的.处理来实现的,在实现的过程中还使用到了异常处理的知识。
[例8-14]
import .*;
import t.*;
import g.*;
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 = ontentPane();
orizontalAlignment(T);//右对齐
pMain_ayout(new GridLayout(4,4,5,5));
buttonMain= new JButton[th];
for(int i=0;i
buttonMain[i] = new JButton(buttons[i]);
pMain_(buttonMain[i]);
}
buttonNorth = new JButton[3];
buttonNorth[0] = new JButton("Back");
buttonNorth[1] = new JButton("CE");
buttonNorth[2] = new JButton("C");
pMain_ayout(new GridLayout(1,3,5,5));
pMain_(buttonNorth[0]);
pMain_(buttonNorth[1]);
pMain_(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_ayout(new GridLayout(4,1,5,5));
pMain_(buttonEast[0]);
pMain_(buttonEast[1]);
pMain_(buttonEast[2]);
pMain_(buttonEast[3]);
(fieldResult,H);
ayout(new BorderLayout(5,5));
(pMain_Center,);
(pMain_East,);
(pMain_North,H);
(pMain,ER);
pack();
setVisible(true);
//注册监x器
MyActionListener listener = new MyActionListener();
for(int i=0;i
buttonMain[i]ctionListener(listener);
}
buttonNorth[0]ctionListener(listener);//Back
buttonEast[3]ctionListener(listener);//"="
buttonNorth[2]ctionListener(listener);//"C"
buttonMain[13]nabled(false);//设置+/-不可用
buttonMain[14]nabled(false);//设置.不可用
buttonNorth[1]nabled(false);//设置CE不可用
buttonEast[0]nabled(false);//设置sqrt不可用
buttonEast[1]nabled(false);//设置%不可用
buttonEast[2]nabled(false);//设置1/x不可用
efaultCloseOperation(_ON_CLOSE);
}
private class MyActionListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
String str = ctionCommand();
//数字键
if(git(At(0))){
handalDigit(str);
}
if(start == true){
if(ls("=")){//"="键
handalEqual(str);
return;
}
//Back、C键处理,完善本程序时可在下面的语句中添加对其他按钮的处理
if(ls("Back")||ls("C")){
handalNonOper(str);
}
//'+','-','*','+'/'的处理,完善本程序时可添加sqrt,%等操作符的处理
if(ls("+")||ls("-")||ls("*")||ls("/")){
handalOper(str);
}
}
}
//处理数字,str表示按钮上的文字
private void handalDigit(String str){
char currentChar=At(0);//获得按下的字符;
if(!ls("=")){
if(value2Input == false){
value1+=eOf(currentChar);
if(At(0)=='0'){//第一个数字为0
value1="0";
}
ext(value1);
}
else{
value2+=eOf(currentChar);
if(At(0)=='0'){//第一个数字为0
value2="0";
}
ext(value2);
}
start = true;
lastChar = str;
}
else{//是=
resetAllValue();
value1+=str;
ext(value1);
}
printAllValue();
}
//处理"="键
private void handalEqual(String str){
if(git(At(0)) && th()>0){
handalOper(str);
}
}
//处理运算符,暂时只处理+,-,*,/str表示按钮上的文字
private void handalOper(String str){
if(start == true){
if(th()>0 && th()>0){
result=operationResult(value1,value2,At(0));
ext(result);
if(!ls("除数不能为零")){
value1=result;
value2="";
}
}
else{//说明第一个操作数输入结束,操作符结束,开始输入第二个操作数
value1Input=false;
value2Input=true;
}
oper = str;//记录操作符
lastChar = str;//记录最后一个字符
printAllValue();
}
}
//处理非运算符,但不是数字,如Back,C等键,str表示按钮上的文字
private void handalNonOper(String str){
if(ls("Back")){//Back键处理
if(git(At(0)) == true){//必须是前一个字符是数字
if(value2Input == true){
if(th()>0){
value2 = tring(0,th()-1);
}
ext(value2);
}
else{
if(th()>0){
value1 = tring(0,th()-1);
}
else{
resetAllValue();
}
ext(value1);
}
}
printAllValue();
}
if(ls("C")){//C键处理
resetAllValue();
ext("");
printAllValue();
}
}
//对两个字符串value1和value2表示的整数做运算,oper表示运算符
private String operationResult(String value1,String value2,char oper){
String result;
try{
switch(oper){
case '+':
result = eOf(eInt(value1)+eInt(value2));
break;
case '-':
result = eOf(eInt(value1)eInt(value2));
break;
case '*':
result = eOf(eInt(value1)*eInt(value2));
break;
case '/':
if(ls("0")){
result="除数不能为零";
resetAllValue();//所有值置初值
}
else{
result = eOf(eInt(value1)/eInt(value2));
}
break;
default:
result="error";
}
}
catch(Exception ex){
result = "操作数或结果超过整数范围";
resetAllValue();
}
return result;
}
//重置变量值
private void resetAllValue(){
value1="";//第一个操作数
value2="";//第二个操作数
oper="";//操作符
result="";//运算结果
lastChar="";//相比当前,其上一个按下按钮的字符
value1Input=true;//处于第一个操作数输入情况
value2Input=false;//出于第二个操作数输入情况
start = false;
}
//打印变量值
private void printAllValue(){
tln("| --------Values display----------------");
tln("| value1:" + value1);
tln("| value2:" + value2);
tln("| oper:" + oper);
tln("| result:" + result);
tln("| lastChar:" + lastChar);
tln("| value1Input:" + value1Input);
tln("| value2Input:" + value2Input);
tln("| start:" + start);
tln("| --------------------------------------");
}
}
}
public class Calculator{
public static void main(String[] args) {
new Calculator("计算器");
}
}
程序运行主界面如下:
图8-18 计算器程序运行主界面
该程序实现了加减乘除运算,且充分考虑了各种异常情况,一般不会出现在按下某个按键时出现程序报异常的错误,对于程序中未实现的按钮功能,运行界面中都以灰色按钮的形式出现,读者可在研究本程序的基础上完成这些按键的功能。