2014-09-04 75 views
1

我正在制作一个小的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); 
+2

请考虑发布[MCVE](http://stackoverflow.com/help/mcve)。 – icza 2014-09-04 12:02:27

+2

您忘记了“weightx/weighty”的值吗?或者它是故意的!我们将高度赞赏粗略的绘图(在纸上,通过移动设备/平板电脑/其他设备拍摄快照)以及视图的外观。 – 2014-09-04 12:40:41

回答

2
//Folder -------------------------------------------------------- 
folderPanel.setBackground(Color.WHITE); 
folderPanel.setLayout(new GridBagLayout()); 
GridBagConstraints gbc = new GridBagConstraints(); 
gbc.gridheight = 1; 
gbc.gridwidth = 1; 
gbc.weightx = 1; 
gbc.weighty = 0; ///////////////////////////// added 
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); 

gbc.weighty = 1;  //////////////////////// added 
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); 
1

我解决了我走的是整行的单元格的问题:

我不得不添加的GBC gridheight和gridwidth每次我设置一个新行:

gbc.gridheight = 1; 
gbc.gridwidth = 1; 

代码:

//Folder ----------------------`---------------------------------- 
    folderPanel.setBackground(Color.WHITE); 
    folderPanel.setLayout(new GridBagLayout()); 
    GridBagConstraints gbc = new GridBagConstraints(); 
    gbc.gridheight = 1; 
    gbc.gridwidth = 1; 
    gbc.weightx = 1; 
    gbc.weighty = 0; 
    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); 

    gbc.weighty = 1; 
    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)); 
      MatteBorder border = new MatteBorder(1, 
        0, 
        0, 
        0, 
        Color.GRAY); 
      iCell.setBorder(border); 
      nameCell.setBorder(border); 
      typeCell.setBorder(border); 
      dateCell.setBorder(border); 
      gbc.gridheight = 1; //////////// ADDED 
      gbc.gridwidth = 1; //////////// ADDED 

      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); 
    scroller.setViewportBorder(new MatteBorder(10, 10, 10, 10, Color.WHITE)); 


    //Setting -------------------------------------------------------- 
    address.setFont(textFieldPolice); 
    address.setPreferredSize(new Dimension(150, 30)); 
    address.setForeground(Color.BLACK); 
    address.setText(setting.getAddress()); 
    password.setFont(textFieldPolice); 
    password.setPreferredSize(new Dimension(150, 30)); 
    password.setForeground(Color.BLACK); 
    password.setText(setting.getPassword()); 

    GridLayout glSetting = new GridLayout(); 
    glSetting.setColumns(1); 
    glSetting.setRows(3); 
    glSetting.setHgap(10); 
    glSetting.setVgap(10); 
    settingForm.setLayout(glSetting); 
    settingForm.add(addressLabel); 
    settingForm.add(address); 
    settingForm.add(passwordLabel); 
    settingForm.add(password); 
    settingForm.add(saveButton); 

    GridBagConstraints gbcSetting = new GridBagConstraints(); 
    gbcSetting.weightx = 1; 
    gbcSetting.weighty = 1; 
    gbcSetting.anchor = GridBagConstraints.NORTHWEST; 

    gbcSetting.gridx = 0; 
    gbcSetting.gridy = 0; 
    settingPanel.setLayout(new GridBagLayout()); 
    settingPanel.add(settingForm, gbcSetting); 
    JPanel settingPaddingPanel = new JPanel(); 
    settingPaddingPanel.setLayout(new FlowLayout(FlowLayout.LEADING, 5, 5)); 
    settingPaddingPanel.setBackground(Color.WHITE); 
    settingPaddingPanel.add(settingPanel); 

    //Main ------------------------------------------------------------ 
    pan.setLayout(cl); 
    pan.add(scroller, "folderPanel"); 
    pan.add(latestPanel, "latestPanel"); 
    pan.add(settingPaddingPanel, "settingPanel"); 

    this.getContentPane().add(header, BorderLayout.NORTH); 
    this.getContentPane().add(menu, BorderLayout.WEST); 
    this.getContentPane().add(pan, BorderLayout.CENTER); 

    this.setVisible(true); 
} 
+0

IHMO,当您使用'Layout Manager'时,请停止使用'setXxXSize()'方法,因为计算'JComponent'大小的部分属于布局问题。 – 2014-09-05 01:33:29

+1

这里这个[示例](http://stackoverflow.com/a/17919032/1057230),包含了一些关于'weightx/weighty' thingy的解释。希望它会帮助你在你的努力,以及:-) – 2014-09-05 01:40:20

+1

请看看这个,在[这](http://stackoverflow.com/a/17640318/1057230)的另一种解释。希望能帮助到你 :-) – 2014-09-05 17:27:33