我知道這里有很多關于 JElements 沒有顯示的堆疊溢位問題,都是因為有人忘記在建構式的末尾添加一個 setVisible(true) 。但至少我相信我的問題是不同的。我目前正在為大學創建一個國際象棋游戲,為此我有
- 游戲課:這里都聚在一起
- Piece 包中擴展 JButton 的抽象 Piece 類
- 以及每個 Piece(Rook、Bishop、...)的類,每個擴展 Piece 并位于 Pieces 包中
在我寫更多之前,這又是一個愚蠢的錯誤,這里的代碼:```java import javax.swing.*; 進口件。*;
匯入 java.awt.*;匯入 java.awt.event.ActionEvent;匯入 java.awt.event.ActionListener;
公共類游戲擴展 JFrame { 私有靜態最終 int 寬度 = 8; 私有靜態最終 int 高度 = 8;
private static Piece clicked;
private static Piece[][] fields = new Piece[width][height];
private JPanel main = new JPanel();
public static void init(JPanel g) {
for (int y = 0; y < fields.length; y ) {
for (int x = 0; x < fields[y].length; x ) {
if (y == 1) fields[y][x] = new Pawn(x, y, true); //2nd or 7th row is filled with pawns
else if (y == 6) fields[y][x] = new Pawn(x, y, false);
else {
fields[y][x] = new Empty(x,y,true);
}
fields[y][x].addActionListener(e -> {
var p = (Piece) e.getSource();
System.out.println(p.getX() p.getY());
});
g.add(fields[y][x]);
}
}
}
public Game() {
main.setBackground(Color.blue.darker());
main.setLayout(new GridLayout(8,8));
this.setSize(800,800);
init(main);
this.add(main);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
var g = new Game();
}
}
```java
package Pieces;
import javax.swing.*;
public abstract class Piece extends JButton {
private int x;
private int y;
private final boolean isWhite;
public Piece(int x, int y, boolean isWhite) {
this.x = x;
this.y = y;
this.isWhite = isWhite;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public boolean isWhite() {
return isWhite;
}
public boolean canMoveTo(int toX, int toY) {
return true;
}
}
每個 Piece 擴展類的設定完全如下:
package Pieces;
import java.awt.*;
public class Pawn extends Piece{
public Pawn(int x, int y, boolean isWhite) {
super(x, y, isWhite);
this.setText(isWhite ? "Pawn" : "pawn");
}
}
預期行為:
實際行為:
狀態 1:[狀態 1][1]
狀態 2:[狀態 2][1]
uj5u.com熱心網友回復:
您不能覆寫getX并且getY像這樣,布局管理器使用這些屬性來布局組件。
相反,可能會利用Point來存盤虛擬或“單元格”位置
public static abstract class Piece extends JButton {
private final boolean isWhite;
private Point cell;
public Piece(int x, int y, boolean isWhite) {
cell = new Point(x, y);
this.isWhite = isWhite;
}
public Point getCell() {
return cell;
}
public boolean isWhite() {
return isWhite;
}
public boolean canMoveTo(int toX, int toY) {
return true;
}
}
可運行的示例...
![初始化后 JButtons 不顯示 [Java Swing]](https://img.uj5u.com/2022/05/16/cd797e5ca4054b628452f14d21b6d54a.png)
import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.JFrame;
import java.awt.GridLayout;
import java.awt.Point;
import javax.swing.JButton;
import javax.swing.JPanel;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new BoardPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class BoardPane extends JPanel {
private static final int width = 8;
private static final int height = 8;
private Piece clicked;
private Piece[][] fields = new Piece[width][height];
public BoardPane() {
setBackground(Color.blue.darker());
setLayout(new GridLayout(8, 8));
buildBoard();
}
protected void buildBoard() {
for (int y = 0; y < fields.length; y ) {
for (int x = 0; x < fields[y].length; x ) {
if (y == 1) {
fields[y][x] = new Pawn(x, y, true); //2nd or 7th row is filled with pawns
} else if (y == 6) {
fields[y][x] = new Pawn(x, y, false);
} else {
fields[y][x] = new Empty(x,y,true);
}
fields[y][x].addActionListener(e -> {
var p = (Piece) e.getSource();
System.out.println(p.getCell());
});
add(fields[y][x]);
}
}
}
}
public static abstract class Piece extends JButton {
private final boolean isWhite;
private Point cell;
public Piece(int x, int y, boolean isWhite) {
cell = new Point(x, y);
this.isWhite = isWhite;
}
public Point getCell() {
return cell;
}
public boolean isWhite() {
return isWhite;
}
public boolean canMoveTo(int toX, int toY) {
return true;
}
}
public static class Pawn extends Piece {
public Pawn(int x, int y, boolean isWhite) {
super(x, y, isWhite);
this.setText(isWhite ? "Pawn" : "pawn");
}
}
public static class Empty extends Piece {
public Empty(int x, int y, boolean isWhite) {
super(x, y, isWhite);
this.setText("X");
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/474897.html
