[英]How can I force 4 cells per row with equal width in Miglayout when only one cell is used?
我有一个动态宽度的面板。 添加到面板的组件应从左到右排列,每行最多 4 个组件(单元格),其中每个组件/单元格填充面板宽度的 25%。 如果只有一个组件,它应该填充父宽度的 25% - 其余 75%(3 个单元格)的空间应该是空的。
我使用 hack 让它工作,但我对这个实现并不满意 - 使用单元格并为每个未使用的单元格创建一个“空”面板。 请参阅下面的代码片段:
MigLayout migLayout = new MigLayout("fillx, insets 0, wrap 4", "", "");
JPanel panel = new JPanel(migLayout);
JLabel label1 = new JLabel("1");
JLabel label2 = new JLabel("2");
JPanel filler1 = new JPanel();
JPanel filler2 = new JPanel();
panel.add(label1, "cell 0 0);
panel.add(label2, "cell 1 0);
panel.add(filler1, "cell 2 0);
panel.add(filler2 , "cell 3 0);
这给出了如下所示的内容,其中外支架是外面板,两个内支架是标签:
[ [ 1 ] [ 2 ] ]
我希望它可以设置约束,而不是使用填充物,如下所示:
MigLayout migLayout = new MigLayout("always 4 cells, fillx", "[fill][fill][fill][fill]", "");
JPanel panel = new JPanel(migLayout);
JLabel label1 = new JLabel();
JLabel label2 = new JLabel();
panel.add(label1);
panel.add(label2);
我尝试了各种布局约束并添加了组件参数,但它们的格式通常如下:
[ [ 1 ] [ 2 ] ]
我会选择GridLayout 。 GridLayout 将根据给定的行/列将您的面板分成单元格(检查其构造函数)。 然后每个组件将 100%(宽度和高度)适合单元格。
但是:如果您将网格布局设置为 1 行 4 列(您的情况)并且只添加了 1 个组件,则布局将调整为:1 行和 1 列,因为它不会留下空白空间。
技巧/解决方案:添加一个空组件与我们向 BoxLayout 添加间隙的方式完全相同。
private static Component createSpace() {
return Box.createRigidArea(new Dimension(1, 1));
}
缺点:如果要在显示后将组件添加到面板中,则必须删除空格,因此您必须将所有空组件存储到结构中或执行以下操作(我总是喜欢它):
gridLayoutPanel.removeAll();
gridLayoutPanel.add(component1);
gridLayoutPanel.add(newComponent); //This was space before
gridLayoutPanel.add(createSpace());
gridLayoutPanel.add(createSpace());
gridLayoutPanel.repaint();
gridLayoutPanel.revalidate();
SSCCE将是(忽略组件的高度):
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class GridLayoutTest extends JFrame {
public GridLayoutTest() {
getContentPane().setLayout(new GridLayout(1, 4));
JButton component1 = new JButton("Component1");
JButton component2 = new JButton("Component2");
JButton component3 = new JButton("Component3");
add(component1); //25% of the width
add(component2); //25% of the width
add(component3); //25% of the width
add(createSpace()); //25% of the width
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(500, 200);
setLocationRelativeTo(null);
}
private static Component createSpace() {
return Box.createRigidArea(new Dimension(1, 1));
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new GridLayoutTest().setVisible(true));
}
}
您可以在列约束中使用 %,并记得添加 '!' 这样每一列都有一个固定的宽度。
MigLayout migLayout = new MigLayout("wrap 4, fill", "[25%!,fill][25%!,fill][25%!,fill][25%!,fill]");
JPanel panel = new JPanel(migLayout);
JLabel label1 = new JLabel();
JLabel label2 = new JLabel();
panel.add(label1, "grow"); // add grow here if you want to fill up the 25%
panel.add(label2, "grow");
还可以查看http://www.miglayout.com/whitepaper.html ,所有 miglayout 技巧都在其中
可能有点晚了,但我认为这是解决方案:
public class Test extends JFrame {
public Test() {
// Col Constraint
String ccol="[fill, 25%:25%:25%]";
// Cell Constraint
String ccell="";
MigLayout migLayout = new MigLayout("debug, fillx, insets 5, wrap 4", ccol+ccol+ccol+ccol, "");
Container panel = getContentPane();
panel.setLayout(migLayout);
MyComponent label;
// row 1
label = new MyComponent("1.1");
panel.add(label, ccell + "cell 0 0");
label = new MyComponent("1.2");
panel.add(label, ccell + "cell 1 0");
// row 2
label = new MyComponent("2.1");
panel.add(label, ccell + "cell 0 1");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(500, 200);
setLocationRelativeTo(null);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new Test().setVisible(true));
}
private static class MyComponent extends JLabel {
public MyComponent(String text) {
super(text);
setOpaque(true);
setBackground(Color.YELLOW);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.