ButtonPanel.add( numbutton[4] ).setForeground(Color.blue); numbutton[4].setBackground(Color.LIGHT_GRAY);
ButtonPanel.add( numbutton[5] ).setForeground(Color.blue); numbutton[5].setBackground(Color.LIGHT_GRAY);
ButtonPanel.add( numbutton[6] ).setForeground(Color.blue); numbutton[6].setBackground(Color.LIGHT_GRAY);
ButtonPanel.add( btnSubtract ).setForeground(Color.red); ButtonPanel.add( btnBackspace ).setForeground(Color.red);
ButtonPanel.add( numbutton[1] ).setForeground(Color.blue); numbutton[1].setBackground(Color.LIGHT_GRAY);
ButtonPanel.add( numbutton[2] ).setForeground(Color.blue); numbutton[2].setBackground(Color.LIGHT_GRAY);
ButtonPanel.add( numbutton[3] ).setForeground(Color.blue); numbutton[3].setBackground(Color.LIGHT_GRAY);
ButtonPanel.add( btnMultiply ).setForeground(Color.red); ButtonPanel.add( btnDivide1 ).setForeground(Color.red);
ButtonPanel.add( numbutton[0] ).setForeground(Color.blue); numbutton[0].setBackground(Color.LIGHT_GRAY); ButtonPanel.add( btnDot ).setForeground(Color.blue); btnDot.setBackground(Color.LIGHT_GRAY);
ButtonPanel.add( btnPosMinus ).setForeground(Color.blue); btnPosMinus.setBackground(Color.LIGHT_GRAY);
ButtonPanel.add( btnDivide ).setForeground(Color.red); ButtonPanel.add( btnEqual ).setForeground(Color.red);
//为每个按钮设置监听
for(i=0;i<10;i++) { numbutton[i].addActionListener(comControler); }
btnDot.addActionListener(comControler);
btnPosMinus.addActionListener(comControler); btnBackspace.addActionListener(comControler); btnDivide1.addActionListener(comControler); btnEqual.addActionListener(comControler); btnClear.addActionListener(comControler); btnAdd.addActionListener(comControler); btnSubtract.addActionListener(comControler); btnMultiply.addActionListener(comControler); btnDivide.addActionListener(comControler); btnX2.addActionListener(comControler);
btnX3.addActionListener(comControler); btnXY.addActionListener(comControler); btnSin.addActionListener(comControler); btnCos.addActionListener(comControler); btnTan.addActionListener(comControler); btnSqrt.addActionListener(comControler); btnN.addActionListener(comControler); btnLeft.addActionListener(comControler); btnRight.addActionListener(comControler);
add( EditTextPanel, BorderLayout.NORTH ); add( ButtonPanel, BorderLayout.CENTER );
} }
/////////////////////////////////////////////////////////////////////////////// package Frame;
import java.awt.event.ActionEvent; import java.awt.event.ActionListener;
import javax.swing.JTextField;
public class ComputeControler implements ActionListener{ JTextField textField; //integer1 ,integer2 String op1, op2, operator; String errMsg = \ //the state for now ,begin state = 0 int state = 0; ComputeControler( JTextField tf) { textField = tf; } @Override
public void actionPerformed( ActionEvent e ) {
String s;
s = e.getActionCommand();
switch( state ) {
case 0:
inputState0(s); break; case 1:
inputState1(s); break; case 2:
inputState2(s); break; case 3:
inputState3(s); break; case 4:
inputState4(s); break; case 5:
inputState5(s); break; default:
System.out.println( \ System.exit(1); } }
private boolean isDigit( String s ) {
boolean b;
b = s.equals(\ ||s.equals(\ return b; }
private int fN( float fop1 ) {
int ruslt=0;
if ( fop1 == 0 || fop1 == 1 ) {
ruslt = 1;
} else {
ruslt = (int) (fop1 * fN(fop1-1) ); }
return ruslt; }
private boolean isOperator(String s) {
return s.equals(\ ||s.equals(\ ||s.equals(\
||s.equals(\ }
//state 0 start
private void inputState0( String s ) {
if ( isDigit(s)||s.equals(\ {
state = 2;
textField.setText(\ inputState2(s); }
if ( isOperator(s) ) {
op1 = \ operator = s; state = 4; }
if ( s.equals(\ {
textField.setText(errMsg); state = 1; } }
//state 1 error
private void inputState1( String s ) {
if ( isDigit(s)||s.equals(\ {
textField.setText(\
state = 0;
inputState0(s); } else {
state = 0;
textField.setText(\ } }
//state 2 op1 reading,op1 is being input private void inputState2( String s ) {
if ( isDigit(s) ) {
String text = textField.getText(); if ( text.equals(\ text = s; else
text = text + s; textField.setText(text); }
if ( s.equals(\ {
String text = textField.getText(); if ( !text.contains(\ {
text = text + s;
textField.setText(text); } }
if ( s.equals(\ {
String text = textField.getText(); if ( text.charAt(0) == '-' ) text = text.substring(1); else
text = \ textField.setText(text); }
if ( isOperator(s)||s.equals(\ {
state = 3;