Java + Miglayout - 顶部边距边框问题?

4
我有一个关于边距的问题。可能很容易解决,但我不知道原因。我有四个组件,三个jscrollpanel和一个jpanel。组件的摆放方式如下所示:
问题用红色椭圆圈标记。如何消除这个边距?我知道,问题与边框有关(即使我为每个组件使用相同的方法创建边框)。我正在使用以下内容:
setBorder(BorderFactory.createTitledBorder("Sterowanie:"));

但是如果我不为JPanel设置边框(带有标签“Sterowanie”的组件),它会填满整个区域而没有留白。加上边框后,它只会填充边框区域。我用来放置组件的代码如下:

proxys = new ItemViewer("Numery:");
add(proxys, "height 65%, width 33%");

accs = new ItemViewer("Konta:");
add(accs, "height 65%, width 33%");

panel = new JPanel();
panelLayout = new MigLayout("insets 0 0 0 0");
panel.setBorder(BorderFactory.createTitledBorder("Sterowanie:"));
add(panel, "height 65%, width 34%, wrap");

log = new Log("Log:");
add(log, "height 35%, width 100%, span");

嗯?

虽然没有太多使用MigLayout的经验,但你尝试过使用CompoundBorder吗?如果你尝试将其与TitledBorder一起使用,只需添加一个EmptyBorder即可,希望这可以在某种程度上帮到你 :-) - nIcE cOw
2个回答

2
您使用MigLayout的方式对我来说有点奇怪(请不要介意,我只是学了另一种方法)。尝试这样做:
content.setLayout(new MigLayout("fill", "[sg, grow][sg, grow][sg, grow]", "[65%][35%]"));
content.add(topLeftComponent, "grow");
content.add(topMiddleComponent, "grow");
content.add(topRightComponent, "grow, wrap");
content.add(bottomComponent, "span 3, grow");

我知道这不是同一种“精神”,但我总觉得这个风格更容易构建。


1
当然没有冒犯的意思 :) 我忘记将这个问题标记为已解决,我一年前使用了Kleopatra的解决方案。但是你的答案也很有用,也许有人会遇到同样的问题,然后他可以尝试两种解决方案。谢谢! - marxin

1
不知道为什么会发生这种情况(我最初的猜测是ItemView与普通面板的垂直默认对齐方式不同),但可以重现 - 并通过使所有单元格可增长来解决问题,无论是在单元格还是行约束中:
    JComponent comp = new JScrollPane(new JTable(20, 1));
    comp.setBorder(new TitledBorder("Some Item"));
    JComponent other = new JScrollPane(new JTable(10, 1));
    other.setBorder(new TitledBorder("Other items"));
    JComponent panel = new JPanel();
    panel.setBorder(new TitledBorder("Stewhatever"));
    JTextArea log = new JTextArea();
    log.setBorder(new TitledBorder("Log"));

    MigLayout layout = new MigLayout("wrap 3, debug"); //, "", "[fill, grow]"); 
    JComponent content = new JPanel(layout);
    String cc = "width 33%, height 65%, grow";
    content.add(comp, cc);
    content.add(other, cc);
    content.add(panel, cc);
    content.add(log, "height 35%, span, grow");

没有任何增长,片段会复制您的屏幕截图,如果在cc或注释行约束中有增长,则所有上部组件都将对齐在顶部。

不确定这是否是一个错误或应该预期的行为。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接