我有一个带有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;
}
答案 0 :(得分:4)
假设包含面板的layoutManager(即传递给initContent的面板)具有horizontalBoxLayout
类似的东西(边框只是为了可视化哪个容器在哪里,通常难以用如此多的嵌套来决定:)
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 - 硬编码大小错误,some reasons。
答案 1 :(得分:1)
尝试取下容器顶部的胶水,然后将其添加到底部。
更改了代码:
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));
[...]}