'Grid wont show despite using for loop to create the cells
I am making a 8x8 Othello board and for some reason the buttons I created wont show up. This is how my code looks like now and when it runs all that comes up is a frame with all green. Here is the code, the green comes from the background of the ContentPane. I have watched some tutorials and most seem to implement a grid this way. Can t seem to find the error
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Otello extends JFrame implements ActionListener {
Random random = new Random();
JFrame frame = new JFrame();
JPanel title_panel = new JPanel();
JPanel button_panel = new JPanel();
JLabel textfield = new JLabel();
JButton[][] buttons = new JButton[8][8];
boolean player1_turn; //Om true är det Player1 tur, vid false är det Player2 tur
Otello(){
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800,800);
frame.getContentPane().setBackground(Color.GREEN);
frame.setLayout(new BorderLayout());
frame.setVisible(true);
textfield.setBackground(new Color(25,25,25));
//field.setForeground(new Color(25,245,0));
textfield.setFont(new Font("Ink free",Font.BOLD, 75 ));
textfield.setHorizontalAlignment(JLabel.CENTER);
textfield.setText("Otello");
textfield.setOpaque(true);
title_panel.setLayout(new BorderLayout());
title_panel.setBounds(0,0,800,65);
button_panel.setLayout(new GridLayout(8,8));
button_panel.setBackground(new Color(150,150,150));
for(int i = 0; i < 8; i++) {
for (int j = 0; i < 8; j++) {
buttons[i][j] = new JButton();
button_panel.add(buttons[i][j]);
buttons[i][j].setFocusable(false);
buttons[i][j].addActionListener(this);
buttons[i][j].setBackground(Color.RED);
buttons[i][j].setOpaque(true);
}
}
title_panel.add(textfield);
frame.add(title_panel,BorderLayout.NORTH);
frame.add(button_panel);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
