繁体   English   中英

如何制作 JPanel 网格?

[英]How do I make A JPanel grid?

我正在尝试为类似国际象棋的游戏创建一个 gui(6x6 棋盘,棋子较少)。

使用 for 循环和 JPanel 可以很容易地创建电路板,但我不知道如何将这些部件放在 JPanel 之上。 我尝试通过创建一个带有图标的 JLabel 对象来添加一件并将其添加到 JPanel 中,但它看起来不太好。

public class BoardPanel extends JPanel
{
    public static final int LENGTH = 6;
    private final Color[] COLOR_ARRAY = {Color.decode("#FFFACD"), Color.decode("#593E1A")};

    public BoardPanel() 
    {
        //grid layout 6x6
        setLayout(new GridLayout(LENGTH, LENGTH));
        int numView = 0;
        //tiles color determined by odd/even
        for (int i = 0; i < LENGTH; i++) 
            {
                for (int j = 0; j < LENGTH; j++)
                {
                    add(new TileView(numView, COLOR_ARRAY[j % 2]));
                }

                swap();
            }

    }

    private void swap() {

        Color temp = COLOR_ARRAY[0];
        COLOR_ARRAY[0] = COLOR_ARRAY[1];
        COLOR_ARRAY[1] = temp;
    }
}

是否有我需要使用的特定类才能正确执行此操作? 默认板:

默认板

一块板:

一块板

谢谢,

要获得正确的电路板,只需处理颜色变化:

class BoardPanel extends JPanel
{
    public static final int LENGTH = 6;
    private final Color[] COLOR_ARRAY = {Color.decode("#FFFACD"), Color.decode("#593E1A")};

    public BoardPanel()   {
        //grid layout 6x6
        setLayout(new GridLayout(LENGTH, LENGTH));
        int numView = 1;

        for (int i = 0; i < LENGTH; i++)
        {
            numView = (numView == 0) ? 1:0;
            for (int j = 0; j < LENGTH; j++)
            {
                add(new TileView(COLOR_ARRAY[numView]));
                numView = (numView == 0) ? 1:0;
            }
        }
    }
}

class TileView extends JLabel {

    TileView(Color color) {
        setPreferredSize(new Dimension(100,100));
        setOpaque(true);
        setBackground(color);
    }
}

尝试将另一个组件(部件)添加到电路板(网格布局)会将其搞砸。
您想要做的是添加另一个包含这些片段的JPanel
JPanel需要透明,所以它下面的板是可见的。 在摆动中实现它的一种方法是使用玻璃板:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

public class Chess extends JFrame {

    public Chess()  {

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        add( new BoardPanel());          //add underlaying board
        setGlassPane(new PiecesPanel()); //add glass pane
        getGlassPane().setVisible(true);
        pack();
        setVisible(true);
    }

    public static void main(String[] args) throws InterruptedException {
        new Chess();
    }
}

class BoardPanel extends JPanel {

    public static final int LENGTH = 6;
    private final Color[] COLOR_ARRAY = {Color.decode("#FFFACD"), Color.decode("#593E1A")};

    public BoardPanel()   {
        //grid layout 6x6
        setLayout(new GridLayout(LENGTH, LENGTH));
        int numView = 1;
        //tiles color determined by odd/even
        for (int i = 0; i < LENGTH; i++)
        {
            numView = (numView == 0) ? 1:0;
            for (int j = 0; j < LENGTH; j++)
            {
                add(new TileView(COLOR_ARRAY[numView]));
                numView = (numView == 0) ? 1:0;
            }
        }
    }
}

//container for pieces
class PiecesPanel extends JPanel {

    public static final int LENGTH = 6;
    PiecesPanel(){

        setOpaque(false); //make it transparent
        setLayout(new GridLayout(LENGTH, LENGTH));
        JComponent piece = new Piece();
        add(piece);
    }
}

class TileView extends JLabel {

    public static final int SIZE = 100;
    TileView(Color color) {
        setPreferredSize(new Dimension(SIZE, SIZE));
        setOpaque(true);
        setBackground(color);
    }
}

class Piece extends JLabel{

    Piece() {

        URL url = null;
        try {
            url = new URL("https://dl1.cbsistatic.com/i/r/2017/08/15/9b37ca73-de21-4998-ae7a-07d2915a551e/thumbnail/64x64/0cd91f1c045919af6bdafab3a6f07f99/imgingest-6339051052035379444.png");
        } catch (MalformedURLException ex) { ex.printStackTrace();  }
        setIcon(new ImageIcon(url));
        setVerticalTextPosition(SwingConstants.BOTTOM);
        setHorizontalTextPosition(SwingConstants.CENTER);
    }
}

编辑: Piece 图片链接已失效。 改用https://findicons.com/files/icons/2711/free_icons_for_windows8_metro/64/pawn.png



在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM