Java Swing+;MIG布局、其他组件的动态增长和收缩
我想实现让组件划分总可用空间,这取决于它们是应该缩小还是增大。这应该是动态的,也就是说;我没有任何关于添加到屏幕上的组件数量的信息。组件应根据其特性保持其高度或增长:Java Swing+;MIG布局、其他组件的动态增长和收缩,java,swing,layout,miglayout,Java,Swing,Layout,Miglayout,我想实现让组件划分总可用空间,这取决于它们是应该缩小还是增大。这应该是动态的,也就是说;我没有任何关于添加到屏幕上的组件数量的信息。组件应根据其特性保持其高度或增长: +++++++++++++++++++++++++++++++++ | +-----------------------+ | | | don't grow | | | +-----------------------+ | |
+++++++++++++++++++++++++++++++++
| +-----------------------+ |
| | don't grow | |
| +-----------------------+ |
| |
| +-----------------------+ |
| | | |
| | growy | |
| | | |
| +-----------------------+ |
| |
| +-----------------------+ |
| | don't grow | |
| +-----------------------+ |
| |
| +-----------------------+ |
| | don't grow | |
| +-----------------------+ |
| |
| +-----------------------+ |
| | | |
| | growy | |
| | | |
| +-----------------------+ |
| |
| +-----------------------+ |
| | don't grow | |
| +-----------------------+ |
+++++++++++++++++++++++++++++++++
我能够创建以下结果:
提供的资料如下:
第二个和第五个文本字段按预期增长。其他组件保持其预期大小,尽管它们的容器不会“收缩”,并被处理为将其空间与其他组件分开。第二个和第五个文本字段应该占用所有的“额外”空间。经过一些测试,我可能已经找到了一个适合我问题中提到的要求的解决方案
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(new LC().debug(1000).flowY().fillX()));
// 1rd set of components
panel.add(new JLabel("Titel"), new CC().grow());
panel.add(new JTextArea(), new CC().grow().push()); // this component can use all the extra space
// 2st set of components
panel.add(new JLabel("Titel"), new CC().grow());
panel.add(new JTextField(), new CC().grow());
panel.add(new JLabel("Description"), new CC().grow());
// 3nd set of components
panel.add(new JLabel("progress"), new CC().grow());
panel.add(new JProgressBar(), new CC().grow());
panel.add(new JTextArea(), new CC().grow().push()); // this component can use all the extra space
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
结果:
正如您可能注意到的,屏幕上的两个组件占用了所有额外的空间,而其他组件保留其默认大小
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(new LC().debug(1000).flowY().fillX()));
// 1rd set of components
panel.add(new JLabel("Titel"), new CC().grow());
panel.add(new JTextArea(), new CC().grow().push()); // this component can use all the extra space
// 2st set of components
panel.add(new JLabel("Titel"), new CC().grow());
panel.add(new JTextField(), new CC().grow());
panel.add(new JLabel("Description"), new CC().grow());
// 3nd set of components
panel.add(new JLabel("progress"), new CC().grow());
panel.add(new JProgressBar(), new CC().grow());
panel.add(new JTextArea(), new CC().grow().push()); // this component can use all the extra space
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}