我最近正在尝试棋盘游戏,现在我正在制作跳棋棋盘游戏。但是,我无法弄清楚如何在GridLayout
. 每个单元格都有自己的JPanel ,我通过FOR Loop在 2D 数组中分配了它。
我需要在特定的JPanelp1Chip
中显示只是 a的图像,可以说它的变量名 name is ,而不会弄乱..png
board[2][3]
GridLayout
关于我如何做到这一点的示例代码会很棒,因为它将帮助我更好地理解。
我在互联网上搜索过,但找不到我需要的东西,或者至少找不到解释如何做的东西。
这是到目前为止的代码:
package checkers;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Image;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.io.File;
import java.io.IOException;
import javax.swing.JTextField;
public class Main extends JFrame {
private JPanel contentPane;
Image p1Chip;
JPanel[][] board = new JPanel[8][8];
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main frame = new Main();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Main() throws IOException {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 800, 800);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
startGame();
}
//Start Game!
public void startGame() throws IOException{
drawBoard();
}
//******************************DRAWS BOARD******************************\\
//Draws the board
public void drawBoard() throws IOException{
System.out.println("Start Drawing Board!");
getContentPane().setLayout(new GridLayout(8,8));
int colorAssignRow = 0;
int colorAssignCol = 0;
for(int r = 0; r < 8; r++){
colorAssignRow++;
colorAssignCol = 0;
for(int c = 0; c < 8; c++){
colorAssignCol++;
board[r][c] = new JPanel();
if(colorAssignRow%2!=0){
if(colorAssignCol%2==0)board[r][c].setBackground(Color.RED);
else board[r][c].setBackground(Color.BLACK);
}
else if(colorAssignRow%2==0){
if(colorAssignCol%2==0)board[r][c].setBackground(Color.BLACK);
else board[r][c].setBackground(Color.RED);
}
getContentPane().add(board[r][c]);
}
}
System.out.println("Board Drawing Done!");
}
//******************************END OF DRAWING BOARD******************************\\
public void getAssets(){
System.out.println("Getting assets!");
p1Chip = new ImageIcon("P1ChipNormal.png").getImage();
}
}
上面的代码工作正常,因为它只输出JPanels的 Checkers 板,每个都位于网格的不同单元格中。
更新:添加此方法以显示芯片,但是当我运行此方法时,没有显示芯片。
public void drawChips(){
/*
* When: 0 and even
* 1 and odd
* 2 and even
*/
//Drawing Player One Chips\\
for(int r = 0; r < 8; r++){
for(int c = 0; c < 8; c++){
label[r][c] = new JLabel();
board[r][c] = new JPanel();
if(r==0 && c%2==0){
label[r][c].setIcon(p1Chip);
board[r][c].add(label[r][c]);
}
else if(r==1 && c%2!=0 && c!=0){
label[r][c].setIcon(p1Chip);
board[r][c].add(label[r][c]);
}
else if(r==2 && c%2==0){
label[r][c].setIcon(p1Chip);
board[r][c].add(label[r][c]);
}
}
}
}