GridBagLayout Java 中的边距/填充
是否可以在 GridBagLayout 中为整行/列设置边距/填充?我在约束对象上使用插图,但是使用这种方法我需要在每个组件上从底部设置填充。
是否可以填充
JFrame
中的所有内容?因为现在每个组件都与框架对齐。
constraints.weightx = 2;
constraints.weighty = 1;
constraints.fill = GridBagConstraints.BOTH;
addComponent(this, layout, constraints, questionPanel = new QuestionPanel(), 0, 0, 1, 1);
constraints.weightx = 1;
constraints.weighty = 1;
constraints.fill = GridBagConstraints.BOTH;
addComponent(this, layout, constraints, categoryPanel = new CategoryPanel(), 0, 1, 1, 1);
constraints.weightx = 1;
constraints.weighty = 0;
constraints.fill = GridBagConstraints.HORIZONTAL;
addComponent(this, layout, constraints, answerPanel = new AnswerPanel(), 1, 0, 2, 1);
constraints.weightx = 1;
constraints.weighty = 2;
constraints.fill = GridBagConstraints.BOTH;
addComponent(this, layout, constraints, tabPane = new JTabbedPane(), 2, 0, 2, 1);
constraints.weightx = 1;
constraints.weighty = 0;
constraints.fill = GridBagConstraints.NONE;
constraints.anchor = GridBagConstraints.SOUTHEAST;
addComponent(this, layout, constraints, buttonPanel = new ButtonPanel(), 3, 1, 1, 1);
我正在使用私有方法 addComponent:
private void addComponent(Container context, GridBagLayout _layout, GridBagConstraints _constraints, Component component, int row, int column, int width, int height) {
_constraints.gridx = column;
_constraints.gridy = row;
_constraints.gridwidth = width;
_constraints.gridheight = height;
_layout.setConstraints(component, _constraints);
context.add(component);
}
如何在单元格之间添加一些“空气(填充/边距)”?
Is it possible to set an margin/padding in
GridBagLayout
for the whole row/column? I use the inset on the constraints-object however, using this approach I need to set padding from bottom on every single component.Is it possible to pad all of the content in the
JFrame
? Because now every component is aligned with the frame.
constraints.weightx = 2;
constraints.weighty = 1;
constraints.fill = GridBagConstraints.BOTH;
addComponent(this, layout, constraints, questionPanel = new QuestionPanel(), 0, 0, 1, 1);
constraints.weightx = 1;
constraints.weighty = 1;
constraints.fill = GridBagConstraints.BOTH;
addComponent(this, layout, constraints, categoryPanel = new CategoryPanel(), 0, 1, 1, 1);
constraints.weightx = 1;
constraints.weighty = 0;
constraints.fill = GridBagConstraints.HORIZONTAL;
addComponent(this, layout, constraints, answerPanel = new AnswerPanel(), 1, 0, 2, 1);
constraints.weightx = 1;
constraints.weighty = 2;
constraints.fill = GridBagConstraints.BOTH;
addComponent(this, layout, constraints, tabPane = new JTabbedPane(), 2, 0, 2, 1);
constraints.weightx = 1;
constraints.weighty = 0;
constraints.fill = GridBagConstraints.NONE;
constraints.anchor = GridBagConstraints.SOUTHEAST;
addComponent(this, layout, constraints, buttonPanel = new ButtonPanel(), 3, 1, 1, 1);
I am using a private method addComponent:
private void addComponent(Container context, GridBagLayout _layout, GridBagConstraints _constraints, Component component, int row, int column, int width, int height) {
_constraints.gridx = column;
_constraints.gridy = row;
_constraints.gridwidth = width;
_constraints.gridheight = height;
_layout.setConstraints(component, _constraints);
context.add(component);
}
How can I add some "air(padding/margin)" between the cells?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
GridBagConstraints
类的insets
属性例如:
希望这就是您正在寻找的内容。
Use the
insets
property of theGridBagConstraints
classFor example:
Hope this is what you are looking for.
据我所知,执行第一个操作的唯一方法是为每个约束设置一个 Insets 对象...(在 Java 中,手动编码布局显然并不容易:P )
但是有一种非常简单的方法可以实现第二个,无需弄乱任何 Insets 或任何东西:将 contentPane 的布局设置为 FlowLayout ,并具有您想要的任何垂直和水平间隙,向 contentPane 添加一个面板,而不是添加将所有内容添加到 contentPane 中,将其添加到您的新面板中。
As far as I know, the only way of doing the first one is to set an Insets object for each constraint... (Hand-coding layout clearly wasn't designed to be easy in Java :P )
But there's a very simple way of doing the second, without messing around with any Insets or anything: set the layout of the contentPane to a FlowLayout with whatever vertical and horizontal gaps you want, add a panel to the contentPane, and instead of adding everything to the contentPane, add it to your new panel.