2013-08-27 64 views
0

即時通訊使用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

確保至少一張照片有重量(或更好,他們都同樣重量!= 0),然後用GridBagConstraints.anchor = PAGE_START;或FIRST_LINE_START。 – DSquare

回答

2

GridBagLayout將其組件排列在容器的中心附近,這是它的(有時是煩人的)默認功能。

你可以嘗試在以下容器中的其他組件的位置向右增加與weightx=1weighty=1GridBagConstraints一個空的「填充物」部件(比如一個JLabel)。這將迫使他們到容器的頂部/左上角...

更新...

中心/默認...

enter image description here

import java.awt.BorderLayout; 
import java.awt.EventQueue; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.io.File; 
import java.io.IOException; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.imageio.ImageIO; 
import javax.swing.ImageIcon; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class GridBagLayoutTest { 

    public static void main(String[] args) { 
     new GridBagLayoutTest(); 
    } 

    public GridBagLayoutTest() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 
       frame.add(new TestPane()); 
       frame.setSize(600, 600); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class TestPane extends JPanel { 

     public TestPane() { 
      setLayout(new GridBagLayout()); 
      GridBagConstraints gbc = new GridBagConstraints(); 
      gbc.gridx = 0; 
      gbc.gridy = 0; 

      JLabel picture = new JLabel(); 
      try { 
       picture.setIcon(new ImageIcon(ImageIO.read(new File("/path/to/your/picture")))); 
      } catch (IOException ex) { 
       ex.printStackTrace(); 
       picture.setText("Bad picture"); 
      } 
      add(picture, gbc); 
     } 
    }   
} 

對齊...

enter image description here

import java.awt.BorderLayout; 
import java.awt.EventQueue; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.io.File; 
import java.io.IOException; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.imageio.ImageIO; 
import javax.swing.ImageIcon; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class GridBagLayoutTest { 

    public static void main(String[] args) { 
     new GridBagLayoutTest(); 
    } 

    public GridBagLayoutTest() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 
       frame.add(new TestPane()); 
       frame.setSize(600, 600); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class TestPane extends JPanel { 

     public TestPane() { 
      setLayout(new GridBagLayout()); 
      GridBagConstraints gbc = new GridBagConstraints(); 
      gbc.gridx = 0; 
      gbc.gridy = 0; 

      JLabel picture = new JLabel(); 
      try { 
       picture.setIcon(new ImageIcon(ImageIO.read(new File("/path/to/your/picture")))); 
      } catch (IOException ex) { 
       ex.printStackTrace(); 
       picture.setText("Bad picture"); 
      } 
      add(picture, gbc); 

      JLabel filler = new JLabel(); 
      gbc.gridx = 1; 
      gbc.gridy = 1; 
      gbc.weightx = 1; 
      gbc.weighty = 1; 
      add(filler, gbc); 
     } 
    }   
} 
+0

我不能讓它工作.. 另一個問題是,即時動態添加JPanels。 – user2712751

+0

不應該有任何區別(動態添加JPanel)。您可以簡單地移除填充物,添加任何新面板並將填充物放置在最後位置(到右側/底部)的位置 – MadProgrammer