一.界面显示
二.代码
2.1.CalculatorDemo.java
package test16_Calculator;
import java. awt. event. *;
import javax. swing. *;
import java. awt. *;
public class CalculatorDemo {
public static void main ( String[ ] args) {
Calculator calculator = new Calculator ( ) ;
}
}
class Calculator extends JFrame implements ActionListener {
int buttonSize = 20 ;
private boolean isEqual;
String [ ] buttonNameArray = {
"7" , "8" , "9" , "/" ,
"4" , "5" , "6" , "*" ,
"1" , "2" , "3" , "-" ,
"0" , "." , "C" , "+" ,
"圆的面积" , "test" , "test" , "=" } ;
Operation op ;
String operator = "" ;
private JButton[ ] buttonArray = new JButton [ buttonSize] ;
private JTextField displayField;
private JPanel panel;
public Calculator ( ) {
isEqual = true ;
for ( int i= 0 ; i< buttonArray. length; i++ ) {
buttonArray[ i] = new JButton ( buttonNameArray[ i] ) ;
}
displayField = new JTextField ( ) ;
panel = new JPanel ( ) ;
Font font = new Font ( "宋体" , Font. BOLD, 16 ) ;
Font font1 = new Font ( "宋体" , Font. BOLD, 20 ) ;
for ( int i= 0 ; i< buttonArray. length; i++ ) {
buttonArray[ i] . setFont ( font) ;
}
displayField. setFont ( font1) ;
displayField. setHorizontalAlignment ( JTextField. RIGHT) ;
displayField. setText ( "0" ) ;
displayField. setForeground ( Color. BLACK) ;
displayField. setBorder ( BorderFactory. createCompoundBorder (
BorderFactory. createLineBorder ( Color. black) ,
BorderFactory. createEmptyBorder ( 5 , 5 , 5 , 5 ) ) ) ;
panel. setBorder ( BorderFactory. createCompoundBorder ( BorderFactory. createLineBorder ( Color. black) ,
BorderFactory. createEmptyBorder ( 10 , 10 , 10 , 10 ) ) ) ;
panel. setLayout ( new GridLayout ( 5 , 4 , 10 , 10 ) ) ;
this . add ( displayField, BorderLayout. NORTH) ;
for ( int i= 0 ; i< buttonArray. length; i++ ) {
panel. add ( buttonArray[ i] ) ;
}
this . add ( panel, BorderLayout. CENTER) ;
for ( int i= 0 ; i< buttonArray. length; i++ ) {
buttonArray[ i] . addActionListener ( this ) ;
}
this . setTitle ( "计算器" ) ;
this . setSize ( 500 , 350 ) ;
this . setDefaultCloseOperation ( JFrame. EXIT_ON_CLOSE) ;
int width = Toolkit. getDefaultToolkit ( ) . getScreenSize ( ) . width;
int height = Toolkit. getDefaultToolkit ( ) . getScreenSize ( ) . height;
this . setLocation ( width / 2 - 200 , height / 2 - 150 ) ;
this . setVisible ( true ) ;
}
private int judgeOperation ( String s) {
int operatorNum = 0 ;
if ( "+-*/" . indexOf ( s) != - 1 ) {
operatorNum = 2 ;
}
else {
if ( "圆的面积" . indexOf ( s) != - 1 ) {
operatorNum = 1 ;
}
else {
operatorNum = 0 ;
}
}
return operatorNum;
}
@Override
public void actionPerformed ( ActionEvent e) {
String s = e. getActionCommand ( ) ;
if ( "1234567890.=" . indexOf ( s) == - 1 ) {
operator = s;
}
if ( "C" . indexOf ( s) == - 1 ) {
operator = "" ;
}
if ( judgeOperation ( s) == 0 && ! s. equals ( "=" ) ) {
if ( isEqual) {
displayField. setText ( s) ;
}
else {
displayField. setText ( displayField. getText ( ) + s) ;
}
isEqual = false ;
}
else {
if ( judgeOperation ( s) != 0 ) {
op = OperationFactory. createOperate ( s) ;
op. setNum1 ( Double. parseDouble ( displayField. getText ( ) ) ) ;
displayField. setText ( "" ) ;
}
else {
if ( judgeOperation ( operator) == 2 && s. equals ( "=" ) ) {
op. setNum2 ( Double. parseDouble ( displayField. getText ( ) ) ) ;
if ( op. getNum2 ( ) == 0 && operator. equals ( "/" ) ) {
JOptionPane. showMessageDialog ( null,
"除数不能为0" , "提示" , 3 ) ;
displayField. setText ( "" ) ;
}
else {
double result = op. getResult ( ) ;
displayField. setText ( Double. toString ( result) ) ;
isEqual = true ;
}
}
else
if ( s. equals ( "=" ) && judgeOperation ( operator) == 1 ) {
double result = op. getResult ( ) ;
displayField. setText ( Double. toString ( result) ) ;
isEqual = true ;
}
}
}
}
}
2.2.OperationFactory.java
package test16_Calculator;
public class OperationFactory {
public static Operation createOperate ( String operate) {
Operation op = null;
switch ( operate) {
case "+" : {
op = new OperationAdd ( ) ;
break ;
}
case "-" : {
op = new OperationSub ( ) ;
break ;
}
case "*" : {
op = new OperationMul ( ) ;
break ;
}
case "/" : {
op = new OperationDiv ( ) ;
break ;
}
case "圆的面积" : {
op = new OperationCircle ( ) ;
break ;
}
default : break ;
}
return op;
}
}
2.3.Operation.java
package test16_Calculator;
public class Operation {
private double num1;
private double num2;
public double getResult ( ) {
return 0 ;
}
public double getNum1 ( ) {
return num1;
}
public void setNum1 ( double num1) {
this . num1 = num1;
}
public double getNum2 ( ) {
return num2;
}
public void setNum2 ( double num2) {
this . num2 = num2;
}
}
class OperationAdd extends Operation {
public double getResult ( ) {
return getNum1 ( ) + getNum2 ( ) ;
}
}
class OperationSub extends Operation {
public double getResult ( ) {
return getNum1 ( ) - getNum2 ( ) ;
}
}
class OperationMul extends Operation {
public double getResult ( ) {
return getNum1 ( ) * getNum2 ( ) ;
}
}
class OperationDiv extends Operation {
public double getResult ( ) {
return getNum1 ( ) / getNum2 ( ) ;
}
}
class OperationCircle extends Operation {
public double getResult ( ) {
return getNum1 ( ) * getNum1 ( ) * Math. PI;
}
}