我有一个有趣的布局问题,我正在使用 MigLayout 来解决它。我需要用不对称的瓷砖布局一个面板。我想出的作品,但考虑到 MigLayout 的目标是不需要嵌套面板,这是相当不雅的。我的布局需要如下所示:
每个矩形都将包含一个 JPanel,其中包含各种控件。我想出了一个使用嵌套 JPanel 的解决方案。问题是是否存在不需要额外嵌套面板的更简单的解决方案。这是我的代码,因此您可以看到我如何嵌套面板:
/*
* 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);
}
}