[英]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. 我正在为一个项目制作国际象棋游戏,我想要做的第一件事就是创建一个框架,然后用64个JButton(有组织的8x8)填充它,每个JButton将作为棋盘上的方块。 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.
我仍然是Java的新手,但我仍然认为我不应该得到我正在获得的错误,它不会发生在前一段时间,但是当框架确实加载之前说,没有JButtons没有。
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. 在使用3D数组添加坐标到我的JButtons时,我似乎遇到了问题,我不断收到错误“方法添加(ChessSquare)未定义为ChessBoard类型”,最重要的是Eclipse继续为我提供帮助我的错误,但我认为接受它们可能会使事情变得更糟。
Another problem is when I try to hold the square's coordinates from the 3D array in ChessSquare. 另一个问题是当我试图在ChessSquare中保持3D阵列的方形坐标时。
I currently have 2 classes, ChessBoard and ChessSquare, I'm trying to get ChessBoard to use ChessSquare to make the pieces. 我目前有两个班,ChessBoard和ChessSquare,我试图让ChessBoard使用ChessSquare来制作作品。
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 " “ 另一个问题是当我试图在ChessSquare中保持3D阵列的方形坐标时 ”
Put x
and y
as ChessSquare's attributes, and receive the values in ChessSquare's constructor: 将
x
和y
作为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;
}
...
For initializing the squares and rendering them, modify the loop in ChessBoard
's constructor. 为了初始化方块并渲染它们,修改
ChessBoard
构造函数中的循环。 You'll want to add the newly created ChessSquare
to the frame. 您需要将新创建的
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);
}
}
After that each ChessSquare
knows its x
and y
positions. 之后,每个
ChessSquare
知道它的x
和y
位置。
Oracle has some great tutorials: http://docs.oracle.com/javase/tutorial/java/TOC.html . Oracle有一些很棒的教程: 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 在获得基础知识之后,请阅读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. 首先,至关重要的是你根本没有添加你的
ChessSquare
按钮。 So call add()
method from frame to add buttons. 所以从frame调用
add()
方法来添加按钮。
Second: Call setVisible(true)
for your frame in the end of constructor. 第二步:在构造函数的末尾为您的框架调用
setVisible(true)
。
Third: Set layout for your frame - frame.setLayout(grid);
第三
frame.setLayout(grid);
:设置框架的布局 - frame.setLayout(grid);
Fourth: Set default close operation - frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
第四:设置默认关闭操作 -
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
PS Also call pack()
instead setSize()
for frame. PS也调用
pack()
而不是setSize()
用于帧。
Good luck! 祝好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.