1

I have code which creates a chessboard. It was working recently but I'm not sure what I changed. When I run it an empty window appears.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Scanner;

public class Chess extends JFrame implements ActionListener {

    JButton[][] a = new JButton[8][8];        //This is the individual squares of the game board
    JPanel board = new JPanel();              //The first instance of the game board   

    int lineCounter = 0;                      //Records the current horizontal row

    String[][] pieceList = new String[8][8];  //This list has the game pieces recorded in it


    public Chess() {

        //Create the board
        setTitle("CHESS");
        setSize(600,600);                      //Sets the window size to 600x600
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        setLayout(new GridLayout(8,8));        //Sets the game board to an 8x8 grid

        for (int i = 0; i < 8; i++) {                            /*This nested loop determines                    the colour of the the board squares, and which colour pieces should go 
                                                              onton which tiles.*/
            for (int j = 0; j < 8; j++){

                if((i+j)%2 == 0 && lineCounter < 3) {              //This if statement sets the top part of the game board, and sets the red pieces
                    pieceList[i][j] = "RedPiece";
                    a[i][j] = new JButton();
                    a[i][j].setBackground(Color.black);
                    board.add(a[i][j]);
                } else if ((i+j)%2 == 0 && lineCounter >= 5) {    //This if statement sets the bottom of the board, and the blue pieces
                    pieceList[i][j] = "BluePiece";
                    a[i][j] = new JButton();
                    a[i][j].setBackground(Color.black);
                    board.add(a[i][j]);

                } else if ((i+j)%2 == 0 && 3 <= lineCounter && lineCounter < 5) {      //This if statement sets the middle of the game board
                    pieceList[i][j] = null;
                    a[i][j] = new JButton();
                    a[i][j].setBackground(Color.black);
                    board.add(a[i][j]);
                } else {
                    pieceList[i][j] = null;
                    a[i][j] = new JButton();
                    a[i][j].setBackground(Color.gray);
                    board.add(a[i][j]);
                }
            }
            lineCounter++;
        }
    }

A for loop follows that adds the new JButtons and sets the background colours. The setVisible(true) is in a separate class. I'll be happy to post more code but I'm sure the problem is here somewhere. I may have left something out. Currently, I'm not adding the pieces game pieces yet. This is the driver class I used:

   public class ChessGUI {
      public static void main(String[] args){
          Chess run = new Chess();
          run.setVisible(true);
      }
   }
Bakuriu
  • 98,325
  • 22
  • 197
  • 231
  • 1
    Nothing in your code you've posted actually adds the buttons to a `Container`. – resueman Sep 11 '14 at 13:49
  • If I add a nested loop that adds JButtons, and a `setVisible(true)`, it works fine. Could you show us the code that is supposed to add the buttons and set the background colours? You can edit it into your post using the "edit" link below your post. – S.L. Barth is on codidact.com Sep 11 '14 at 13:51
  • 1
    See also [Making a robust, resizable Chess GUI](http://stackoverflow.com/q/21142686/418556). – Andrew Thompson Sep 11 '14 at 13:54
  • 3
    For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal, Complete, Verifiable Example). – Andrew Thompson Sep 11 '14 at 13:55
  • Thanks everyone. I updated the code with the for loop, and tried my best to implement MCVE. – Geoff Murphy Sep 11 '14 at 15:04
  • Thanks, Nir. I tried upvoting but I don't have enough reputation :). I'm getting some unexpected output, though. It is not the 8x8 grid I was expecting. My tiles are very thin and there's about 15 in the first row. It also only occupies the top part of the screen. – Geoff Murphy Sep 11 '14 at 16:31
  • Thanks everyone. It's finally back to working condition. I appreciate the help! – Geoff Murphy Sep 11 '14 at 17:44
  • if you are using netbeans see the local history https://ui.netbeans.org/docs/ui/LocalHistory/ , if you are using eclipse http://help.eclipse.org/luna/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2FgettingStarted%2Fqs-LocalHistory.htm – mussdroid Sep 12 '14 at 06:41

3 Answers3

0

Check carefully i think you are not adding those buttons to the board panel you've created and add that panel to frame set border layout to panel not for the Chess class itself.....hope it helps

0

Post the other class with the for loop. I guess you add the buttons like this (for example):

   board.add(a[0][0]);

if so, you add the buttons correctly, but you need to add the JPanel to the JFrame, like this:

   add(board);

(you just add it, because your class extends JFrame, add it in this class).

Edit: As I thought. You need to add the JPanel (after you create it) to the JFrame, like this:

   JPanel board = new JPanel();
   add(board);

In your case add this line right in the begging of the constructor.

Nirgn
  • 1,879
  • 4
  • 23
  • 28
0

You have two issues with your code. First, you're setting the layout on the Chess object, not on the panel you're adding the Buttons to. Second, you never add that JPanel to the frame.

Since it looks like you don't really need the panel, the easiest way to fix it is to remove JPanel board = new JPanel();, and change every board.add(a[i][j]); to add(a[i][j]);

This will add the Buttons directly to the frame (so they're actually being displayed), and puts them into the Container with a GridLayout, so that they're correctly position.

resueman
  • 10,572
  • 10
  • 31
  • 45