包含卡片布局的网格布局 - 是否可以完成?

时间:2016-07-16 09:50:46

标签: java swing jframe grid-layout cardlayout

目标是显示一个细胞表,其中所有细胞彼此独立,每个细胞有几个可选的显示(想象一个Kakuro板,危险板,分机)

这是我第一次尝试挥杆。经过大量的阅读,我决定我的方法是首先使用卡片布局(这部分我很满意)独立设计每个单元格,然后进行“包装”。它在网格布局中。 这可能是我对基本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();
    }

}

我得到的是一堆与主板框架分开的无关框架。 显然我没有正确处理帧(我应该只创建一个..?)。 我们将非常感谢您指导我采取正确方法的任何帮助。

1 个答案:

答案 0 :(得分:1)

如果要将Board添加到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");
    }
}

拿着牢房的董事会:

    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

相关问题