
import java.awt.*;
import javax.swing.*;
class SimpleNestedLayout {
    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                JPanel gui = new JPanel(new BorderLayout(5,5));
                int sz = 4;
                Container content = new JPanel(new GridLayout(sz, 0, 2, 2));
                for (int f=0; f<sz*sz; f++) {
                    content.add(new JButton());
                }
                gui.add(content, BorderLayout.CENTER);
                Container info = new JPanel(
                        new FlowLayout(FlowLayout.CENTER, 50, 5));
                info.add(new JLabel("Flow"));
                info.add(new JLabel("Layout"));
                gui.add(info, BorderLayout.PAGE_START);
                gui.add(new JLabel("Label"), BorderLayout.LINE_END);
                JOptionPane.showMessageDialog(null, gui);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}
笔记
- 对于 8x8 网格,更改sz为 8。
- 如果提到的“标签”就像在 GUI 中看到的标签,它可能会出现在最外面的面板BorderLayout中FlowLayout(本身就是一个面板),或者Label出现在最外面的gui面板中的其他两个空缺位置中。
- ( ) 和info( )面板还可以根据需要接受更多组件。FlowLayoutcontentGridLayout
- 其他嵌套布局的简单示例。
- PlayerGui(31 行)
  
- WestPanel(30 LOC)不是一个很好的例子,因为它- extends JPanel不是简单地保留一个实例,而是简短。
  
- AmortizationLayout(53 LOC)作为一个例子特别好,因为它使用标题边框概述了父子布局。
 