[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]
请求高人解答疑问。

解决方案 »

  1.   

     CalcButton[p]= new Button(ButtonName[p]);  CalcButton中的第p元素等于 一个新new出来的button 并且用buttonname数组中的第p个元素来命名
      

  2.   

    1楼正解。
    CalcButton[p]指的是数组的第p个元素,并不是整个数组。
      

  3.   

    new不只是用在声明类,和建立数组吗?那这个new是那个方面呢?
      

  4.   

    是你说的前面那个方面,创建Button类的一个对象
      

  5.   

    创建对象的话那为何前面不加上Button变成Button CalcButton[p]= new Button(ButtonName[p]);呢?
      

  6.   

    问得很好。
    因为CalcButton变量已经声明过了,不需要再声明了。假想,如果将[p]下标访问改成成员字段,那么这个语句就类似于
    CalcButton.member_p = new Button(...
    在前面加个类型声明,是不是多余了?这样是不是好理解一些?
      

  7.   

    十分感谢,这个地方我明白了,第二个地方想请教高手,CalcButton[p].addActionListener(this); 
    这行程序是什么意思呢,有什么作用呢?
      

  8.   

    呵呵,
    在CalcButton数组中p位置上有个Button,将当前对象作为这个Button的事件侦听器加上去。
    作用是从此这个Button上按鼠标就有用了。