下面是本站小編帶來的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 計算器程式執行主介面
該程式實現了加減乘除運算,且充分考慮了各種異常情況,一般不會出現在按下某個按鍵時出現程式報異常的錯誤,對於程式中未實現的按鈕功能,執行介面中都以灰色按鈕的形式出現,讀者可在研究本程式的基礎上完成這些按鍵的功能。