簡體   English   中英

試圖創建一個充滿JButtons的框架,但我的JButtons將無法加載

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

我正在為一個項目制作國際象棋游戲,我想要做的第一件事就是創建一個框架,然后用64個JButton(有組織的8x8)填充它,每個JButton將作為棋盤上的方塊。 我仍然是Java的新手,但我仍然認為我不應該得到我正在獲得的錯誤,它不會發生在前一段時間,但是當框架確實加載之前說,沒有JButtons沒有。

在使用3D數組添加坐標到我的JButtons時,我似乎遇到了問題,我不斷收到錯誤“方法添加(ChessSquare)未定義為ChessBoard類型”,最重要的是Eclipse繼續為我提供幫助我的錯誤,但我認為接受它們可能會使事情變得更糟。

另一個問題是當我試圖在ChessSquare中保持3D陣列的方形坐標時。

我目前有兩個班,ChessBoard和ChessSquare,我試圖讓ChessBoard使用ChessSquare來制作作品。

對不起,如果我不是很清楚,我會非常累。

在此先感謝您的幫助。

這是我的代碼:

板:

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();
    }

}

廣場:

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));
                }
}

另一個問題是當我試圖在ChessSquare中保持3D陣列的方形坐標時

xy作為ChessSquare的屬性,並在ChessSquare的構造函數中接收值:

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;
    }
...

為了初始化方塊並渲染它們,修改ChessBoard構造函數中的循環。 您需要將新創建的ChessSquare添加到框架中。

    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);
        }
    }

之后,每個ChessSquare知道它的xy位置。

Oracle有一些很棒的教程: http//docs.oracle.com/javase/tutorial/java/TOC.html

在獲得基礎知識之后,請閱讀Swing: http//docs.oracle.com/javase/tutorial/uiswing/components/index.html

有很多問題:

首先,至關重要的是你根本沒有添加你的ChessSquare按鈕。 所以從frame調用add()方法來添加按鈕。

第二步:在構造函數的末尾為您的框架調用setVisible(true)

第三frame.setLayout(grid); :設置框架的布局 - frame.setLayout(grid);

第四:設置默認關閉操作 - frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

PS也調用pack()而不是setSize()用於幀。

祝好運!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM