在 for 循环中将数组中的按钮添加到 GridLayout
Adding JButtons from a array to a GridLayout in a for cycle
public class Nono extends JPanel implements ActionListener{
JButton[][] foo= new JButton[6][6];
JButton test;
public Nono(){
TitledBorder border = BorderFactory.createTitledBorder("Tablero de Juego");
border.setTitleColor(Color.BLUE);
setBorder(border);
setLayout(new GridLayout(7,7));
for( int i=0;i==6;i++){
for(int j=0;j==6;j++){
foo[i][j]= new JButton("");
}
}
for( int i=0;i==6;i++){
for(int j=0;j==6;j++){
add(foo[i][j]);
}
}
}
我试过添加按钮,但没有用。我已经手动向同一面板添加了一个测试按钮,并且该按钮确实有效。我已经在相同的 2 个循环中创建并添加它,并在单独的一个循环中创建并添加它以获得相同的结果。
在你的 for
循环中,我认为你的意思是你的条件语句是 i<6
而不是 i==6
示例:
for (int i=0; i<6; i++){
for (int j=0; j<6; j++){
你的 GridLayout
应该是 6x6 而不是 7x7:
setLayout(new GridLayout(6,6));
public class Nono extends JPanel implements ActionListener{
JButton[][] foo= new JButton[6][6];
JButton test;
public Nono(){
TitledBorder border = BorderFactory.createTitledBorder("Tablero de Juego");
border.setTitleColor(Color.BLUE);
setBorder(border);
setLayout(new GridLayout(7,7));
for( int i=0;i==6;i++){
for(int j=0;j==6;j++){
foo[i][j]= new JButton("");
}
}
for( int i=0;i==6;i++){
for(int j=0;j==6;j++){
add(foo[i][j]);
}
}
}
我试过添加按钮,但没有用。我已经手动向同一面板添加了一个测试按钮,并且该按钮确实有效。我已经在相同的 2 个循环中创建并添加它,并在单独的一个循环中创建并添加它以获得相同的结果。
在你的 for
循环中,我认为你的意思是你的条件语句是 i<6
而不是 i==6
示例:
for (int i=0; i<6; i++){
for (int j=0; j<6; j++){
你的 GridLayout
应该是 6x6 而不是 7x7:
setLayout(new GridLayout(6,6));