简体   繁体   中英

Trying to create a frame full of JButtons, but my JButtons won't load

I'm making a chess game for a project and one of the first things I want to do is create a frame, then fill it with 64 JButtons (organised 8x8) which will each act as the squares on a chess board. I'm still very new to Java, but I still don't think I should be getting the error I'm getting, it wasn't happening a while ago, but that said when the frame did load before, none of the JButtons did.

I seem to be having a problem when it comes to adding coordinates to my JButtons using a 3D array, I keep getting the error "the method add(ChessSquare) is undefined for the type ChessBoard," on top of that Eclipse keeps offering me help with my mistakes but I think I may be making things even worse by accepting them.

Another problem is when I try to hold the square's coordinates from the 3D array in ChessSquare.

I currently have 2 classes, ChessBoard and ChessSquare, I'm trying to get ChessBoard to use ChessSquare to make the pieces.

Sorry if I'm not very clear, I'm incredibly tired.

Thanks in advance for any help.

Here is my code:

Board:

import java.awt.GridLayout;
import java.io.IOException;
import javax.swing.JFrame;
import Logic.ChessSquare;


public class ChessBoard {

    //chess board constructor
    public ChessBoard() throws IOException {

        //create grid and grid dimensions
        GridLayout grid = new GridLayout(8,8);

        //create frame and set specifications of frame
        JFrame frame = new JFrame();
        frame.setVisible(true);
        frame.setSize(458, 458);
        frame.setTitle("I'm starting to prefer C");

        //initialise 3D array
        ChessSquare[][] square = new ChessSquare[8][8];

        //create 64 instances of ChessSquare and assign each square as an element in the 3D array
        for (int i = 0; i < 8; i++){
            for(int l = 0; l < 8; l++){
                square[i][l] = new ChessSquare();
                this.squarePos(square[i][l]); //this is where my main gripe is
            }
        }



    }

    public static void main(String[] args) throws IOException{
        new ChessBoard();
    }

}

Square:

package Logic;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;

//chess square class, 1 instance of which for each square in the grid
public class ChessSquare extends JButton {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    //instance variables for position and piece
    public int squarePos;
    public String selectedPiece;

    //accessor method for position
    public void squarePos(){
        int squarePos = ChessBoard.square;
    }


    //constructor for chess squares
    public ChessSquare() throws IOException {
                BufferedImage buttonIcon = ImageIO.read(new File("E:\\Eclipse\\ChessF\\src\\Images\\EmptySquare.jpg"));
                JButton button = new JButton(new ImageIcon(buttonIcon));
                }
}

" Another problem is when I try to hold the square's coordinates from the 3D array in ChessSquare "

Put x and y as ChessSquare's attributes, and receive the values in ChessSquare's constructor:

public class ChessSquare extends JButton {
    private int x, y;

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    //instance variables for position and piece
    public int squarePos;
    public String selectedPiece;

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

For initializing the squares and rendering them, modify the loop in ChessBoard 's constructor. You'll want to add the newly created ChessSquare to the frame.

    for (int i = 0; i < 8; i++){
        for(int l = 0; l < 8; l++){
            ChessSquare square = new ChessSquare(i, l);
            square[i][l] = square;
            frame.add(square);
        }
    }

After that each ChessSquare knows its x and y positions.

Oracle has some great tutorials: http://docs.oracle.com/javase/tutorial/java/TOC.html .

Also after you've got the basics, read up on Swing: http://docs.oracle.com/javase/tutorial/uiswing/components/index.html

There are multiple problems:

First and crucial is you are not adding your ChessSquare buttons at all. So call add() method from frame to add buttons.

Second: Call setVisible(true) for your frame in the end of constructor.

Third: Set layout for your frame - frame.setLayout(grid);

Fourth: Set default close operation - frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

PS Also call pack() instead setSize() for frame.

Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM