我正在制作一个小的Java swing应用程序,我必须在一个滚动窗格中创建一个带有gridBagLayout的表格。GridBagLayout单元格取整行
首先,GridBagLayout不会在面板顶部对齐。其次,如果我居中(1,2,3,4)单元格,它们居中在面板的中间(不在单元格的中间)。
我在做什么错?
这里是我的代码:
//Folder --------------------------------------------------------
folderPanel.setBackground(Color.WHITE);
folderPanel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridheight = 1;
gbc.gridwidth = 1;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.anchor = GridBagConstraints.NORTHWEST;
JLabel iCell = new JLabel();
JLabel nameCell = new JLabel();
JLabel typeCell = new JLabel();
JLabel dateCell = new JLabel();
iCell.setPreferredSize(new Dimension(50, 60));
iCell.setHorizontalAlignment(SwingConstants.CENTER);
nameCell.setPreferredSize(new Dimension(50, 60));
nameCell.setHorizontalAlignment(SwingConstants.CENTER);
typeCell.setPreferredSize(new Dimension(50, 60));
typeCell.setHorizontalAlignment(SwingConstants.CENTER);
dateCell.setPreferredSize(new Dimension(50, 60));
dateCell.setHorizontalAlignment(SwingConstants.CENTER);
int gridy = 0;
int i = 0;
iCell.setText("#");
gbc.gridx = 0;
gbc.gridy = gridy;
folderPanel.add(iCell, gbc);
//---------------------------------------------
nameCell.setText("Name");
gbc.gridx = 1;
gbc.gridy = gridy;
folderPanel.add(nameCell, gbc);
//---------------------------------------------
typeCell.setText("Type");
gbc.gridx = 2;
gbc.gridy = gridy;
folderPanel.add(typeCell, gbc);
//---------------------------------------------
dateCell.setText("Date");
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.gridx = 3;
gbc.gridy = gridy;
folderPanel.add(dateCell, gbc);
gridy++;
i++;
DBCursor cursor = imagesCollection.find();
try {
while(cursor.hasNext()) {
DBObject obj = cursor.next();
iCell = new JLabel();
nameCell = new JLabel();
typeCell = new JLabel();
dateCell = new JLabel();
iCell.setPreferredSize(new Dimension(50, 60));
nameCell.setPreferredSize(new Dimension(50, 60));
typeCell.setPreferredSize(new Dimension(50, 60));
dateCell.setPreferredSize(new Dimension(50, 60));
iCell.setText(Integer.toString(i));
gbc.gridx = 0;
gbc.gridy = gridy;
folderPanel.add(iCell, gbc);
//---------------------------------------------
nameCell.setText((String) obj.get("name"));
gbc.gridx = 1;
gbc.gridy = gridy;
folderPanel.add(nameCell, gbc);
//---------------------------------------------
typeCell.setText((String) obj.get("type"));
gbc.gridx = 2;
gbc.gridy = gridy;
folderPanel.add(typeCell, gbc);
//---------------------------------------------
DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
dateCell.setText(df.format(obj.get("creationDate")));
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.gridx = 3;
gbc.gridy = gridy;
folderPanel.add(dateCell, gbc);
gridy++;
i++;
}
} finally {
cursor.close();
}
JScrollPane scroller = new JScrollPane(folderPanel);
请考虑发布[MCVE](http://stackoverflow.com/help/mcve)。 – icza 2014-09-04 12:02:27
您忘记了“weightx/weighty”的值吗?或者它是故意的!我们将高度赞赏粗略的绘图(在纸上,通过移动设备/平板电脑/其他设备拍摄快照)以及视图的外观。 – 2014-09-04 12:40:41