如何制作JPanel网格? [英] How do I make A JPanel grid?

查看:82
本文介绍了如何制作JPanel网格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i'm trying to create a gui for chess-like game (6x6 board with less pieces).

使用循环和JPanels创建板非常容易,但是我不知道如何将这些块放在JPanel之上.我尝试通过创建带有图标的JLabel对象来添加一个片段并将其添加到JPanel中,但是看起来不太好.

creating the board is pretty easy with for loops and JPanels but I have no idea how to put the pieces on top of the JPanel. I tried adding one piece by creating a JLabel object with an icon and add it to JPanel but it doesn't look good.

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;
    }
}

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

is there a particular class that i need to use in order to do this properly? default board:

一块一块的板子:

谢谢

推荐答案

要使面板正确,只需处理颜色变化:

To get the board right, just handle the color change :

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必须是透明的,因此可见.秋千上实现此目的的一种方法是使用玻璃窗格:

Trying to add another component (the piece) to the board (gridlayout) messes it up.
What you want to do is add another JPanel that contains the pieces.
The pieces JPanel needs to be transparent, so the board under it, is visible. One way to achieve it in swing is to use a glass pane :

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);
    }
}


这篇关于如何制作JPanel网格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆