只有源码,但几乎每行都有注释
Operation.java
public class Operation {
private double numA;
private double numB;
public double getResult ( ) {
return 0 ;
}
public double getNumA ( ) {
return numA;
}
public double getNumB ( ) {
return numB;
}
public void setNumA ( double numA) {
this . numA = numA;
}
public void setNumB ( double numB) {
this . numB = numB;
}
}
class OperationAdd extends Operation {
@Override
public double getResult ( ) {
return getNumA ( ) + getNumB ( ) ;
}
}
class OperationSub extends Operation {
@Override
public double getResult ( ) {
return getNumA ( ) - getNumB ( ) ;
}
}
class OperationMul extends Operation {
@Override
public double getResult ( ) {
return getNumA ( ) * getNumB ( ) ;
}
}
class OperationDiv extends Operation {
@Override
public double getResult ( ) {
return getNumA ( ) / getNumB ( ) ;
}
}
class OperationCircle extends Operation {
@Override
public double getResult ( ) {
return Math. PI * getNumA ( ) * getNumA ( ) ;
}
}
OperationFactory.java
public class OperationFactory {
public static Operation createOperate ( String operate) {
Operation operation = null ;
switch ( operate) {
case "+" :
operation = new OperationAdd ( ) ;
break ;
case "-" :
operation = new OperationSub ( ) ;
break ;
case "*" :
operation = new OperationMul ( ) ;
break ;
case "/" :
operation = new OperationDiv ( ) ;
break ;
case "圆的面积" :
operation = new OperationCircle ( ) ;
break ;
}
return operation;
}
}
CalculatorDemo.java //最重要的
import javax. swing. * ;
import java. awt. * ;
import java. awt. event. ActionEvent;
import java. awt. event. ActionListener;
import java. io. IOException;
class Calculator extends JFrame implements ActionListener {
int size = 20 ;
private boolean isEqual;
String [ ] str = { "7" , "8" , "9" , "/" ,
"4" , "5" , "6" , "*" ,
"1" , "2" , "3" , "-" ,
"圆的面积" , "0" , "." , "+" ,
"test" , "test" , "test" , "=" } ;
Operation operation = null ;
String temp = "" ;
private JButton [ ] jButtons = new JButton [ size] ;
private JTextField jTextField;
JPanel jPanel;
public Calculator ( ) {
isEqual = true ;
for ( int i= 0 ; i< jButtons. length; i++ ) {
jButtons[ i] = new JButton ( str[ i] ) ;
}
jTextField = new JTextField ( ) ;
jPanel = new JPanel ( ) ;
Font font = new Font ( "宋体" , Font. BOLD , 16 ) ;
Font font1 = new Font ( "宋体" , Font. BOLD , 20 ) ;
for ( int i= 0 ; i< jButtons. length; i++ ) {
jButtons[ i] . setFont ( font) ;
}
jTextField. setFont ( font1) ;
jTextField. setHorizontalAlignment ( JTextField. RIGHT ) ;
jTextField. setText ( "" ) ;
jTextField. setForeground ( Color. BLACK ) ;
jTextField. setBorder ( BorderFactory. createCompoundBorder (
BorderFactory. createLineBorder ( Color. black) ,
BorderFactory. createEmptyBorder ( 5 , 5 , 5 , 5 ) ) ) ;
jPanel. setBorder ( BorderFactory. createCompoundBorder (
BorderFactory. createLineBorder ( Color. BLACK ) ,
BorderFactory. createEmptyBorder ( 10 , 10 , 10 , 10 ) ) ) ;
jPanel. setLayout ( new GridLayout ( 5 , 4 , 10 , 10 ) ) ;
this . add ( jTextField, BorderLayout. NORTH ) ;
for ( int i= 0 ; i< jButtons. length; i++ ) {
jPanel. add ( jButtons[ i] ) ;
}
this . add ( jPanel, BorderLayout. CENTER ) ;
for ( int i= 0 ; i< jButtons. length; i++ ) {
jButtons[ i] . addActionListener ( this ) ;
}
setTitle ( "nt计算器" ) ;
setSize ( 500 , 350 ) ;
setLocation ( 400 , 200 ) ;
this . setDefaultCloseOperation ( JFrame. EXIT_ON_CLOSE ) ;
this . setVisible ( true ) ;
}
private int judgeOperation ( String s) {
int flag = 0 ;
if ( "+-*/" . indexOf ( s) != - 1 )
flag = 2 ;
else
if ( "圆的面积test" . indexOf ( s) != - 1 )
flag = 1 ;
else
flag = 0 ;
return flag;
}
@Override
public void actionPerformed ( ActionEvent actionEvent) {
String s = actionEvent. getActionCommand ( ) ;
if ( "1234567890.=" . indexOf ( s) == - 1 )
temp = s;
if ( judgeOperation ( s) == 0 && ! s. equals ( "=" ) ) {
if ( isEqual)
jTextField. setText ( s) ;
else
jTextField. setText ( jTextField. getText ( ) + s) ;
isEqual = false ;
}
else
if ( judgeOperation ( s) != 0 ) {
operation = OperationFactory. createOperate ( s) ;
operation. setNumA ( Double. parseDouble ( jTextField. getText ( ) ) ) ;
jTextField. setText ( "" ) ;
}
else
if ( judgeOperation ( temp) == 2 && s. equals ( "=" ) ) {
operation. setNumB ( Double. parseDouble ( jTextField. getText ( ) ) ) ;
if ( operation. getNumB ( ) == 0 && temp. equals ( "/" ) ) {
JOptionPane. showMessageDialog ( null ,
"除数不能为0!!" , "提示" , 3 ) ;
jTextField. setText ( "" ) ;
}
else {
double result = operation. getResult ( ) ;
jTextField. setText ( Double. toString ( result) ) ;
isEqual = true ;
}
}
else
if ( s. equals ( "=" ) && judgeOperation ( temp) == 1 ) {
double result = operation. getResult ( ) ;
jTextField. setText ( Double. toString ( result) ) ;
isEqual= true ;
}
}
}
public class CalculatorDemo {
public static void main ( String args[ ] ) throws IOException{
new Calculator ( ) ;
}
}