【问题标题】:Java + Miglayout - top margin border issue?Java + Miglayout - 上边距边框问题?
【发布时间】:2012-06-13 11:11:17
【问题描述】:

我的保证金有问题。可能它很容易解决,但我不知道是什么原因。我有四个组件,三个 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有了这个,希望这可能会有所帮助:-)

标签: java swing border padding miglayout


【解决方案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");

在没有任何增长的情况下,sn-p 会复制您的屏幕截图,在 cc 或注释行约束中增长,所有上部组件都在顶部对齐。

不确定这是一个错误还是应该预料到的

【讨论】:

    【解决方案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");
    

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

    【讨论】:

    • 当然没有冒犯 :) 我忘记将这个问题标记为已解决,我一年前使用过 kleopatra 解决方案。但是您的答案也很有用,也许有人会遇到同样的问题,然后他可以尝试两种解决方案。谢谢!
    猜你喜欢
    • 2013-04-25
    • 2012-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-15
    • 2011-05-24
    • 1970-01-01
    相关资源
    最近更新 更多