import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class ch5_e5_13c extends Applet implements ActionListener
{
final int NUMBER = 4;
ComplexInputPanel inputComplexPnl[][];
ComplexOutputPanel outputComplexPnl[];
Button caculateBtn[];
final String COMMAND_NAME[] = {"ADD","MINUS","MULTIPLE","DIVIDE"};
public ch5_e5_13c()
{
super();
//初始化
inputComplexPnl = new ComplexInputPanel[NUMBER][2];
outputComplexPnl = new ComplexOutputPanel[NUMBER];
caculateBtn = new Button[NUMBER];
for(int i=0; i<NUMBER; i++)
{
for(int j=0; j<2; j++)
inputComplexPnl[i][j] = new ComplexInputPanel();
outputComplexPnl[i] = new ComplexOutputPanel();
caculateBtn[i] = new Button("=");
caculateBtn[i].addActionListener(this);
caculateBtn[i].setActionCommand(COMMAND_NAME[i]);
}
//加入各图形界面成分
for(int i=0; i<NUMBER; i++)
{
//第一个操作数的输入界面成分
add(inputComplexPnl[i][0]);
//操作符
switch(i)
{
case 0: add(new Label("+"));
break;
case 1: add(new Label("-"));
break;
case 2: add(new Label("*"));
break;
case 3: add(new Label("/"));
break;
}
//第二个操作数的输入界面成分
add(inputComplexPnl[i][1]);
//启动运算的按钮
add(caculateBtn[i]);
//运算结果的输出界面成分
add(outputComplexPnl[i]);
}//for
}
public void init()
{
for(int i=0; i<NUMBER; i++)
{
for(int j=0; j<2; j++)
inputComplexPnl[i][j].resetFaces();
outputComplexPnl[i].resetFaces();
}
}//init
public void actionPerformed(ActionEvent e)
{
try
{
if(e.getActionCommand().equals(COMMAND_NAME[0]))
{
inputComplexPnl[0][0].getComplexNumber();
outputComplexPnl[0].setComplexNumber(
inputComplexPnl[0][0].complexAdd(
inputComplexPnl[0][1].getComplexNumber()));
}
else if(e.getActionCommand().equals(COMMAND_NAME[1]))
{
inputComplexPnl[1][0].getComplexNumber();
outputComplexPnl[1].setComplexNumber(
inputComplexPnl[1][0].complexMinus(
inputComplexPnl[1][1].getComplexNumber()));
}
else if(e.getActionCommand().equals(COMMAND_NAME[2]))
{
inputComplexPnl[2][0].getComplexNumber();
outputComplexPnl[2].setComplexNumber(
inputComplexPnl[2][0].complexMulti(
inputComplexPnl[2][1].getComplexNumber()));
}
else if(e.getActionCommand().equals(COMMAND_NAME[3]))
{
inputComplexPnl[3][0].getComplexNumber();
outputComplexPnl[3].setComplexNumber(
inputComplexPnl[3][0].complexDivide(
inputComplexPnl[3][1].getComplexNumber()));
}
else
{
showStatus("未定义这种事件的处理操作" + e.toString());
return;
}
}
catch(NumberFormatException nfe)
{
showStatus("数据格式错误,请重新输入。");
for(int i=0; i<NUMBER; i++)
{
for(int j=0; j<2; j++)
inputComplexPnl[i][j].resetFaces();
outputComplexPnl[i].resetFaces();
}
}
}
}
interface ComplexCaculationsInterface
{
public ComplexNumber complexAdd(ComplexNumber c);
public ComplexNumber complexAdd(double d);
public ComplexNumber complexMinus(ComplexNumber c);
public ComplexNumber complexMinus(double d);
public ComplexNumber complexMulti(ComplexNumber c);
public ComplexNumber complexMulti(double d);
public ComplexNumber complexDivide(double d);
public ComplexNumber complexDivide(ComplexNumber c);
}
abstract class ComplexPanel extends Panel implements ComplexCaculationsInterface
{
ComplexNumber innerComplex;
Label prefixLabel,midLabel,suffixLabel;
ComplexPanel()
{
super();
innerComplex = new ComplexNumber();
prefixLabel = new Label("(");
midLabel = new Label("+");
suffixLabel = new Label("i)");
createAddFaces();
}
abstract void createAddFaces();
abstract ComplexNumber getComplexNumber()
throws NumberFormatException;
abstract void setComplexNumber(ComplexNumber c);
abstract void resetFaces();
public ComplexNumber complexAdd(ComplexNumber c)
{
return innerComplex.complexAdd(c);
}
public ComplexNumber complexAdd(double d)
{
return innerComplex.complexAdd(d);
}
public ComplexNumber complexMinus(ComplexNumber c)
{
return innerComplex.complexMinus(c);
}
public ComplexNumber complexMinus(double d)
{
return innerComplex.complexMinus(d);
}
public ComplexNumber complexMulti(ComplexNumber c)
{
return innerComplex.complexMulti(c);
}
public ComplexNumber complexMulti(double d)
{
return innerComplex.complexMulti(d);
}
public ComplexNumber complexDivide(double d)
{
return innerComplex.complexDivide(d);
}
public ComplexNumber complexDivide(ComplexNumber c)
{
return innerComplex.complexDivide(c);
}
}
class ComplexInputPanel extends ComplexPanel
{
private TextField realTfd,imaginaryTfd;
void createAddFaces()
{
realTfd = new TextField(3);
imaginaryTfd = new TextField(3);
add(prefixLabel);
add(realTfd);
add(midLabel);
add(imaginaryTfd);
add(suffixLabel);
}
ComplexNumber getComplexNumber() throws NumberFormatException
{
innerComplex = new ComplexNumber(
Double.parseDouble(realTfd.getText()),
Double.parseDouble(
(imaginaryTfd.getText().equals("")) ? "0" :
imaginaryTfd.getText() )
);
return innerComplex;
}
void setComplexNumber(ComplexNumber c)
{
double r,i;
r = c.getRealPart();
i = c.getImaginaryPart();
innerComplex.setRealPart(r);
innerComplex.setImaginaryPart(i);
realTfd.setText(Double.toString(r));
imaginaryTfd.setText(Double.toString(i));
}
void resetFaces()
{
realTfd.setText("");
imaginaryTfd.setText("");
}
}
class ComplexOutputPanel extends ComplexPanel
{
private Label realLbl,imaginaryLbl;
void createAddFaces()
{
realLbl = new Label(" ");
imaginaryLbl = new Label(" ");
add(prefixLabel);
add(realLbl);
add(midLabel);
add(imaginaryLbl);
add(suffixLabel);
}
ComplexNumber getComplexNumber() throws NumberFormatException
{
innerComplex = new ComplexNumber(
Double.parseDouble(realLbl.getText()),
Double.parseDouble(
(imaginaryLbl.getText().equals("")) ? "0" :
imaginaryLbl.getText() )
);
return innerC