我确实有多个JPanel,所有这些都基于它们是三列宽的假设。并且一列或多列高。目前我正在使用GridBagLayout。
我计划在另一个JPanel中将它们添加到彼此之下。问题是,列不排列。
在保持单个JPanel彼此分离的同时,我能做些什么吗?没有回到一些更固定的布局。 我可以给JPanels父容器并添加元素,同时保持JPanel本身为空(当不处于“独立模式”时)。但我认为这种做法很恶心。
感谢您的帮助!
编辑: 我的Layout类看起来像这样:
public class Layout extends GridBagLayout {
public void collocate_line(List<? extends JComponent> compos, final Container container) {
int currentCol = 0;
for (final JComponent compo: compos) {
final GridBagConstraints gbc = new GridBagConstraints(
currentCol, this.currentLine, GRID_WIDTH, GRID_HEIGHT,
DEFAULT_WEIGHT_X, DEFAULT_WEIGHT_Y, GridBagConstraints.CENTER,
GridBagConstraints.BOTH, DEFAULT_INSETS, 0, 0);
container.add(compo, gbc);
currentCol++;
}
currentLine++;
}
}
包含JPanel的内容如下所示:
public class ScopePanel extends JPanel implements ActionListener {
private final ChooseClassPath classPath;
private final ChooseStubsJava stubsJava;
public ScopePanel(final IPreparedCommands exec, final ResourceBundle messages) {
super();
final Layout gbl = new Layout();
this.setLayout(gbl);
this.classPath = new ChooseClassPath(exec, messages, gbl);
this.add(this.classPath, GUIUtil.mkgbc_fillx(0, 0, GridBagConstraints.REMAINDER, 1));
this.stubsJava = new ChooseStubsJava(exec, messages, gbl);
this.add(this.stubsJava, GUIUtil.mkgbc_fillx(0, 2, GridBagConstraints.REMAINDER, 1));
}
}
一个元素看起来像:
public class ChooseClassPath extends JPanel {
public ChooseClassPath(final IPreparedCommands exec, final ResourceBundle messages) {
this(exec, messages, new Layout());
}
public ChooseClassPath(final IPreparedCommands exec, final ResourceBundle messages, final Layout layout) {
super();
{ // Line 1
final List<JComponent> line = new ArrayList<JComponent>();
final JLabel label = new JLabel(messages.getString("label_classpath"));
line.add(label);
//...
layout.collocate_line(line, this);
}
}
}
结果是:
答案 0 :(得分:0)
确保传递与所有JPanel相同的GirdBagConstraints属性集。 esp属性,如gridwidth,fill,weightx等。此外,JPanel中的内容(如果每个JPanel实例的内容不同)可能会影响网格的个别大小。如果GridLayout或任何其他更简单的布局无法满足您的要求,请考虑使用GridLayout并转到GridBagLayout。