The constructGrid method constructs a grid of buttons. When first created after pressing the "new game button", the buttons are fine and visibile, but after you click one of the buttons or the frame background all of the buttons dissapear. Here is all of the code. Also, another smaller issue is that the menu bar appears above the top of the frame.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Javalization
{
private static void Construct()
{
JFrame.setDefaultLookAndFeelDecorated(true);
final JFrame frame = new JFrame("Javalization v1.0");
final JFrame game = new JFrame("Javalization v1.0");
MenuBar menuBar = new MenuBar();
Menu menuFile = new Menu();
MenuItem menuFileExit = new MenuItem();
menuFile.setLabel("File");
menuFileExit.setLabel("Exit");
menuFileExit.addActionListener
(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
);
menuFile.add(menuFileExit);
menuBar.add(menuFile);
frame.setMenuBar(menuBar);
JButton butan = new JButton("Exit");
JButton nGame = new JButton("New Game");
Container con = frame.getContentPane();
Container gameCon = game.getContentPane();
frame.setResizable(false);
game.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel emptyLabel = new JLabel("");
emptyLabel.setLayout(null);
emptyLabel.setPreferredSize(new Dimension(512,384));
emptyLabel.setBackground(Color.CYAN);
frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);
JLabel gameLabel = new JLabel("");
gameLabel.setLayout(null);
gameLabel.setPreferredSize(new Dimension(512,512));
gameLabel.setBackground(Color.CYAN);
game.getContentPane().add(gameLabel, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
game.pack();
game.setVisible(false);
//Image icon = Toolkit.getDefaultToolkit().getImage("Logo.jpg");
//frame.setIconImage(icon);
//game.setIconImage(icon);
nGame.setBounds(80,304,100,50);
butan.setBounds(206,304,100,50);
butan.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae)
{
System.exit(0);
}});
nGame.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent x)
{
frame.setVisible(false);
game.setVisible(true);
}
});
con.add(butan);
con.add(nGame);
constructGrid(gameCon,500,500,25,25);
}
private static void constructGrid(Container container, int xPane, int yPane, int xSize, int ySize )
{
int xMod = (xPane/xSize);
int yMod = (yPane/ySize);
int row = 0;
int column = 0;
int ConstructedCounter = (xMod*yMod) + 1;
JButton [][] ButtonArray = new JButton[xMod][yMod + 1];
while (ConstructedCounter >= 0)
{
ConstructedCounter --;
JButton current = new JButton();
current.setBounds(xMod * row + 50, yMod * column + 50, xMod, yMod);
current.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent x)
{
}
});
ButtonArray[row][column] = current;
container.add(ButtonArray[row][column]);
column++;
if (column >= xMod)
{
column = 0;
row ++;
}
JButton last = new JButton();
last.setBounds(xMod * row + 50, yMod * column + 50, xMod, yMod);
ButtonArray[row][column] = last;
container.add(ButtonArray[row][column]);
}
}
public static void main(
String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
Construct();
}
});
}
}