繁体   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