【发布时间】:2013-08-27 06:54:38
【问题描述】:
我使用 GridBag 在 JScrollPane 中显示一些带有图像的 JPanel。 当有 3 个或更多图像时,GridBagConstraints 可以正常工作,但是当我有 1 个或 2 个时,它们会与 JScrollPane 的中心对齐,而不是位于顶部的位置(就像在画廊中一样) 这是我的代码:
JPanel jPanel1 = new JPanel();
GridBagLayout layout = new GridBagLayout();
jPanel1.setLayout(layout);
GridBagConstraints gbc = new GridBagConstraints();
JPanel photo = new JPanel();
Dimension d = new Dimension(100,100);
photo.setPreferredSize(d);
gbc.insets = new Insets(0,3,3,3);
gbc.gridx = i;
gbc.gridy = j;
jPanel1.add(photo, gbc);
jScrollPane1.setViewportView(jPanel1);
我省略了将图像分配给照片 Jpanel 的部分。 我希望 JPanel 在它们的位置保持静止,并且如果有可用空间则不对齐。 如果我不清楚,我可以上传一些快照。 谢谢!
【问题讨论】:
-
确保至少一张照片有重量(或者更好,所有照片的重量都相同!= 0),然后使用 GridBagConstraints.anchor = PAGE_START;或 FIRST_LINE_START。
标签: java swing jpanel gridbaglayout scrollpane