gpt4 book ai didi

java - 是否有可能 "reset"一个 MigLayout

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:28:35 24 4
gpt4 key购买 nike

我是 MigLayout 的新手。我正在尝试创建与此类似的布局:

enter image description here

有一排等间距的按钮,后面是一行列表,该列表跨越所有列并增长以填充可用的垂直空间,最后一行包含更多控件。

我可以毫不费力地获得前两行。

但是,当我添加最后一行的内容时,MigLayout(理所当然地)尝试保留前两行的列。我留下了这样的东西:

enter image description here

最后一行的标签和微调器扩展了列的宽度,我在第一行留下了不均匀的间隙。

有什么方法可以告诉 MigLayout 我想忘记到目前为止建立的行/列并“重新开始”,或者这里的解决方案是创建嵌套布局吗?

这是一个完整的示例面板。

public class TestPanel extends JPanel {

JButton button1 = new JButton("Button 1");
JButton button2 = new JButton("Button 2");
JButton button3 = new JButton("Button 3");
JButton button4 = new JButton("Button 4");
JButton button5 = new JButton("Button 5");

JList list = new JList(new String[]{"some", "fake", "data"});

JLabel label = new JLabel("this is my long label");
JSpinner spinner = new JSpinner();
JCheckBox checkbox = new JCheckBox("Check me");

public TestPanel() {
initComponents();
}

private void initComponents() {

setLayout(new MigLayout());

add(button1);
add(button2);
add(button3);
add(button4);
add(button5, "wrap");

add(list, "span, growx, growy, wrap");

// without these 3 lines, the row of buttons are equally spaced
// adding the long label extends the width of the first column
add(label);
add(spinner);
add(checkbox, "span, align right");
}
}

最佳答案

我能够通过合并和拆分单元格来实现所需的布局。

setLayout(new MigLayout());
add(button1);
add(button2);
add(button3);
add(button4);
add(button5, "wrap");
add(list, "span, growx, growy, wrap");

// merge 4 cells then split the combined cell in half
// label goes in the first cell of the split
// spinner goes in the second cell of the split
add(label, "span 4, split 2);
add(spinner);
// check box goes in the 5th and final cell of the row (after the 4 merged cells)
add(checkBox, "align right");

结果如下:

enter image description here

关于java - 是否有可能 "reset"一个 MigLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17977685/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com