使用MigLayout创建具有不对称大小的切片

时间:2012-06-03 15:31:20

标签: java miglayout

我有一个有趣的布局问题,我正在使用MigLayout来解决它。我需要用不对称的瓷砖布局面板。我提出的工作原理,但鉴于MigLayout不需要嵌套面板的目标,它是相当不优雅的。我的布局需要看起来像这样:

Tiles with asymmetric sizes

每个矩形都包含一个JPanel,其中包含各种控件。我想出了一个使用嵌套JPanels的解决方案。问题是是否有一个更简单的解决方案,不需要额外嵌套面板。这是我的代码,所以你可以看到我如何嵌套面板:

/*
 * Simple code to demonstrate how to solve a unique layout problem.
 *
 * The goal is to layout panels with asymmetrical sizes, like this
 *
 * +-------+-------------------+
 * |       |                   |
 * |       |                   |
 * |       |-------------------|
 * |       |                   |
 * |       |                   |
 * |       |                   |
 * |-------|                   |
 * |       |                   |
 * |       |                   |
 * +-------+-------------------+
 *
 * The trick is to layout two panels vertically with proportional
 * spacing and then layout the two panels horizontally into a super
 * panel, also with proportional spacing. Doing it this way allows
 * any variation of the proportions, controlled easily using the
 * row and column constraints.
*/
package miglayoutdemo;

import java.awt.Color;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import net.miginfocom.swing.MigLayout;

public class AsymmetricTilesDemo
{

    public static void main(String[] args)
    {
        JFrame frame = new JFrame("Asymmetric Tiles");
        JPanel panel = new JPanel();
        frame.setContentPane(panel);
        frame.setSize(400, 400);
        // Insets and gaps are set to zero to tile the surface
        // Column constraint sets the relative column size. Both must be specified or it fails.
        panel.setLayout(new MigLayout("inset 0, gapx 0, gapy 0, fill", "[25%][75%]", "[fill]"));

        // The staggered heights are obtained by using independently laid out
        // panel, one for each column. Otherwise, the MigLayout stretches one
        // or the other cell to make a rectalinear grid of cells, and the
        // staggered height fails.
        JPanel leftPanel = new JPanel();
        JPanel rightPanel = new JPanel();
        // As the column percentages are specified above, the row constraints
        // are specified below for the left and right JPanels. Notice that
        // one is 75/25 and the other is 25/75, which staggers the two
        // horizontal separators.
        leftPanel.setLayout(new MigLayout("inset 0, gapx 0, gapy 0, fill, wrap 1", "[fill]", "[75%][25%]"));
        rightPanel.setLayout(new MigLayout("inset 0, gapx 0, gapy 0, fill, wrap 1", "[fill]", "[25%][75%]"));

        JPanel topPanelLeft = new JPanel();
        JPanel bottomPanelLeft = new JPanel();
        JPanel topPanelRight = new JPanel();
        JPanel bottomPanelRight = new JPanel();
        // Add line borders so we can see what the layout is doing.
        topPanelLeft.setBorder(BorderFactory.createLineBorder(Color.black, 1));
        bottomPanelLeft.setBorder(BorderFactory.createLineBorder(Color.black, 1));
        topPanelRight.setBorder(BorderFactory.createLineBorder(Color.black, 1));
        bottomPanelRight.setBorder(BorderFactory.createLineBorder(Color.black, 1));

        // Grow must be specified in the y direction or the panels
        // do not fill the vertical space.
        leftPanel.add(topPanelLeft, "growy");
        leftPanel.add(bottomPanelLeft, "growy");

        rightPanel.add(topPanelRight, "growy");
        rightPanel.add(bottomPanelRight, "growy");

        // Grow must be specified in the x direction or the panels
        // do not fill the horizontal space.
        panel.add(leftPanel, "growx");
        panel.add(rightPanel, "growx");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

0 个答案:

没有答案