如何使JLabel始终位于绝对布局中的另一个JLabel之上?

时间:2016-06-20 08:49:29

标签: java netbeans jlabel chess

我正在java Netbeans中构建一个国际象棋游戏,其中的部分是JLabel。所以我希望这些碎片始终位于方形JLabels的顶部。但是,当我将方块的背景颜色更改为绿色(以显示可能的合法移动)时,方块上的碎片变得不可见(或者更确切地说,它们会落后于我认为)。如何确保碎片保持在上面?

注意:布局设置为绝对布局。

以下是截图:

The actual Board is this

You can see the Black queen vanished after mouseOver the Knight

此外,在最后一行的光方块上的骑士在第一张图像中丢失,并以某种方式再次出现在第二张图像中。可能是什么问题?

骑士代码类:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package chess;

import static chess.Main_interface.labels;
import java.awt.Color;
import javax.swing.JLabel;

/**
 *
 * @author pc
 */
public class Knight {
    static void acceptiveSquares(JLabel wn) {
    int index = -1;

        for(int i=0;i<labels.length;i++)
    {
        if(labels[i].getLocation().equals(wn.getLocation()))
            index = i;
    }
           boolean pieceInMiddle = index==18||index==19||index==20||index==21||index==26||index==27||index==28||index==29||index==34||index==35||index==36||index==37||index==42||index==43||index==44||index==45;

        System.out.print(index);
        if(!(index==-1))
        {
            if(pieceInMiddle){
             labels[index+10].setBackground(Color.GREEN);
            labels[index-10].setBackground(Color.GREEN);
            labels[index+6].setBackground(Color.GREEN);
            labels[index-6].setBackground(Color.GREEN);
            labels[index+17].setBackground(Color.GREEN);
            labels[index-17].setBackground(Color.GREEN);
            labels[index+15].setBackground(Color.GREEN);
            labels[index-15].setBackground(Color.GREEN);
        }

        else if(index==22||index==30||index==38||index==46)
        {
            labels[index-10].setBackground(Color.GREEN);
            labels[index+6].setBackground(Color.GREEN);
            labels[index+17].setBackground(Color.GREEN);
            labels[index-17].setBackground(Color.GREEN);
            labels[index+15].setBackground(Color.GREEN);
            labels[index-15].setBackground(Color.GREEN);
        }
        else if(index==10||index==11||index==12||index==13)
        {
           labels[index+10].setBackground(Color.GREEN);
            labels[index-10].setBackground(Color.GREEN);
            labels[index+6].setBackground(Color.GREEN);
            labels[index-6].setBackground(Color.GREEN);
            labels[index+17].setBackground(Color.GREEN);
            labels[index+15].setBackground(Color.GREEN);
        }
        else if(index==50||index==51||index==52||index==53)
        {
           labels[index+10].setBackground(Color.GREEN);
            labels[index-10].setBackground(Color.GREEN);
            labels[index+6].setBackground(Color.GREEN);
            labels[index-6].setBackground(Color.GREEN);
            labels[index-17].setBackground(Color.GREEN);
            labels[index-15].setBackground(Color.GREEN);
        }
             else if(index==17||index==25||index==33||index==41)
        {
            labels[index+10].setBackground(Color.GREEN);
            labels[index-6].setBackground(Color.GREEN);
            labels[index+17].setBackground(Color.GREEN);
            labels[index-17].setBackground(Color.GREEN);
            labels[index+15].setBackground(Color.GREEN);
            labels[index-15].setBackground(Color.GREEN);
        }
             else if(index==16||index==24||index==32||index==40)
        {
            labels[index+10].setBackground(Color.GREEN);
            labels[index-6].setBackground(Color.GREEN);
            labels[index+17].setBackground(Color.GREEN);
            labels[index-15].setBackground(Color.GREEN);
        }
             else if(index==23||index==31||index==39||index==47)
        {
            labels[index-10].setBackground(Color.GREEN);
            labels[index+6].setBackground(Color.GREEN);
            labels[index-17].setBackground(Color.GREEN);
            labels[index+15].setBackground(Color.GREEN);
        }
             else if(index==58||index==59||index==60||index==61)
        {
            labels[index-10].setBackground(Color.GREEN);
            labels[index-6].setBackground(Color.GREEN);
            labels[index-17].setBackground(Color.GREEN);
            labels[index-15].setBackground(Color.GREEN);
        }
             else if(index==2||index==3||index==4||index==5)
        {
            labels[index+10].setBackground(Color.GREEN);
            labels[index+6].setBackground(Color.GREEN);
            labels[index+17].setBackground(Color.GREEN);
            labels[index+15].setBackground(Color.GREEN);
        }
             else if(index==8)
        {
            labels[index+10].setBackground(Color.GREEN);
            labels[index-6].setBackground(Color.GREEN);
            labels[index+17].setBackground(Color.GREEN);
        }
             else if(index==15)
        {
            labels[index-10].setBackground(Color.GREEN);
            labels[index+6].setBackground(Color.GREEN);
            labels[index+15].setBackground(Color.GREEN);
        }
             else if(index==48)
        {
            labels[index+10].setBackground(Color.GREEN);
            labels[index-6].setBackground(Color.GREEN);
            labels[index-15].setBackground(Color.GREEN);
        }
             else if(index==55)
        {
            labels[index+6].setBackground(Color.GREEN);
            labels[index-10].setBackground(Color.GREEN);
            labels[index-17].setBackground(Color.GREEN);
        }

        }
    }
}

和骑士的MouseEntered方法(和其他部分相同):

 private void wnMouseEntered(java.awt.event.MouseEvent evt) {                                
      Knight.acceptiveSquares(wn);
PieceMovement.Piece=wn;  //setting the movable piece to Knight  
    } 

0 个答案:

没有答案