This name is more important than the previous one because it will have to be as single word or else it won’t be usable.

The frame must be named. To make sure it can be referenced outside of the ButtonGrid constructor method you place it out side of that method, but within the class. Most variables are named at the top of the class right before the constructor. To create a new frame you type: JFrame frame = new JFrame(); Inside the constructor method we need to make sure that all of the buttons are put in the grid layout. To do this we set the layout of frame by typing: frame. setLayout(new GridLayout(x, y)); Not necessarily mandatory, but to make the frame close when you hit the ‘x’ button in the top right hand corner we need to add the line: frame. setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE); To make the frame a proper size so everything fits we need to run the pack command: frame. pack(); Lastly for the frame we need to make it so it’s visible: frame. setVisible(true);

The buttons that the user interacts with need to be made, but since we don’t know how many we need, they have to be named first. So right below the line where you create frame create the buttons: JButton[][] grid; The two sets of square brackets are there to say that the JButton’s in the grid are kept in a two-dimensional format, if there were only one set of square brackets then it would simply be a line of JButton’s, which still works, it’s just easier to reference which button is being created or interacted with when it’s two-dimensional. The JButton’s have been named, but we still have to say how many buttons there are. You need to add a line of code in the constructor that sets the amount: grid=new JButton[width][length]; Now that it’s been determined that there will be a certain number of buttons, each must be created. The easiest way to do this is with two for loops, one for the x-axis, one for the y-axis. Inside the two loops we make a new button, and for ease of reference the example puts text inside each button so we know which button in the two-dimensional array is where. To create a button, inside the loop you need to put grid[x][y] = new JButton ("("+x+","+y+")");