I am trying to add a JButton Array to my GridLayout but it seems not to be working.
Probably a rookie mistake — what am I doing wrong?
import java.awt.*;
import java.applet.Applet;
import javax.swing.*;
public class Grid extends JApplet
{
public JButton[] inv;
public void init()
{
setLayout(new GridLayout(4,5));
int i = 0;
while(i>20)
{
inv[i] = new JButton("Slot #" + i);
add(inv[i]);
System.out.println("Button " + i + " added.");
i++;
}
}
}
PS – If you something that could be programmed in a better manner — please fix it for me.
Thank you.
You didn’t create the array, read your text books on how to allocate entries for an array. The Learning the Java Language tutorial has a section on using arrays.
Your loop is wrong. Is
iever greater than 20?