即時通訊使用GridBag在JScrollPane中顯示一些帶有圖像的JPanel。 當GridBagConstraints有3個或更多的圖像時,它可以正常工作,但是當我有1或2時,它們會對齊到JScrollPane的中心,而不是位於頂部的位置(如在圖庫中) 這是我的代碼:GridBagLayout中的JPanel位置不正確
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的部分。 我希望JPanels在他們的位置保持靜態,並且如果有空閒空間,不要對齊。 如果我不清楚,我可以上傳幾個快照。 謝謝!
確保至少一張照片有重量(或更好,他們都同樣重量!= 0),然後用GridBagConstraints.anchor = PAGE_START;或FIRST_LINE_START。 – DSquare