使用JLayeredPane创建棋盘游戏布局 [英] Creating a board game layout using JLayeredPane

查看:37
本文介绍了使用JLayeredPane创建棋盘游戏布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一项任务,要求我创建您在图像中看到的布局,这是游戏开发的一部分.在此之前,我从来没有为桌面应用程序使用Java,因此在使用Swing& amp;时,我是一个完全的菜鸟.AWT库.该图像表明我们使用JLayeredPane作为根容器,然后将其余部分添加到它的顶部.我的问题是我尝试从背景图像和gridLayout开始,但是除了显示背景之外,我似乎什么都没做.网格根本不显示(没有边界线,没有单元格的背景),并且我添加到网格中的任何其他组件都失败了.有人可以在这里指出正确的方向吗?我已经阅读了文档&看到了各种布局,容器和组件的一些示例,但我似乎无法使它们全部协同工作.

I have an assignment which requires me to create the layout that you see in the image as part of the development of a game. I've never worked with Java for desktop applications before so i'm a complete noob when it comes to using the Swing & AWT libraries. The image suggests that we use a JLayeredPane as our root container and then add the rest on top of it. My issue is that i've tried starting with the background image and the gridLayout but i can't seem to make anything other than the background show up. The grid doesn't show up at all (no border line, no background of the cells) and any other component i've added to it has failed. Can somebody point me in the right direction here? I've read the docs & saw some example of various layouts,containers and components but i can't seem to make all of them work together.

到目前为止,这是我的代码:

Here's my code so far:

public class BoardView  extends JFrame{


// Constructor
public BoardView() {
    JFrame window = new JFrame("Sorry Game"); // create a new Jwindow instance
    ImageIcon appIcon = new ImageIcon(getClass().getClassLoader().getResource("res/icon.png")); // create the icon for the app
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // when the 'X' button is clicked, the app stops
    window.setSize(new Dimension(1000, 700)); // setting the size of the window
    window.setResizable(false); // Window won't be resizable
    window.setIconImage(appIcon.getImage()); // set the icon for the app
    window.setLayout(new BorderLayout());
    JLayeredPane layeredPane = new JLayeredPane();
    JLabel background = new JLabel();
    background.setSize(1000,700);
    background.setIcon(new ImageIcon(getClass().getClassLoader().getResource("res/background.png")));  for the JLabel
    layeredPane.add(background,0);

    JPanel gridPanel = new JPanel(new GridLayout(16,16));
    gridPanel.setSize(650,700);
    layeredPane.add(gridPanel);

    for(int i = 0; i < 256; i++) {
        JLabel tile = new JLabel();
        tile.setBackground(Color.red);
        tile.setBorder(new LineBorder(Color.black));
        gridPanel.add(tile);
    }

    JLabel logo = new JLabel();
    logo.setIcon(new ImageIcon(getClass().getClassLoader().getResource("res/sorryImage.png")));
    layeredPane.add(logo);
    window.add(layeredPane);
    window.setLocationRelativeTo(null); // centers the window to the screen
    window.setVisible(true); // make the window visible
}
}

我的思考过程是,我可以将JFrame的布局设置为BorderLayout,以便可以将最终布局分为两部分,西部分和东部分.西部将包含网格和各种JLabel和Button,而东部将包含其余组件.我尝试使用 BorderLayout.WEST &在将组件添加到JFrame时使用 EAST 参数,但没有一个起作用或更改了一件事情.我还尝试根据文档在将组件添加到JLayeredPane时使用深度索引,但是再次没有任何变化.

My thought process was that i could set the JFrame's layout to a BorderLayout so that i can brake the final layout down into two parts, the West one and the East one. The West one would contain the Grid and the various JLabels and Buttons and the East one would contain the rest of the components. I've tried using the BorderLayout.WEST & EAST parameters when adding components to the JFrame but none has worked or changed a single thing. I've also tried using an index for the depth when adding components to the JLayeredPane as per the docs but again nothing changes.

P.S.请注意,我不是在找人为我创建布局.我希望有人能帮助我了解我做错了什么以及创建这种布局的最佳方法是什么.

P.S. Please note that i'm not looking for someone to create the layout for me. I want someone to help me understand what i'm doing wrong and what the best way of creating such layouts is.

推荐答案

为了初始化我要在其中放置图像的网格单元格,我不需要在这些位置手动添加它们吗?

In order to initialize the cells of the grid that i want to have images in, don't i need to add them manually in those positions?

如果使用 GridLayout ,则每个单元格都必须具有一个组件,并且必须按顺序添加这些组件.也就是说,添加组件后,它们将根据需要自动包装到下一行.

If you use a GridLayout every cell must have a component and the components must be added in sequential order. That is as components are added they will wrap automatically to the next row as required.

因此,即使您不希望在单元格中放置图像,也需要添加一个虚拟组件,比如说一个没有文本/图标的JLabel.

So even if you don't want an image in a cell you would need to add a dummy component, lets say a JLabel with no text/icon.

一种更简单的方法可能是使用 GridBagLayout .可以将 GridBagLayout 配置为为没有组件的单元格保留"空间.因此,您可以将组件添加到特定的单元格.

An easier approach might be to use a GridBagLayout. The GridBagLayout can be configured to "reserve" space for cells that don't have components. So you can add a component to a specific cell.

import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;

public class GridBagLayoutSparse extends JPanel
{
    public GridBagLayoutSparse()
    {
        setBorder( new LineBorder(Color.RED) );

        GridBagLayout gbl = new GridBagLayout();
        setLayout( gbl );
/*
    //  Set up a grid with 5 rows and columns.
        //  The minimimum width of a column is 50 pixels
        //  and the minimum height of a row is 20 pixels.

        int[] columns = new int[5];
        Arrays.fill(columns, 50);
        gbl.columnWidths = columns;

        int[] rows = new int[5];
        Arrays.fill(rows, 20);
        gbl.rowHeights = rows;
*/
        //  Add components to the grid at top/left and bottom/right

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.BOTH;

        gbc.gridx = 0;
        gbc.gridy = 0;
        addLabel("Cell 0:0", gbc);

        gbc.gridx = 3;
        gbc.gridy = 4;
        addLabel("Cell 3:4", gbc);
    }

    private void addLabel(String text, GridBagConstraints gbc)
    {
        JLabel label = new JLabel(text);
        label.setBorder( new LineBorder(Color.BLUE) );

        add(label, gbc);
    }

    private static void createAndShowGUI()
    {
        JFrame frame = new JFrame("GridBagLayoutSparse");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout( new GridBagLayout() );
        frame.add(new GridBagLayoutSparse());
        frame.setSize(300, 300);
        frame.setLocationByPlatform( true );
        frame.setVisible( true );
    }

    public static void main(String[] args) throws Exception
    {
        java.awt.EventQueue.invokeLater( () -> createAndShowGUI() );
    }
}

  1. 按原样运行代码,组件将在中心分组在一起.
  2. 取消注释块注释,然后再次运行.组件将放置在适当的单元格中.

这篇关于使用JLayeredPane创建棋盘游戏布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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