GridBagConstraints的布局问题?

jincheng2008 2009-07-13 07:07:12
大家晚上好,小弟写了个GridBagConstraints布局不知道报错:
Exception :
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: cannot add to layout: constraint must be a string (or null)

布局封装的方法体:
private void setupComponet(JComponent component, int gridx, int gridy,
int gridwidth, int ipadx, boolean fill) {
final GridBagConstraints constraint = new GridBagConstraints();

constraint.gridx = gridx;
constraint.gridy = gridy;
if (gridwidth > 1) {
constraint.gridwidth = gridwidth;
}
if (ipadx > 0) {
constraint.ipadx = ipadx;
}
//Insets(int top, int left, int bottom, int right)
//创建并初始化具有指定顶部、左边、底部、右边 inset 的新 Insets 对象
constraint.insets = new Insets(0, 10, 0, 10);
if (fill) {
constraint.fill = GridBagConstraints.HORIZONTAL;
}
System.out.println(constraint.gridy+""+constraint.gridx);
listFrame.add(component, constraint);
}

组件调用:
JPanel panel2 = new JPanel();
panel2.setLayout(new GridBagLayout());

setupComponet(new JLabel("过滤:"), 2, 0, 1, 1, false);-------此步调用封装的方法体,这步出错,

final JTextField filterText = new JTextField();
setupComponet(filterText, 3, 0, 1, 150, true);
listFrame.add(panel2,new BorderLayout().NORTH);
...全文
260 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
closewbq 2009-07-15
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 closewbq 的回复:]
这是由于你传的是JLabel,应该是没有组件约束的,你 listFrame.add(component, null);就可以
[/Quote]
不好意思,JLabel是有组件约束的。

setupComponet(new JLabel("过滤:"), 2, 0, 1, 1, false);-------此步调用封装的方法体,这步出错, 是因为在
setupComponet方法中 listFrame.add(component, constraint); 出的问题,constraint是依赖于GirdBagLayout布局的,因为你的listFrame没有使用GirdBagLayout布局才造成的吧
closewbq 2009-07-13
  • 打赏
  • 举报
回复
这是由于你传的是JLabel,应该是没有组件约束的,你 listFrame.add(component, null);就可以

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧