比如:
Button[] buttons=new Button[10];
for (int i=0;i<10;i++){
Button button=new Button("ok"+i);
button.setSize(...);
...
this.addComponent(button,...);
buttons[i]=button;
}

解决方案 »

  1.   

    控件数组,就是把一件控件放在数组中嘛。这一点在 java 实现里是非常灵活的。你可以用 Button[] buttons = new Button[10] 定义一个包含 10 个元素的 button 数组,然后给每一个元素赋值,如
    buttons[0] = new Button("First Button");也可以用 Component[] components = new Component[5] 定义一个包含 5 个元素的控件数组,然后给每一个元素赋值,可以是不同的组件类型。如
    components[0] = new Lable("First is a lable");
    components[1] = new Button("Second is a button");
    ...如果你是想在一个窗口中动态的添加组件,那是没有必要使用数组的,你只需要在合适的地方使用添加语句添加一个新生成的控件对象就行了。如
    myFrame.getContentPane().add(new Button("New Button"));然后让窗口的大小自动再配一次,如 myFrame.pack();
    或者在共自动适配之前使用 myFrame.invalidate() 使用新添加的控件有效。
      

  2.   

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    class Buttonarray extends JFrame
    {
    //private JButton[] jbarray={new JButton("sdf"),new JButton("dsf")};
    private JButton[] jbarray= new JButton[15];
    private JButton jb=new JButton(" ");
    private JButton jc=new JButton("fa");
    public static void main (String args[])
    {
    JFrame test=new Buttonarray();
    test.setSize(300,300);
    test.setTitle("ButtonArray");
    }
    public Buttonarray()
    {
    Container ctp=getContentPane();
    ctp.setLayout(new GridLayout(7,2));
      for(int i=0;i<=14;i++)
    jbarray[i]=new JButton("Button"+i);
    for (int i=0; i<=14;i++)
    {
    ctp.add(jbarray[i]);
    }
    addWindowListener(new WindowAdapter()
    {
    public void windowClosing(WindowEvent e)
    {
    System.exit(0);
    }
    });
    setSize(300,300);
    setVisible(true);
    }

    }