主题:计算器的几种实现
/////////////////////////文件夹3
/**
* This class implements a calculator throught a GUI.
*
* @author Tang chaoyong 200532580087
* @version 1.1
* @date 3,16,2007
*/
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
public class Calculator extends JPanel{
private JButton button1,button2,button3,button4,button5,button6,button7,button8,button9,button0,buttonPlus,
buttonMinus,buttonMuti,buttonDiv,buttonPoint,buttonLeftB,buttonRightB,buttonResult,buttonClear,
buttonBack;
private JTextField inputField;
private JTextArea errorArea;
private JTextField expressionArea;
private String result;
private String expression;
private Stack<Double> sOperand;
private Stack<Character> sOperator;
public Calculator(){
result = new String();
expression = new String();
sOperand = new Stack<Double>();
sOperator = new Stack<Character>();
button1 = new JButton("1");
button2 = new JButton("2");
button3 = new JButton("3");
button4 = new JButton("4");
button5 = new JButton("5");
button6 = new JButton("6");
button7 = new JButton("7");
button8 = new JButton("8");
button9 = new JButton("9");
button0 = new JButton("0");
buttonPlus = new JButton("+");
buttonMinus = new JButton("-");
buttonMuti = new JButton("*");
buttonDiv = new JButton("/");
buttonPoint = new JButton(".");
buttonLeftB = new JButton("(");
buttonRightB = new JButton(")");
buttonResult = new JButton("=");
buttonClear = new JButton("C");
buttonBack = new JButton("B");
inputField = new JTextField(6);
inputField.setHorizontalAlignment(JTextField.RIGHT);
inputField.setText("");
inputField.setEditable(false);
expressionArea = new JTextField(20);
expressionArea.setHorizontalAlignment(JTextField.RIGHT);
expressionArea.setText("");
expressionArea.setEditable(false);
errorArea = new JTextArea();
errorArea.setEditable(false);
errorArea.setBackground(Color.GREEN);
JPanel panel1 = new JPanel();
panel1.setBorder(BorderFactory.createTitledBorder("Result"));
panel1.add(expressionArea);panel1.add(inputField);
JPanel panel2 = new JPanel();
panel2.setBorder(BorderFactory.createTitledBorder("Number"));
panel2.setLayout(new GridLayout(4, 3, 6, 6));
panel2.add(button1);panel2.add(button2);panel2.add(button3);
panel2.add(button4);panel2.add(button5);panel2.add(button6);
panel2.add(button7);panel2.add(button8);panel2.add(button9);
panel2.add(button0);
JPanel panel3 =new JPanel();
panel3.setBorder(BorderFactory.createTitledBorder("Operator"));
panel3.setLayout(new GridLayout(4,3,6,6));
panel3.add(buttonPlus);panel3.add(buttonMinus);panel3.add(buttonMuti);
panel3.add(buttonDiv);panel3.add(buttonLeftB);panel3.add(buttonRightB);
panel3.add(buttonClear);panel3.add(buttonBack);panel3.add(buttonResult);
panel3.add(buttonPoint);
JPanel panel4 = new JPanel();
panel4.setBorder(BorderFactory.createTitledBorder("Mistakes"));
panel4.add(errorArea);
button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e1){
result = inputField.getText();
result += 1;expression += 1;
inputField.setText(result);expressionArea.setText(expression);
}
});
button2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e2){
result = inputField.getText();
result += 2;expression += 2;
inputField.setText(result);expressionArea.setText(expression);
}
});
button3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e3){
result = inputField.getText();
result += 3;expression += 3;
inputField.setText(result);expressionArea.setText(expression);
}
});
button4.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e4){
result = inputField.getText();
result += 4;expression += 4;
inputField.setText(result);expressionArea.setText(expression);
}
});
button5.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e5){
result = inputField.getText();
result += 5;expression += 5;
inputField.setText(result);expressionArea.setText(expression);
}
});
button6.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e6){
result = inputField.getText();
result += 6;expression += 6;
inputField.setText(result);expressionArea.setText(expression);
}
});
button7.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e1){
result = inputField.getText();
result += 7;expression += 7;
inputField.setText(result);expressionArea.setText(expression);
}
});
button8.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e8){
result = inputField.getText();
result += 8;expression += 8;
inputField.setText(result);expressionArea.setText(expression);
}
});
button9.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e9){
result = inputField.getText();
result += 9;expression += 9;
inputField.setText(result);expressionArea.setText(expression);
}
});
button0.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e0){
result = inputField.getText();
result += 0;expression += 0;
inputField.setText(result);expressionArea.setText(expression);
}
});
buttonPlus.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e1){
inputField.setText("");
expression += " + ";expressionArea.setText(expression);
}
});
buttonMinus.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e1){
inputField.setText("");
expression += " - ";expressionArea.setText(expression);
}
});
buttonMuti.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e1){
inputField.setText("");
expression += " * ";expressionArea.setText(expression);
}
});
buttonDiv.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e1){
inputField.setText("");
expression += " / ";expressionArea.setText(expression);
}
});
buttonPoint.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e1){
result += ".";expression += ".";
inputField.setText(result);expressionArea.setText(expression);
}
});
buttonLeftB.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e1){
if(!result.equals(""))inputField.setText("");
expression += " ( ";expressionArea.setText(expression);
}
});
buttonRightB.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e1){
if(!result.equals(""))inputField.setText("");
expression += " ) ";expressionArea.setText(expression);
}
});
buttonBack.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e1){
result = inputField.getText();
if(result.length() == 0){
expression = expression.substring(0, expression.length()-1);
expressionArea.setText(expression);
}
else if(result.length() == 1){
result = "";inputField.setText("");
expression = expression.substring(0, expression.length()-1);
expressionArea.setText(expression);
}else{
result = result.substring(0, result.length()-1);
inputField.setText(result);
expression = expression.substring(0, expression.length()-1);
expressionArea.setText(expression);
}
}
});
buttonClear.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e1){
result = "";expression = "";
sOperand.clear();sOperator.clear();sOperator.push('$');
inputField.setText("");expressionArea.setText("");
errorArea.setText("");
}
});
buttonResult.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e1){
String temp;sOperator.push(new Character('#'));
StringTokenizer tokenizer = new StringTokenizer(expression, " ");
while(tokenizer.hasMoreTokens()){
temp = tokenizer.nextToken();
if(temp.equals("+")||temp.equals("-")||temp.equals("*")||temp.equals("/")||temp.equals("(")||temp.equals(")")){
doOperator(temp.charAt(0));
}else{
sOperand.push(Double.parseDouble(temp));
}
}
while((sOperator.peek()).charValue()!='#'){
doOperand(sOperator.pop());
}
inputField.setText("");
double d = sOperand.pop();int i = (new Double(d)).intValue();
if(d == i)temp = (new Integer(i)).toString();
else temp = (new Double(d)).toString();
expressionArea.setText(expression+"="+temp);
expression = temp;
errorArea.setText("");
}
});
this.setLayout(new BorderLayout(6,6));
this.add(BorderLayout.NORTH, panel1);
this.add(BorderLayout.WEST, panel2);
this.add(BorderLayout.CENTER, panel3);
this.add(BorderLayout.SOUTH, panel4);
}
未完!!!!!! 见下面的回复
/**
* This class implements a calculator throught a GUI.
*
* @author Tang chaoyong 200532580087
* @version 1.1
* @date 3,16,2007
*/
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
public class Calculator extends JPanel{
private JButton button1,button2,button3,button4,button5,button6,button7,button8,button9,button0,buttonPlus,
buttonMinus,buttonMuti,buttonDiv,buttonPoint,buttonLeftB,buttonRightB,buttonResult,buttonClear,
buttonBack;
private JTextField inputField;
private JTextArea errorArea;
private JTextField expressionArea;
private String result;
private String expression;
private Stack<Double> sOperand;
private Stack<Character> sOperator;
public Calculator(){
result = new String();
expression = new String();
sOperand = new Stack<Double>();
sOperator = new Stack<Character>();
button1 = new JButton("1");
button2 = new JButton("2");
button3 = new JButton("3");
button4 = new JButton("4");
button5 = new JButton("5");
button6 = new JButton("6");
button7 = new JButton("7");
button8 = new JButton("8");
button9 = new JButton("9");
button0 = new JButton("0");
buttonPlus = new JButton("+");
buttonMinus = new JButton("-");
buttonMuti = new JButton("*");
buttonDiv = new JButton("/");
buttonPoint = new JButton(".");
buttonLeftB = new JButton("(");
buttonRightB = new JButton(")");
buttonResult = new JButton("=");
buttonClear = new JButton("C");
buttonBack = new JButton("B");
inputField = new JTextField(6);
inputField.setHorizontalAlignment(JTextField.RIGHT);
inputField.setText("");
inputField.setEditable(false);
expressionArea = new JTextField(20);
expressionArea.setHorizontalAlignment(JTextField.RIGHT);
expressionArea.setText("");
expressionArea.setEditable(false);
errorArea = new JTextArea();
errorArea.setEditable(false);
errorArea.setBackground(Color.GREEN);
JPanel panel1 = new JPanel();
panel1.setBorder(BorderFactory.createTitledBorder("Result"));
panel1.add(expressionArea);panel1.add(inputField);
JPanel panel2 = new JPanel();
panel2.setBorder(BorderFactory.createTitledBorder("Number"));
panel2.setLayout(new GridLayout(4, 3, 6, 6));
panel2.add(button1);panel2.add(button2);panel2.add(button3);
panel2.add(button4);panel2.add(button5);panel2.add(button6);
panel2.add(button7);panel2.add(button8);panel2.add(button9);
panel2.add(button0);
JPanel panel3 =new JPanel();
panel3.setBorder(BorderFactory.createTitledBorder("Operator"));
panel3.setLayout(new GridLayout(4,3,6,6));
panel3.add(buttonPlus);panel3.add(buttonMinus);panel3.add(buttonMuti);
panel3.add(buttonDiv);panel3.add(buttonLeftB);panel3.add(buttonRightB);
panel3.add(buttonClear);panel3.add(buttonBack);panel3.add(buttonResult);
panel3.add(buttonPoint);
JPanel panel4 = new JPanel();
panel4.setBorder(BorderFactory.createTitledBorder("Mistakes"));
panel4.add(errorArea);
button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e1){
result = inputField.getText();
result += 1;expression += 1;
inputField.setText(result);expressionArea.setText(expression);
}
});
button2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e2){
result = inputField.getText();
result += 2;expression += 2;
inputField.setText(result);expressionArea.setText(expression);
}
});
button3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e3){
result = inputField.getText();
result += 3;expression += 3;
inputField.setText(result);expressionArea.setText(expression);
}
});
button4.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e4){
result = inputField.getText();
result += 4;expression += 4;
inputField.setText(result);expressionArea.setText(expression);
}
});
button5.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e5){
result = inputField.getText();
result += 5;expression += 5;
inputField.setText(result);expressionArea.setText(expression);
}
});
button6.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e6){
result = inputField.getText();
result += 6;expression += 6;
inputField.setText(result);expressionArea.setText(expression);
}
});
button7.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e1){
result = inputField.getText();
result += 7;expression += 7;
inputField.setText(result);expressionArea.setText(expression);
}
});
button8.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e8){
result = inputField.getText();
result += 8;expression += 8;
inputField.setText(result);expressionArea.setText(expression);
}
});
button9.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e9){
result = inputField.getText();
result += 9;expression += 9;
inputField.setText(result);expressionArea.setText(expression);
}
});
button0.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e0){
result = inputField.getText();
result += 0;expression += 0;
inputField.setText(result);expressionArea.setText(expression);
}
});
buttonPlus.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e1){
inputField.setText("");
expression += " + ";expressionArea.setText(expression);
}
});
buttonMinus.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e1){
inputField.setText("");
expression += " - ";expressionArea.setText(expression);
}
});
buttonMuti.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e1){
inputField.setText("");
expression += " * ";expressionArea.setText(expression);
}
});
buttonDiv.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e1){
inputField.setText("");
expression += " / ";expressionArea.setText(expression);
}
});
buttonPoint.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e1){
result += ".";expression += ".";
inputField.setText(result);expressionArea.setText(expression);
}
});
buttonLeftB.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e1){
if(!result.equals(""))inputField.setText("");
expression += " ( ";expressionArea.setText(expression);
}
});
buttonRightB.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e1){
if(!result.equals(""))inputField.setText("");
expression += " ) ";expressionArea.setText(expression);
}
});
buttonBack.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e1){
result = inputField.getText();
if(result.length() == 0){
expression = expression.substring(0, expression.length()-1);
expressionArea.setText(expression);
}
else if(result.length() == 1){
result = "";inputField.setText("");
expression = expression.substring(0, expression.length()-1);
expressionArea.setText(expression);
}else{
result = result.substring(0, result.length()-1);
inputField.setText(result);
expression = expression.substring(0, expression.length()-1);
expressionArea.setText(expression);
}
}
});
buttonClear.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e1){
result = "";expression = "";
sOperand.clear();sOperator.clear();sOperator.push('$');
inputField.setText("");expressionArea.setText("");
errorArea.setText("");
}
});
buttonResult.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e1){
String temp;sOperator.push(new Character('#'));
StringTokenizer tokenizer = new StringTokenizer(expression, " ");
while(tokenizer.hasMoreTokens()){
temp = tokenizer.nextToken();
if(temp.equals("+")||temp.equals("-")||temp.equals("*")||temp.equals("/")||temp.equals("(")||temp.equals(")")){
doOperator(temp.charAt(0));
}else{
sOperand.push(Double.parseDouble(temp));
}
}
while((sOperator.peek()).charValue()!='#'){
doOperand(sOperator.pop());
}
inputField.setText("");
double d = sOperand.pop();int i = (new Double(d)).intValue();
if(d == i)temp = (new Integer(i)).toString();
else temp = (new Double(d)).toString();
expressionArea.setText(expression+"="+temp);
expression = temp;
errorArea.setText("");
}
});
this.setLayout(new BorderLayout(6,6));
this.add(BorderLayout.NORTH, panel1);
this.add(BorderLayout.WEST, panel2);
this.add(BorderLayout.CENTER, panel3);
this.add(BorderLayout.SOUTH, panel4);
}
未完!!!!!! 见下面的回复