2

興味深いレイアウトの問題があり、MigLayout を使用して解決しています。非対称タイルでパネルをレイアウトする必要があります。私が思いついたものは機能しますが、ネストされたパネルを必要としないという MigLayout の目標を考えると、かなり洗練されていません。私のレイアウトは次のようにする必要があります。

非対称サイズのタイル

各長方形には、さまざまなコントロールを含む 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);
    }
}
4

0 に答える 0