如何在BoxLayout中粘贴到容器顶部 [英] How to stick to top of container in BoxLayout

查看:101
本文介绍了如何在BoxLayout中粘贴到容器顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有Y_AXIS布局的父级JPanel.该容器的子代也都是JPanels. 组件堆叠良好,并且全部被分配,但是我希望它们粘贴到父级JPanel的顶部,以便所有多余的空间都在底部,并且组件之间没有多余的空间.我尝试使用胶水,但是可能做错了.我还将所有孩子的AllignmentX和AllignmentY分别设置为左侧和顶部.

I have a parent JPanel with a Y_AXIS layout. The children of this container are all JPanels as well. The components are stacking fine, and are all alligned, but i want them to stick to the top of the parent JPanel so all excess space is in the bottom, and there is no extra space between the components. I have tried using glue, but i might be doing something wrong. I have also set the AllignmentX and AllignmentY to left and top respectively on all children.

所以,我想要的是在父面板中堆叠一堆子代,其高度并未拉伸,并且粘在父容器的顶部(如果可能的话,还留在左侧),所有多余的空间都放在底部,例如:

So, what i want is a stack of children in the parent panel, that is not stretched in height, and sticks to the top (and left, if possible) of the parent container, with all excess space placed in the bottom, as such:

protected void initContent(JPanel panel) {
    id = new JTextField();
    typename = new JTextField();
    currentstep = new JComboBox();
    currentowner = new JComboBox();
    organization = new JTextField();
    area = new JTextField();
    active = new JLabel();
    finished = new JLabel();
    fields = new JList();
    linkedassignments = new JList();
    jobs = new JList();
    JPanel infopanel = new JPanel();
    JPanel listpanel = new JPanel();
    infopanel.setLayout(new BoxLayout(infopanel, BoxLayout.Y_AXIS));
    infopanel.add(Box.createVerticalGlue());
    infopanel.add(makeInfoContainer("Id", id, 50, 100));
    infopanel.add(makeInfoContainer("Type", typename, 50, 100));
    infopanel.add(makeInfoContainer("Organization", organization, 50, 100));
    infopanel.add(makeInfoContainer("Area", area, 50, 100));
    infopanel.add(makeInfoContainer("Active", active, 50, 100));
    infopanel.add(makeInfoContainer("Finished", finished, 50, 100));
    infopanel.add(makeInfoContainer("Step", currentstep, 50, 100));
    infopanel.add(makeInfoContainer("Owner", currentowner, 50, 100));
    listpanel.setLayout(new GridLayout(0,1,5,5));
    listpanel.add(makeJListContainer("Fields", fields, 200, 200));
    listpanel.add(makeJListContainer("Assignments", linkedassignments, 200, 200));
    listpanel.add(makeJListContainer("Jobs", jobs, 200, 200));
    panel.add(infopanel);
    panel.add(listpanel);
}


private Container makeInfoContainer(String name, Component comp, int labelwidth, int compwidth){
    JPanel cont = new JPanel();
    cont.setAlignmentY(Container.TOP_ALIGNMENT);
    cont.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 0));
    JLabel lbl = new JLabel(name);
    lbl.setPreferredSize(new Dimension(labelwidth, 25));
    cont.add(lbl);
    comp.setPreferredSize(new Dimension(compwidth, 25));
    cont.add(comp);
    return cont;
}

private Container makeJListContainer(String name, JList list, int areawidth, int areaheight){
    Box cont = new Box(BoxLayout.Y_AXIS);
    cont.setPreferredSize(new Dimension(areawidth, areaheight));
    JLabel label = new JLabel(name);
    cont.add(Box.createHorizontalGlue());
    cont.add(label);
    JScrollPane pane = new JScrollPane();
    pane.setAlignmentY(Component.TOP_ALIGNMENT);
    pane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    pane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    pane.add(jobs);
    cont.add(pane);
    return cont;
}

推荐答案

假定包含面板的布局管理器(即传递给initContent的面板)具有horizo​​ntalBoxLayout

Assuming that the layoutManager of the containing panel (that is the one passed into the initContent) has a horizontalBoxLayout

  • 删除所有胶水
  • 将两个info/listPanel的 alignmentY 设置为顶部
  • 实现infoPanel的getMaximum以返回其首选项
  • remove all the glue stuff
  • set the alignmentY of both info/listPanel to top
  • implement the getMaximum of the infoPanel to return its pref

类似的东西(边界只是为了可视化哪个容器在哪里,通常很难决定这么多的嵌套:)

Something like (borders are just to visualize which container is where, often hard to decide with so much nesting :)

JComponent panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.LINE_AXIS));
panel.setBorder(BorderFactory.createLineBorder(Color.GREEN));
initContent(panel);

// in initContent
JPanel infopanel = new JPanel() {

    @Override
    public Dimension getMaximumSize() {
        return getPreferredSize();
    }


};
infopanel.setBorder(BorderFactory.createLineBorder(Color.RED));
infopanel.setAlignmentY(0f);
infopanel.setLayout(new BoxLayout(infopanel, BoxLayout.Y_AXIS));
JPanel listpanel = new JPanel();
listpanel.setBorder(BorderFactory.createLineBorder(Color.BLUE));
listpanel.setAlignmentY(0f);
listpanel.setLayout(new GridLayout(0,1,5,5));

使用它时:删除 all setXXSize-硬编码大小错误,

While you are at it: remove all setXXSize - hard-coding sizes is wrong, some reasons.

这篇关于如何在BoxLayout中粘贴到容器顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆