GridBagLayout Java 中的边距/填充

发布于 2024-10-15 12:27:17 字数 1685 浏览 2 评论 0原文

  1. 是否可以在 GridBagLayout 中为整行/列设置边距/填充?我在约束对象上使用插图,但是使用这种方法我需要在每个组件上从底部设置填充。

  2. 是否可以填充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);
 }

如何在单元格之间添加一些“空气(填充/边距)”?

  1. 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.

  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

腻橙味 2024-10-22 12:27:17

使用 GridBagConstraints 类的 insets 属性

例如:

GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(3,3,3,3);

希望这就是您正在寻找的内容。

Use the insets property of the GridBagConstraints class

For example:

GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(3,3,3,3);

Hope this is what you are looking for.

短暂陪伴 2024-10-22 12:27:17

据我所知,执行第一个操作的唯一方法是为每个约束设置一个 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文