I have issues with the GridLayout i think. I tried to place 25 buttons to the Jpanel "lightJpanel" but all the buttons are only on one at the top left of my lightJpanel. I tried many things but i don't find the issue... The buttons are created but they are not visibles. A solution to resolve the problem ?
Here's what my code currenlty gives me:
Here's my code:
public class Window extends JFrame {
/**
* Main JPanel
*/
private JPanel container = new JPanel();
/**
* Message area
*/
private JLabel screen = new JLabel();
/**
* table light
*/
private Light light[][] = new Light[5][5];
/**
* button who allows to configure the start of game
*/
private JButton configure = new JButton("Configure the lights");
/**
* button who allows to start the game
*/
private JButton play = new JButton("Play");
/**
* button who allows to place random lights
*/
private JButton random = new JButton("Random configuration");
/**
* stop button for stop the game
*/
private JButton stop = new JButton("Stop");
/**
* construct a window
*
* @param controller the controller of the window
*/
public Window() {
this.setSize(500, 500);
this.setTitle("Game of life");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setResizable(false);
initComponents();
this.setContentPane(container);
this.setVisible(true);
}
/**
* Initialization of graphical components
*/
private void initComponents() {
//for the message
Font font = new Font("Arial", Font.BOLD, 20);
screen = new JLabel("Game of life");
screen.setFont(font);
screen.setForeground(Color.white);
JPanel panScreen = new JPanel();
panScreen.setPreferredSize(new Dimension(480, 40));
JPanel menuButton = new JPanel();
menuButton.setPreferredSize(new Dimension(480, 100));
JPanel lightJpanel = new JPanel();
lightJpanel.setPreferredSize(new Dimension(300, 300));
//listeners for menu buttons
MenuListener menuListener = new MenuListener();
lightJpanel.setLayout(new GridLayout(5, 5, 0, 0));
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
light[i][j] = new Light(i, j);
lightJpanel.add(light[i][j]);
}
}
//size button
stop.setPreferredSize(new Dimension(70, 40));
play.setPreferredSize(new Dimension(70, 40));
random.setPreferredSize(new Dimension(200, 40));
configure.setPreferredSize(new Dimension(200, 40));
//add listener
play.addActionListener(menuListener);
random.addActionListener(menuListener);
configure.addActionListener(menuListener);
stop.addActionListener(menuListener);
//add buttons to JPanel menuButton
menuButton.add(configure);
menuButton.add(random);
menuButton.add(play);
menuButton.add(stop);
stop.setEnabled(false);
//decoration JLabel screen
panScreen.add(screen);
panScreen.setBackground(Color.blue);
panScreen.setBorder(new javax.swing.border.BevelBorder(BevelBorder.RAISED));
Border borderLine = BorderFactory.createLineBorder(Color.BLACK);
panScreen.setBorder(borderLine);
//test jpanel
lightJpanel.setBackground(Color.CYAN);
menuButton.setBackground(Color.black);
container.setBackground(Color.green);
//positioning different JPanel to main JPanel
container.add(panScreen);
container.add(menuButton);
container.add(lightJpanel);
}
Here's my Light class :
import java.awt.Color;
import javax.swing.JButton;
public class Light extends JButton{
/**
* coordonates of the light according to the GridLayout
*/
private int x;
/**
* coordonates of the light according to the GridLayout
*/
private int y;
/**
* define if the light is lit or not
*/
private boolean lit;
/**
* construct a light
* @param abs the abscissa of the light
* @param ord the ordered of the light
*/
public Light(int abs, int ord){
super();
this.x = abs;
this.y = ord;
// Default color
this.setBackground(Color.gray);
this.lit = false;
}
public boolean getLit(){
return lit;
}
public int getX(){
return this.x;
}
public int getY(){
return this.y;
}
/**
* turn on the light
*/
public void setOn(){
this.setBackground(Color.green);
this.lit = true;
}
/**
* turn off the light
*/
public void setOff(){
this.setBackground(Color.gray);
this.lit = false;
}
public void changeState(){
if(lit)
setOff();
else
setOn();
}
}