所以我正在制作一个有趣的棋盘游戏。我让董事会部分工作,现在我需要出现的部分。我已经尝试将它们放在JButton上了,这样......有点......按钮纹理(?)接管了棋盘。我昨天用JLabel完成了它,并且有效......有点......图标在JLabel中平铺或类似的东西,因为典当的上半部分在全尺寸图片下方的图片中。这个程序只是设置板和放置件。就在这篇文章之前,我正在看this。这对我来说不起作用,但这正是我所寻找的。 p>
package Chess;
import javax.swing.*;
import java.util.*;
import java.util.logging.Level;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.*;
public class Board extends Game
{
public void createUI()
{
JFrame board = new JFrame("ADG");
JPanel places = new JPanel(new GridLayout(8,8));
JPanel[][] tiles = new JPanel[8][8];
ImageIcon pawnWhite = new ImageIcon("H:/ChessPieces/PawnWhite");
ImageIcon pawnBlack = new ImageIcon("H:/ChessPieces/PawnBlack");
ImageIcon kingBlack = new ImageIcon("H:/ChessPieces/KingBlack");
ImageIcon kingWhite = new ImageIcon("H:/ChessPieces/KingWhite");
ImageIcon queenBlack = new ImageIcon("H:/ChessPieces/QueenBlack");
ImageIcon queenWhite = new ImageIcon("H:/ChessPieces/QueenWhite");
ImageIcon rookBlack = new ImageIcon("H:/ChessPieces/RookBlack");
ImageIcon rookWhite = new ImageIcon("H:/ChessPieces/RookWhite");
ImageIcon bishopBlack = new ImageIcon("H:/ChessPieces/BishopBlack");
ImageIcon bishopWhite = new ImageIcon("H:/ChessPieces/BishopWhite");
ImageIcon knightBlack = new ImageIcon("H:/ChessPieces/KnightBlack");
ImageIcon knightWhite = new ImageIcon("H:/ChessPieces/KnightWhite");
JLabel[] pawnWhites = new JLabel[8];
JLabel[] pawnBlacks = new JLabel[8];
for(int i = 1; i < 9; ++i)
for(int j = 1; j < 9; ++j)
{
tiles[i-1][j-1] = new JPanel();
for(int z = 1; z<9;z++)
{
pawnBlacks[z-1] = new JLabel("", pawnBlack, JLabel.CENTER);
pawnWhites[z-1] = new JLabel("", pawnWhite, JLabel.CENTER);
pawnWhites[z-1].setOpaque(true);
tiles[i-1][j-1].add(pawnWhites[z-1]);
}
if(i%2==0)
{
if(j%2==0)
{
tiles[i-1][j-1].setBackground(Color.BLACK);
tiles[i-1][j-1].setSize(100,100);
places.add(tiles[i-1][j-1]);
} else if(j%2 !=0)
{
tiles[i-1][j-1].setBackground(Color.WHITE);
tiles[i-1][j-1].setSize(100,100);
places.add(tiles[i-1][j-1]);
}
} else if(i%2 !=0)
{
if(j%2==0)
{
tiles[i-1][j-1].setBackground(Color.WHITE);
tiles[i-1][j-1].setSize(100,100);
places.add(tiles[i-1][j-1]);
} else if(j%2 !=0)
{
tiles[i-1][j-1].setBackground(Color.BLACK);
tiles[i-1][j-1].setSize(100,100);
places.add(tiles[i-1][j-1]);
}
}
}
places.setSize(800,800);
board.setSize(800,800);
places.setSize(800,800);
board.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
board.add(places);
board.setVisible(true);
}
}
注意:我尝试将setContentAreaFilled设为false,并且在任何面板中都有8个按钮,ALL都没有我的图标。然后我尝试将其作为数组删除,就像1 JButton一样。之后它根本没有显示按钮。我认为缺少Icon是因为它在某种程度上找不到Icon有问题,但我无法确定。
编辑:感谢peeskillet,我找到了答案。我正在切换太多东西,忘了添加文件扩展名