java - 包含卡片布局的网格布局 - 可以做到吗?

标签 java swing jframe grid-layout cardlayout

目标是显示一个单元格表,其中所有单元格彼此独立,并且每个单元格都有多个可选显示(想象一个 Kakuro 板、危险板、ext.)

这是我第一次尝试挥杆。大量阅读后,我决定我的方法是首先使用卡片布局独立设计每个单元格(这部分我很满意),然后将其“包装”在网格布局中。 这可能是我对基本 Swing 缺乏理解的地方:

在设计每个单元格时,我创建一个框架并向其添加一个面板:

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    public class Cell extends Component{

    JFrame frame = new JFrame();

    /* we'll use a card layout with a panel for each look of the cell */
    CardLayout cardLayout = new CardLayout();
    JPanel containterPanel = new JPanel();
    JPanel firstPanel = new JPanel();
    JPanel secondPanel = new JPanel();
    JPanel thridpanel = new JPanel();

    public  Cell(){

        /* assign the card layout to the container panel */
        containterPanel.setLayout(cardLayout);

        /* assign objects to the different panels - details not important for the sake of the question */
        //....

        /* add the different panels to the container panel and show the initial one */
        containterPanel.add(firstPanel, "firstPanel");
        containterPanel.add(secondPanel, "secondPanel");
        containterPanel.add(thridpanel, "thridpanel");
        cardLayout.show(containterPanel, "firstPanel");

        /* add the container to the frame and display it*/
        frame.add(containterPanel);
        frame.setVisible(true);
    }
}

这表现得很好。 但是我尝试将其包装在网格布局中,其中每个单元格的行为都非常笨拙:

import java.awt.*;


public class Board{

    private static final int COLUMNS_NUM = 3;
    private static final int ROWS_NUM = 3;

    JFrame frame = new JFrame();
    JPanel panel = new JPanel();
    Cell cells[] = new Cell[COLUMNS_NUM * ROWS_NUM];

    public Board(){

        panel.setLayout(new GridLayout(ROWS_NUM, COLUMNS_NUM));
        for (int i = 0; i <ROWS_NUM * COLUMNS_NUM; i++)
        {
            cells[i] = new Cell();
            panel.add(cells[i]);
        }

        frame.add(panel);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        new Board();
    }

}

我得到的是一堆不相关的框架,与主板框架分开。 显然我没有正确处理帧(我应该只创建一个......?)。 任何能指导我采用正确方法的帮助都将不胜感激。

最佳答案

如果要将 Cell 添加到 Board,请将它们设为 JPanel,而不是 JFrame


一个例子:

import java.awt.CardLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;

//make it a sub class of JPanel for easier implementation. 
public class Cell extends JPanel{

    public  Cell(){

        JPanel firstPanel = new JPanel();
        //add a lable just so something is displayed
        firstPanel.add(new JLabel(("Panel 1"))); 
        JPanel secondPanel = new JPanel();
        JPanel thridpanel = new JPanel();

        CardLayout cardLayout = new CardLayout();
        /* assign the card layout */
        setLayout(cardLayout);
    
        /* add the different panels to the container panel and show the initial one */
        add(firstPanel, "firstPanel");
        add(secondPanel, "secondPanel");
        add(thridpanel, "thridpanel");
        cardLayout.show(this, "firstPanel");
    }
}

还有一 block 用来固定电池的板:

    import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;


//make it a sub class of JFrame for easier implementation.
public class Board extends JFrame{

    private static final int COLUMNS_NUM = 3;
    private static final int ROWS_NUM = 3;

    Cell cells[] = new Cell[COLUMNS_NUM * ROWS_NUM];

    public Board(){

        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setPreferredSize(new Dimension(800, 800));

        JPanel panel = new JPanel();
        add(panel);  //or better    getContentPane().add(panel);
        panel.setLayout(new GridLayout(ROWS_NUM, COLUMNS_NUM));

        for (int i = 0; i <(ROWS_NUM * COLUMNS_NUM); i++)
        {
            cells[i] = new Cell();
            panel.add(cells[i]);
        }

        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        new Board();
    }
}

这就是它的样子:

enter image description here

关于java - 包含卡片布局的网格布局 - 可以做到吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38409867/

相关文章:

java - 在单个列单元格中插入 2 行

Java:如何声明一个数组并快速用数据填充它?

java - 如何在不显示 jframe 或 jpanel 的情况下仅显示按钮?

Java:如何取消应用程序退出

java - 让用户保持登录状态,在 Android 上是一个 android 应用程序

Java 将负载平衡到许多 JVM 中

java - Swing PropertyChangeSupport 从文件读取后刷新 GUI

java - 当用户输入文本时 Swing GUI 移动

java - 如何将菜单集成到布局 (Java GUI) 中?

java - 如何在具有 GridLayout 的 JPanels 中居中对齐所有内容