-1

I'm currently trying to make a basic Java Chess game where a ChessBoard is initialized and populated with ChessSquares. Every time I set the window to visible each JButton does not show up unless hovered over. How do I stop this?

ChessBoard

public class ChessBoard extends JFrame implements ActionListener {

    private JPanel p = new JPanel();
    private ChessSquare[][] board = new ChessSquare[8][8];

    public ChessBoard(){

        //Specify frame window.
        setSize(640,640);
        setTitle("Chess");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Add panel to frame window.
        p.setLayout(new GridLayout(8,8));
        setContentPane(p);

        //Populate Chess Board with squares.
        for(int y=0; y<8; y++){
            for(int x=0; x<8; x++){
                board[x][y] = new ChessSquare(x,y);
                board[x][y].addActionListener(this);
                p.add(board[x][y]);
            }
        }

        //Show window.
        setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        ChessSquare s = (ChessSquare) e.getSource();
        //...
    }
}

ChessSquare

import javax.swing.*;

public class ChessSquare extends JButton{

    private int x, y;

    public ChessSquare(int x, int y){
        this.x = x;
        this.y = y;
    }


    public int getX(){
        return x;
    }

    public int getY(){
        return y;
    }
}

On Render | After Mouse Over

  • 1
    1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) See also [Making a robust, resizable Swing Chess GUI](http://stackoverflow.com/q/21142686/418556). 3) Add `@Override` notation to the `getX()` / `getY()` methods to **not** get a compile error message. This in turn indicates a) they already exist b) it's probably unwise to mess with the existing versions. Ultimately, I've rarely seen a good reason to override component classes, and in this case, it's a (provably - given the result) bad idea. – Andrew Thompson Mar 02 '17 at 13:53

1 Answers1

3

Add @Override notation to the getX() / getY() methods to not get a compiler error message. This in turn indicates:

  • Those methods already exist.
  • It's probably unwise to mess with the existing implementations.

Ultimately, I've rarely seen a good reason to override component classes, and in this case, it's a (provably - given the result) bad idea.

This MCVE supports that the problem is in the ChessSquare component by replacing them with standard buttons in the ChessBoard class. It works just fine, despite that there are some further problems in the code (e.g. no call to pack() & guessing the needed size of the GUI, which in this case, leads to a chess board that is not square)1.

import java.awt.GridLayout;
import java.awt.event.*;
import javax.swing.*;

public class ChessBoard extends JFrame implements ActionListener {

    private JPanel p = new JPanel();
    private JButton[][] board = new JButton[8][8];

    public ChessBoard() {

        //Specify frame window.
        setSize(640, 640);
        setTitle("Chess");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Add panel to frame window.
        p.setLayout(new GridLayout(8, 8));
        setContentPane(p);

        //Populate Chess Board with squares.
        for (int y = 0; y < 8; y++) {
            for (int x = 0; x < 8; x++) {
                board[x][y] = new JButton(x + "," + y);
                board[x][y].addActionListener(this);
                p.add(board[x][y]);
            }
        }

        //Show window.
        setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new ChessBoard();
            }
        });
    }

    public void actionPerformed(ActionEvent e) {
        JButton s = (JButton) e.getSource();
        //...
    }
}
  1. "not square" As opposed to the chessboard seen in Making a robust, resizable Swing Chess GUI.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Ah, I understand about the overrides, my mistake! Didn't even think of that! Thank you for your help! I'll just replace the buttons! – Neil Krupa Mar 02 '17 at 15:35