[code=Jav] public void init( ) {
String ButtonName[ ] = { " ", "C", "M+", "M-", "MR", "7", "8", "9", "/",
"4", "5", "6", "*",
"1", "2", "3", "-",
"0", ".", "=", "+" };
Button CalcButton[ ] = new Button[21];
ClickSound = getAudioClip(getCodeBase( ), "sound/click.au");
setLayout(new BorderLayout( ));
Panel DisplayPanel = new Panel( );
DisplayLabel = new Label("", Label.RIGHT);
DisplayLabel.setBackground(Color.black);
DisplayLabel.setForeground(Color.green);
DisplayPanel.add(DisplayLabel);
Panel ButtonPanel = new Panel( );
ButtonPanel.setLayout(new GridLayout(5, 4));
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 4; j++) {
int p = (i - 1) * 4 + j;
CalcButton[p] = new Button(ButtonName[p]);//这个地方可否具体说明一下,CalcButton[p]是原来声明的CalcButton[ ]这个数组吗?还是新声明的数组,数组的后面用new演算子是什么意思?
ButtonPanel.add(CalcButton[p]);
CalcButton[p].addActionListener(this);
}
}
add("North", DisplayPanel);
add("Center", ButtonPanel);
InitialPro( );
}[/code]
请求高人解答疑问。
String ButtonName[ ] = { " ", "C", "M+", "M-", "MR", "7", "8", "9", "/",
"4", "5", "6", "*",
"1", "2", "3", "-",
"0", ".", "=", "+" };
Button CalcButton[ ] = new Button[21];
ClickSound = getAudioClip(getCodeBase( ), "sound/click.au");
setLayout(new BorderLayout( ));
Panel DisplayPanel = new Panel( );
DisplayLabel = new Label("", Label.RIGHT);
DisplayLabel.setBackground(Color.black);
DisplayLabel.setForeground(Color.green);
DisplayPanel.add(DisplayLabel);
Panel ButtonPanel = new Panel( );
ButtonPanel.setLayout(new GridLayout(5, 4));
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 4; j++) {
int p = (i - 1) * 4 + j;
CalcButton[p] = new Button(ButtonName[p]);//这个地方可否具体说明一下,CalcButton[p]是原来声明的CalcButton[ ]这个数组吗?还是新声明的数组,数组的后面用new演算子是什么意思?
ButtonPanel.add(CalcButton[p]);
CalcButton[p].addActionListener(this);
}
}
add("North", DisplayPanel);
add("Center", ButtonPanel);
InitialPro( );
}[/code]
请求高人解答疑问。
CalcButton[p]指的是数组的第p个元素,并不是整个数组。
因为CalcButton变量已经声明过了,不需要再声明了。假想,如果将[p]下标访问改成成员字段,那么这个语句就类似于
CalcButton.member_p = new Button(...
在前面加个类型声明,是不是多余了?这样是不是好理解一些?
这行程序是什么意思呢,有什么作用呢?
在CalcButton数组中p位置上有个Button,将当前对象作为这个Button的事件侦听器加上去。
作用是从此这个Button上按鼠标就有用了。