2017-03-26 60 views
-2

适用x和y coridinates到Jbuttons中我有此ArrayList如何在网格布局

ArrayList<JButton> b; 
int limit = 16; 
for(int i = 0; i< limit; i++){ 
    gameBoard.add(new JButton("Counter")); 
} 

需要做它遍历X的另一种方法,y坐标以获得在网格布​​局的按钮。我怎样才能做到这一点。

回答

0

在谈到摇摆您只需一个简单的循环中添加的按钮,因为GridLayout的构造函数接受一个0作为参数之一:

// limit/3 columns and as many rows as needed -> 3 or 4 
JPanel panelWithGrid = new JPanel(new GridLayout(0,limit/3)); 
for(int i = 0; i< limit; i++){ 
     JButton b =new JButton("Counter"); 
     gameBoard.add(b) ; 
     panelWithGrid.add(b) ; 
    }