Java GridLayout布局panel

牛啦你不走 2012-12-15 10:45:16
布局一个panel,部分源码如下:
private JPanel getDataFieldsPanel(ArrayList<DataField> dataFields) {


// create panel to hold labels and textFields
JPanel displayPanel = new JPanel();

//set the layout of the display panel

displayPanel.setLayout(new GridLayout(dataFields.size(), 2, 0, 0));
//displayPanel.makeColumnsEqualWidth = false;

// create the components according to the length of dataFields
for(DataField element : dataFields) {

JLabel nameLabel = new JLabel(element.getName() + ":");
displayPanel.add(BorderLayout.WEST, nameLabel);

JTextField valueField = new JTextField(element.getValue(), 10);
valueField.setEditable(false);
displayPanel.add(valueField);
}

return displayPanel;

}
效果如图,

结果是两列组件的等宽,问题就是两列内容离得太远了,想靠近一点儿,怎么解决?
...全文
285 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
牛啦你不走 2012-12-15
  • 打赏
  • 举报
回复
yongger520 2012-12-15
  • 打赏
  • 举报
回复
SWT 中 FormLayout 才是王道 Swing不知道有没有
lxq2061971 2012-12-15
  • 打赏
  • 举报
回复
得用GridBagLayout分布吧,Gridlayout是等宽的

62,621

社区成员

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

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