我想实现让组件根据它们是否应该缩小或增长来划分可用的总空间。这应该是动态发生的,意思是;我没有关于添加到屏幕上的组件数量的任何信息。组件应根据其属性保持其高度或增长:
+++++++++++++++++++++++++++++++++
| +-----------------------+ |
| | don't grow | |
| +-----------------------+ |
| |
| +-----------------------+ |
| | | |
| | growy | |
| | | |
| +-----------------------+ |
| |
| +-----------------------+ |
| | don't grow | |
| +-----------------------+ |
| |
| +-----------------------+ |
| | don't grow | |
| +-----------------------+ |
| |
| +-----------------------+ |
| | | |
| | growy | |
| | | |
| +-----------------------+ |
| |
| +-----------------------+ |
| | don't grow | |
| +-----------------------+ |
+++++++++++++++++++++++++++++++++
我能够创建以下结果:
提供以下SSCCE:
public class SimpleMain {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("MigLayout");
JPanel panel = new JPanel(new MigLayout("debug, flowy", "grow", "grow"));
// 1st set of textfields
panel.add(new JTextField(), "shrink, growx, top");
panel.add(new JTextField(), "grow, top");
panel.add(new JTextField(), "shrink, growx, top");
// 2nd set of textfields
panel.add(new JTextField(), "shrink, growx, top");
panel.add(new JTextField(), "grow, top");
panel.add(new JTextField(), "shrink, growx, top");
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
第 2 和第 5 个文本字段按预期增长。尽管它们的容器没有“缩小”并且正在被处理以将其空间与其他组件分开,但其他组件仍保持预期的大小。第 2 和第 5 个文本字段应占用所有“额外”空间。