【发布时间】:2010-03-29 02:01:33
【问题描述】:
这是我下面的代码
import javax.swing.*;
import java.awt.*;
public class board2 {
JFrame frame;
JPanel squares[][] = new JPanel[8][8];
public board2() {
frame = new JFrame("Simplified Chess");
frame.setSize(500, 500);
frame.setLayout(new GridLayout(8, 8));
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
squares[i][j] = new JPanel();
if ((i + j) % 2 == 0) {
squares[i][j].setBackground(Color.black);
} else {
squares[i][j].setBackground(Color.white);
}
frame.add(squares[i][j]);
}
}
squares[0][0].add(new JLabel(new ImageIcon("rookgreen.png")));
squares[0][2].add(new JLabel(new ImageIcon("bishopgreen.png")));
squares[0][4].add(new JLabel(new ImageIcon("kinggreen.png")));
squares[0][5].add(new JLabel(new ImageIcon("bishopgreen.png")));
squares[0][7].add(new JLabel(new ImageIcon("rookgreen.png")));
squares[7][0].add(new JLabel(new ImageIcon("rookred.png")));
squares[7][2].add(new JLabel(new ImageIcon("bishopred.png")));
squares[7][4].add(new JLabel(new ImageIcon("kingred.png")));
squares[7][5].add(new JLabel(new ImageIcon("bishopred.png")));
squares[7][7].add(new JLabel(new ImageIcon("rookred.png")));
for (int i = 0; i < 8; i++) {
squares[1][i].add(new JLabel(new ImageIcon("pawngreen.png")));
squares[6][i].add(new JLabel(new ImageIcon("pawnred.png")));
}
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
new board2();
}
}
我正在尝试创建一种国际象棋游戏,我需要帮助在棋盘的各个面上放置标签以标记 A-H 或 1-8 中的行和列。我不知道该怎么做。稍后我将添加一个拖放片段的功能。最好使用 JLabels 吗?无论如何,我会去把标签放在一边吗?谢谢!
【问题讨论】:
-
如果我是你,我会为此使用更多的 MVP 和一些类。
-
我同意保罗的观点。我的意思是,如果你不能在棋盘上贴上标签,那么阅读 AI 的极小极大定理会很有趣——如果你完全放弃 AI,甚至可以弄清楚每个玩家的移动是否有效。
-
我应该使用什么来代替标签?我应该使用 Panel 还是其他形式的容器?