java計(jì)算器綜合實(shí)例學(xué)習(xí)教程
下面是百分網(wǎng)小編帶來(lái)的Java計(jì)算器綜合實(shí)例學(xué)習(xí)教程,歡迎學(xué)習(xí)!

本節(jié)給出了一個(gè)計(jì)算器的綜合實(shí)例,該實(shí)例主要利用了本章及上一章所涉及的SWING組件及組件事件的處理來(lái)實(shí)現(xiàn)的,在實(shí)現(xiàn)的過程中還使用到了異常處理的知識(shí)。
[例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[];/pic/'.','+/-'
JButton buttonNorth[];/pic/p>
JButton buttonEast[];/pic/x,=
JTextField fieldResult = new JTextField();
/pic/p>
static private String value1="";/pic/p>
static private String value2="";/pic/p>
static private String oper="";/pic/p>
static private String result="";/pic/p>
static private String lastChar="";/pic/p>
static private boolean value1Input=true;/pic/p>
static private boolean value2Input=false;/pic/p>
static private boolean start = false;/pic/p>
public Calculator(String title){
super(title);
Container contentPane = this.getContentPane();
fieldResult.setHorizontalAlignment(JTextField.RIGHT);/pic/p>
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);
/pic/p>
MyActionListener listener = new MyActionListener();
for(int i=0;i
buttonMain[i].addActionListener(listener);
}
buttonNorth[0].addActionListener(listener);/pic/p>
buttonEast[3].addActionListener(listener);/pic/p>
buttonNorth[2].addActionListener(listener);/pic/p>
buttonMain[13].setEnabled(false);/pic/-不可用
buttonMain[14].setEnabled(false);/pic/p>
buttonNorth[1].setEnabled(false);/pic/p>
buttonEast[0].setEnabled(false);/pic/p>
buttonEast[1].setEnabled(false);/pic/p>
buttonEast[2].setEnabled(false);/pic/x不可用
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private class MyActionListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
String str = e.getActionCommand();
/pic/p>
if(Character.isDigit(str.charAt(0))){
handalDigit(str);
}
if(start == true){
if(str.equals("=")){/pic/p>
handalEqual(str);
return;
}
/pic/p>
if(str.equals("Back")||str.equals("C")){
handalNonOper(str);
}
/pic/'的處理,完善本程序時(shí)可添加sqrt,%等操作符的處理
if(str.equals("+")||str.equals("-")||str.equals("*")||str.equals("/")){
handalOper(str);
}
}
}
/pic/p>
private void handalDigit(String str){
char currentChar=str.charAt(0);/pic/p>
if(!lastChar.equals("=")){
if(value2Input == false){
value1+=String.valueOf(currentChar);
if(value1.charAt(0)=='0'){/pic/p>
value1="0";
}
fieldResult.setText(value1);
}
else{
value2+=String.valueOf(currentChar);
if(value2.charAt(0)=='0'){/pic/p>
value2="0";
}
fieldResult.setText(value2);
}
start = true;
lastChar = str;
}
else{/pic/p>
resetAllValue();
value1+=str;
fieldResult.setText(value1);
}
printAllValue();
}
/pic/p>
private void handalEqual(String str){
if(Character.isDigit(lastChar.charAt(0)) && value2.length()>0){
handalOper(str);
}
}
/pic/str表示按鈕上的文字
private void handalOper(String str){
if(start == true){
if(value1.length()>0 && value2.length()>0){
result=operationResult(value1,value2,oper.charAt(0));
fieldResult.setText(result);
if(!result.equals("除數(shù)不能為零")){
value1=result;
value2="";
}
}
else{/pic/p>
value1Input=false;
value2Input=true;
}
oper = str;/pic/p>
lastChar = str;/pic/p>
printAllValue();
}
}
/pic/p>
private void handalNonOper(String str){
if(str.equals("Back")){/pic/p>
if(Character.isDigit(lastChar.charAt(0)) == true){/pic/p>
if(value2Input == true){
if(value2.length()>0){
value2 = value2.substring(0,value2.length()-1);
}
fieldResult.setText(value2);
}
else{
if(value1.length()>0){
value1 = value1.substring(0,value1.length()-1);
}
else{
resetAllValue();
}
fieldResult.setText(value1);
}
}
printAllValue();
}
if(str.equals("C")){/pic/p>
resetAllValue();
fieldResult.setText("");
printAllValue();
}
}
/pic/p>
private String operationResult(String value1,String value2,char oper){
String result;
try{
switch(oper){
case '+':
result = String.valueOf(Integer.parseInt(value1)+Integer.parseInt(value2));
break;
case '-':
result = String.valueOf(Integer.parseInt(value1)-Integer.parseInt(value2));
break;
case '*':
result = String.valueOf(Integer.parseInt(value1)*Integer.parseInt(value2));
break;
case '/':
if(value2.equals("0")){
result="除數(shù)不能為零";
resetAllValue();/pic/p>
}
else{
result = String.valueOf(Integer.parseInt(value1)/Integer.parseInt(value2));
}
break;
default:
result="error";
}
}
catch(Exception ex){
result = "操作數(shù)或結(jié)果超過整數(shù)范圍";
resetAllValue();
}
return result;
}
/pic/p>
private void resetAllValue(){
value1="";/pic/p>
value2="";/pic/p>
oper="";/pic/p>
result="";/pic/p>
lastChar="";/pic/p>
value1Input=true;/pic/p>
value2Input=false;/pic/p>
start = false;
}
/pic/p>
private void printAllValue(){
System.out.println("| --------Values display----------------");
System.out.println("| value1:" + value1);
System.out.println("| value2:" + value2);
System.out.println("| oper:" + oper);
System.out.println("| result:" + result);
System.out.println("| lastChar:" + lastChar);
System.out.println("| value1Input:" + value1Input);
System.out.println("| value2Input:" + value2Input);
System.out.println("| start:" + start);
System.out.println("| --------------------------------------");
}
}
}
public class Calculator{
public static void main(String[] args) {
new Calculator("計(jì)算器");
}
}
程序運(yùn)行主界面如下:
圖8-18 計(jì)算器程序運(yùn)行主界面
該程序?qū)崿F(xiàn)了加減乘除運(yùn)算,且充分考慮了各種異常情況,一般不會(huì)出現(xiàn)在按下某個(gè)按鍵時(shí)出現(xiàn)程序報(bào)異常的錯(cuò)誤,對(duì)于程序中未實(shí)現(xiàn)的按鈕功能,運(yùn)行界面中都以灰色按鈕的形式出現(xiàn),讀者可在研究本程序的基礎(chǔ)上完成這些按鍵的功能。
【java計(jì)算器綜合實(shí)例學(xué)習(xí)教程】相關(guān)文章:
Java的特點(diǎn)學(xué)習(xí)教程01-22
Java中的== 和equals()方法詳解與實(shí)例教程11-09
Java數(shù)組的基本學(xué)習(xí)教程03-01
Java數(shù)組的基礎(chǔ)學(xué)習(xí)教程02-24
java編譯計(jì)算器09-26
java教程之Java編程基礎(chǔ)12-06