Luc*_*uke 17 java swing gridbaglayout
是否可以GridBagLayout为整行/列设置边距/填充?我在constraints-object上使用了inset,但是,使用这种方法我需要在每个组件上从底部设置填充.
是否可以填充所有内容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);
Run Code Online (Sandbox Code Playgroud)
我正在使用私有方法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);
}
Run Code Online (Sandbox Code Playgroud)
如何在单元格之间添加一些"空气(填充/边距)"?
小智 41
使用该类的inset属性GridBagConstraints
例如:
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(3,3,3,3);
Run Code Online (Sandbox Code Playgroud)
希望这是你正在寻找的.
据我所知,做第一个的唯一方法是为每个约束设置一个Insets对象......(手工编码布局显然在Java中不容易设计:P)
但是有一种非常简单的方法可以完成第二种操作,而不需要处理任何Insets或其他内容:将contentPane的布局设置为FlowLayout,无论您需要任何垂直和水平间隙,都可以将面板添加到contentPane,而不是添加所有内容到contentPane,将其添加到新面板.
| 归档时间: |
|
| 查看次数: |
29781 次 |
| 最近记录: |