Java 国际象棋分层问题

Java 国际象棋分层问题,java,swing,layer,chess,Java,Swing,Layer,Chess,我对Java相当陌生,我决定尝试用Swing做一个国际象棋游戏。 我知道我的程序效率极低,但这并不是我真正的问题。 我的问题是,在添加JButtons之后,我无法查看白色棋子的图像,但是如果我在添加按钮之前输入添加棋子JLabel的代码,我可以看到棋子。出于这个原因,我认为问题在于我有分层问题,我尝试用分层窗格替换面板,但没有成功 基本上,我想做的是让JButton和JLabel都显示在同一个方框中,并保持按钮的可点击性。 另外,我真的不想合并任何新的导入,只想使用JButtons、JLabel

我对Java相当陌生,我决定尝试用Swing做一个国际象棋游戏。 我知道我的程序效率极低,但这并不是我真正的问题。 我的问题是,在添加JButtons之后,我无法查看白色棋子的图像,但是如果我在添加按钮之前输入添加棋子JLabel的代码,我可以看到棋子。出于这个原因,我认为问题在于我有分层问题,我尝试用分层窗格替换面板,但没有成功

基本上,我想做的是让JButton和JLabel都显示在同一个方框中,并保持按钮的可点击性。 另外,我真的不想合并任何新的导入,只想使用JButtons、JLabels等。 对不起,如果我的解释有点混乱,请提前感谢。 这是我的密码:

private JFrame frame = new JFrame();
private JPanel[][] square = new JPanel[8][8];
private JButton[][] squareB = new JButton[8][8];
private JPanel blank[] = new JPanel[6];
private JButton newGame = new JButton("New Game");
private JButton exitGame = new JButton("Exit");

ArrayList <Chess> pieces = new ArrayList<Chess>();
ChessGame() throws IOException
{   

    frame.setLayout(new GridLayout(9,8));
    frame.setSize(800,900);
    for(int x=0; x<8; x++)
    {
        for(int y=0; y<8; y++)
        {
            square[x][y] = new JPanel();
            square[x][y].setSize(100,100);
        }
    }

    for(int y=0; y<8; y++)
    {
        for(int x=0; x<8; x++)
        {
            frame.add(square[x][y]);
            if(y%2==0)
                if(x%2==0)
                    square[x][y].setBackground(Color.cyan);
                else
                    square[x][y].setBackground(Color.blue);
            else
                if(x%2==0)
                    square[x][y].setBackground(Color.blue);
                else
                    square[x][y].setBackground(Color.cyan);
        }
    }
    for(int x=0; x<8; x++)
    {
        for(int y=0; y<8; y++)
        {
            squareB[x][y] = new JButton();
            squareB[x][y] = new Chess(x,y);
            square[x][y].add(squareB[x][y]);
            squareB[x][y].setSize(square[x][y].getSize());
            squareB[x][y].setMargin(new Insets(0, 0, 100, 100));
            squareB[x][y].setContentAreaFilled( false );  
            squareB[x][y].setOpaque(false);
            squareB[x][y].setContentAreaFilled(false);
            squareB[x][y].setBorderPainted(false);
            squareB[x][y].addActionListener(this);
        }
    }
    for(int x=0; x<6; x++)
    {
        blank[x] = new JPanel();
        frame.add(blank[x]);
        if(x==2)
        {
            frame.add(newGame);
            frame.add(exitGame);
        }
    }

    JLabel WPawn = new JLabel(new ImageIcon(((new ImageIcon("C:\\Users\\Matthew\\Desktop\\Chess Pieces\\WPawn.png")).getImage()).getScaledInstance(80, 83, java.awt.Image.SCALE_SMOOTH)));
    squareB[1][1].add(WPawn);

    newGame.addActionListener(this);
    exitGame.addActionListener(this);
    frame.setVisible(true);
}
public static void main(String[] args) throws IOException
{
    new ChessGame();
}
private JFrame=new JFrame();
私人JPanel[][]square=新JPanel[8][8];
私有JButton[][]squareB=新JButton[8][8];
私有JPanel blank[]=新JPanel[6];
私有JButton newGame=新JButton(“新游戏”);
私有JButton exitGame=新JButton(“退出”);
ArrayList片段=新的ArrayList();
ChessGame()抛出异常
{   
frame.setLayout(新的GridLayout(9,8));
框架。设置尺寸(800900);

对于(int x=0;x而不是尝试将
JLabel
添加到没有布局管理器的
JButton
,请设置
JButton
s图标

Image WPawn = new ImageIcon(ImageIcon("C:\\Users\\Matthew\\Desktop\\Chess Pieces\\WPawn.png").getImage().getScaledInstance(80, 83, java.awt.Image.SCALE_SMOOTH));
squareB[1][1].setIcon(WPawn);
你可能想仔细看看


Ps我还建议通过
ImageIcon
来阅读图像

为了更快地获得更好的帮助,发布一个..只有几块棋子。看这个和。我知道这有点傻,但我有点想使用JButtons。这是不可能的吗?那只是JButtons和jpanel,没有棋子。就像我说的,相当低效耳鼻喉科。