简体   繁体   中英

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]);             

        }   
    }

}

I've tried to add the buttons, but it does not work. I've added a test button to the same panel manually and that one does work. I've creating and adding it in the same 2 for cycles and in separate one for the same result.

In your for loops, I think you mean for your condition statements to be i<6 rather than i==6

Example:

for (int i=0; i<6; i++){
    for (int j=0; j<6; j++){

Also your GridLayout should be 6x6 instead of 7x7:

setLayout(new GridLayout(6,6));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM