如何坚守在BoxLayout的容器的顶部(How to stick to top of contai

2019-09-20 08:30发布

我有一个Y_AXIS布局父的JPanel。 此容器的孩子们都JPanels为好。 该组件是堆叠的罚款,并都神韵:,但我希望他们能坚持到父的JPanel所以所有多余的空间是在底部的顶部,并且在组件之间没有多余的空间。 我一直在使用胶水试过,但我可能会做一些错误。 我还设置AllignmentX和AllignmentY分别为左,顶部的所有儿童。

所以,我想是在父面板儿堆,未在高度拉伸,并坚持到顶部(离开,如果可能的话)父容器,放置在底部的所有多余空间,因此:

编辑:

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;
}

Answer 1:

假设含面板的LayoutManager的(即所述一个传递到initContent)具有horizo​​ntalBoxLayout

  • 删除所有的胶料
  • 同时设置信息的alignmentY / listPanel顶部
  • 落实信息面板的getMaximum返回其PREF

喜欢的东西(的边界只是想象哪个容器是在哪里,往往很难有这么多的嵌套来决定:)

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));

当你在这:删除所有 setXXSize -硬编码的大小是错误的, 有些原因 。



Answer 2:

尝试在容器的顶部取出胶水,将它添加到底部来代替。

改变的代码:

protected void initContent(JPanel panel) {
       [...]
       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("Owner", currentowner, 50, 100));
       infopanel.add(Box.createVerticalGlue());
       listpanel.setLayout(new GridLayout(0,1,5,5));
       [...]
}


文章来源: How to stick to top of container in BoxLayout