简体   繁体   English

GridBagLayout Java中的边距/填充

[英]Margin/padding in GridBagLayout Java

  1. Is it possible to set an margin/padding in GridBagLayout for the whole row/column? 是否可以在整个行/列的GridBagLayout设置边距/填充? I use the inset on the constraints-object however, using this approach I need to set padding from bottom on every single component. 我在constraints-object上使用了inset,但是,使用这种方法我需要在每个组件上从底部设置填充。

  2. Is it possible to pad all of the content in the JFrame ? 是否可以填充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: 我正在使用私有方法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? 如何在单元格之间添加一些“空气(填充/边距)”?

Use the inset property of the GridBagConstraints class 使用GridBagConstraints类的inset属性

For example: 例如:

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

Hope this is what you are looking for. 希望这是你正在寻找的。

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 ) 据我所知,做第一个的唯一方法为每个约束设置一个Insets对象......(手工编码布局显然在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. 但是有一种非常简单的方法可以完成第二种操作,而不需要处理任何Insets或其他内容:将contentPane的布局设置为FlowLayout,无论您需要任何垂直和水平间隙,都可以将面板添加到contentPane,而不是添加所有内容到contentPane,将其添加到新面板。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM