[英]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.