2

我有一个带有 Y_AXIS 布局的父 JPanel。这个容器的子容器也是 JPanel。组件堆叠良好,并且全部对齐,但我希望它们粘在父 JPanel 的顶部,因此所有多余的空间都在底部,并且组件之间没有额外的空间。我试过用胶水,但我可能做错了什么。我还将所有孩子的 AllignmentX 和 AllignmentY 分别设置为 left 和 top。

所以,我想要的是父面板中的一堆孩子,它们的高度没有被拉伸,并且粘在父容器的顶部(如果可能的话,左侧),所有多余的空间都放在底部,因此:

编辑:

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

代码产生的窗口

4

2 回答 2

4

假设包含面板的layoutManager(也就是传入initContent的那个)有一个horizo​​ntalBoxLayout

  • 去除所有胶水
  • 将两个 info/listPanel 的alignmentY设置为顶部
  • 实现 infoPanel 的 getMaximum 以返回其首选项

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

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 - 硬编码大小是错误的,有一些原因

于 2012-07-28T10:36:49.157 回答
1

尝试去除容器顶部的胶水并将其添加到底部。

更改代码:

protected void initContent(JPanel panel) {
       [...] infopanel.add(Box.createVerticalGlue()); [...] infopanel.add(Box.createVerticalGlue()); [...]
       JPanel infopanel = new JPanel();
       JPanel listpanel = new JPanel();
       infopanel.setLayout(new BoxLayout(infopanel, BoxLayout.Y_AXIS));
       
       infopanel.add(makeInfoContainer("Id", id, 50, 100));
       
       infopanel.add(makeInfoContainer("Owner", currentowner, 50, 100));
       
       listpanel.setLayout(new GridLayout(0,1,5,5));
       
}
于 2012-07-28T10:14:46.817 回答