I am having trouble with my form having a it grid of buttons to appear. The form is supposed to display a grid of buttons that will be used as a platform for a simple battleship game.
This is the code for the Panel for the player grid
package view;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JPanel;
public class PlayerPanel extends JPanel {
private List<PositionButton> ButtonList;
public PlayerPanel()
{
ButtonList = new ArrayList<PositionButton>();
for(int i = 0;i < 10;i++)
for(int j = 0;j < 10;j++)
{
ButtonList.add(new PositionButton(i,j));
}
while(ButtonList.iterator().hasNext())
{
this.add(ButtonList.iterator().next());
}
while(ButtonList.iterator().hasNext())
{
ButtonList.iterator().next().setVisible(true);
}
}
}
This is the class for the actual form:
package view;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GameForm {
private JFrame theFrame;
private JPanel BoardPanel;
private PlayerPanel p1Panel;
private PlayerPanel p2Panel;
public GameForm()
{
//set up the Frame
theFrame = new JFrame();
theFrame.setSize(new Dimension(800,500));
theFrame.setVisible(true);
theFrame.setResizable(false);
theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//set up the board panel
BoardPanel = new JPanel();
theFrame.add(this.BoardPanel);
//set up the first players board
p1Panel = new PlayerPanel();
p1Panel.setLayout(new GridLayout(0,0));
this.BoardPanel.add(p1Panel);
p1Panel.setSize(new Dimension(400,250));
//set up the second players boards
p2Panel = new PlayerPanel();
p2Panel.setLayout(new GridLayout(0,0));
this.BoardPanel.add(p2Panel);
p2Panel.setSize(new Dimension(400,250));
BoardPanel.setVisible(true);
p1Panel.setVisible(true);
p2Panel.setVisible(true);
}
}
And this is the code for the individual button
package view;
import javax.swing.JButton;
public class PositionButton extends JButton {
private int x;
private int y;
public PositionButton(int x, int y)
{
this.x = x;
this.y = y;
this.setVisible(true);
}
public int getX()
{
return this.x;
}
public int getY()
{
return this.y;
}
}
I am struggling to find out why my buttons are not appearing. Thank you for your help in advance.