2016-06-13 36 views
0

伙计!我怎样才能正确放置椅子?在下面的图片的第二帧中,我张贴了我想要放置这些对象的圈子。 enter image description here如何用GridBagLayout填充BOTH和CENTER面板?

我已经使用了4个BorderLayouts放置标签和图像,以便不给GridBagLayout的过于复杂,然后我将它们添加到GridBagLayout的是这样的:

c.fill = GridBagConstraints.BOTH/*PAGE_START*/; 
    c.weightx = 10; 
    c.weighty = 10; 
    c.gridx = 1; 
    c.gridy = 0; 
    panel.add(topPlayerPanel, c); 

    c.gridx = 0; 
    c.gridy = 1; 
    panel.add(leftPlayerPanel, c); 

    c.gridx = 1; 
    c.gridy = 1; 
    panel.add(t, c); 

    c.gridx = 2; 
    c.gridy = 1; 
    panel.add(rightPlayerPanel, c); 

    c.gridx = 1; 
    c.gridy = 2; 
    panel.add(bottomPlayerPanel, c); 

如果我把他们带PAGE_START,LINE_START,LINE_END,PAGE_END,展示位置是正确的。不过,这并不表明我的整体形象:(

+0

为了尽快得到答案,请在提问时提出问题[mcve](http://stackoverflow.com/help/mcve)。 – predi

回答

0

您可以使用占用多余的空间GridBagLayout看不见的组件和“推”的其他组件周围。我用Box.Filler那些计算机图标推到表板。

table with chairs

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import javax.swing.BorderFactory; 
import javax.swing.Box; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 
import javax.swing.UIManager; 

public class GBLTableWithChairs extends JFrame { 

    public GBLTableWithChairs() { 
     setTitle("Table with chairs"); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     setLayout(new GridBagLayout()); 

     GridBagConstraints gbc; 
     Box.Filler filler; 
     JLabel label; 

     JPanel table = new JPanel(new BorderLayout()); 
     table.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.GRAY)); 
     table.add(new JLabel("<html>TABLE&nbsp;TABLE<br/>TABLE&nbsp;TABLE<br/>TABLE&nbsp;TABLE")); 
     gbc = new GridBagConstraints(); 
     gbc.gridx = 1; 
     gbc.gridy = 1; 
     add(table, gbc); // to frame 

     JPanel leftChair = new JPanel(new GridBagLayout()); 
     filler = createHorizontalFiller(); 
     gbc = new GridBagConstraints(); 
     gbc.gridx = 0; 
     gbc.gridy = 0; 
     gbc.fill = GridBagConstraints.HORIZONTAL; 
     gbc.weightx = 1.0d; 
     leftChair.add(filler, gbc); // to leftChair 
     label = new JLabel(UIManager.getIcon("FileView.computerIcon")); 
     gbc = new GridBagConstraints(); 
     gbc.gridx = 1; 
     gbc.gridy = 0; 
     leftChair.add(label, gbc); // to leftChair 
     gbc = new GridBagConstraints(); 
     gbc.gridx = 0; 
     gbc.gridy = 1; 
     add(leftChair, gbc); // to frame 

     JPanel topChair = new JPanel(new GridBagLayout()); 
     filler = createVerticalFiller(); 
     gbc = new GridBagConstraints(); 
     gbc.gridx = 0; 
     gbc.gridy = 0; 
     gbc.fill = GridBagConstraints.VERTICAL; 
     gbc.weighty = 1.0d; 
     topChair.add(filler, gbc); // to topChair 
     label = new JLabel(UIManager.getIcon("FileView.computerIcon")); 
     gbc = new GridBagConstraints(); 
     gbc.gridx = 0; 
     gbc.gridy = 1; 
     topChair.add(label, gbc); // to topChair 
     gbc = new GridBagConstraints(); 
     gbc.gridx = 1; 
     gbc.gridy = 0; 
     add(topChair, gbc); // to frame 

     JPanel rightChair = new JPanel(new GridBagLayout()); 
     filler = createHorizontalFiller(); 
     gbc = new GridBagConstraints(); 
     gbc.gridx = 1; 
     gbc.gridy = 0; 
     gbc.fill = GridBagConstraints.HORIZONTAL; 
     gbc.weightx = 1.0d; 
     rightChair.add(filler, gbc); // to rightChair 
     label = new JLabel(UIManager.getIcon("FileView.computerIcon")); 
     gbc = new GridBagConstraints(); 
     gbc.gridx = 0; 
     gbc.gridy = 0; 
     rightChair.add(label, gbc); // to rightChair 
     gbc = new GridBagConstraints(); 
     gbc.gridx = 2; 
     gbc.gridy = 1; 
     add(rightChair, gbc); // to frame 

     JPanel bottomChair = new JPanel(new GridBagLayout()); 
     filler = createVerticalFiller(); 
     gbc = new GridBagConstraints(); 
     gbc.gridx = 0; 
     gbc.gridy = 1; 
     gbc.fill = GridBagConstraints.VERTICAL; 
     gbc.weighty = 1.0d; 
     bottomChair.add(filler, gbc); // to bottomChair 
     label = new JLabel(UIManager.getIcon("FileView.computerIcon")); 
     gbc = new GridBagConstraints(); 
     gbc.gridx = 0; 
     gbc.gridy = 0; 
     bottomChair.add(label, gbc); // to bottomChair 
     gbc = new GridBagConstraints(); 
     gbc.gridx = 1; 
     gbc.gridy = 2; 
     add(bottomChair, gbc); // to frame 

     setSize(200, 200); 
     setLocationRelativeTo(null); 
    } 

    private Box.Filler createHorizontalFiller() { 
     return new Box.Filler(new Dimension(0, 0), new Dimension(0, 0), new Dimension(32767, 0)); 
    } 

    private Box.Filler createVerticalFiller() { 
     return new Box.Filler(new Dimension(0, 0), new Dimension(0, 0), new Dimension(0, 32767)); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       new GBLTableWithChairs().setVisible(true); 
      } 
     }); 
    } 

} 

这应该让你开始

+0

非常感谢,真的!但我还有一个问题......我在下面添加了更多细节 – libburst

0

非常感谢我使用的填料rewrited我的代码,但我有这个问题:!

例如为左椅子,我放置填料,然后将leftChairPanel(用BorderLayout.NORTH一个JLabel一个BorderLayout的它conists,并在BorderLayout.CENTER图像),但我得到这...也许我使用了错误的GridBagConstraints填充?

JPanel table = new JPanel(new BorderLayout()); 
    table.add(t); 
    gbc = new GridBagConstraints(); 
    gbc.gridx = 1; 
    gbc.gridy = 1; 
    panel.add(table, gbc); 

    JPanel leftChair = new JPanel(new GridBagLayout()); 
    filler = createHorizontalFiller(); 
    gbc.gridx = 0; 
    gbc.gridy = 0; 
    gbc.fill = GridBagConstraints.VERTICAL; 
    gbc.weightx = 1.0d; 
    leftChair.add(filler, gbc); //alla sedia di sinistra 
    gbc = new GridBagConstraints(); 
    gbc.gridx = 1; 
    gbc.gridy = 0; 
    leftChair.add(leftPlayerPanel, gbc); 
    gbc = new GridBagConstraints(); 
    gbc.gridx = 0; 
    gbc.gridy = 1; 
    panel.add(leftChair, gbc); //al mainPanel 

    JPanel topChair = new JPanel(new GridBagLayout()); 
    filler = createVerticalFiller(); 
    gbc = new GridBagConstraints(); 
    gbc.gridx = 0; 
    gbc.gridy = 0; 
    gbc.fill = GridBagConstraints.VERTICAL; 
    gbc.weighty = 1.0d; 
    topChair.add(filler, gbc); //alla sedia in alto 
    gbc = new GridBagConstraints(); 
    gbc.gridx = 0; 
    gbc.gridy = 1; 
    topChair.add(topPlayerPanel, gbc); 
    gbc = new GridBagConstraints(); 
    gbc.gridx = 1; 
    gbc.gridy = 0; 
    panel.add(topChair, gbc); //al panel 

    JPanel rightChair = new JPanel(new GridBagLayout()); 
    filler = createHorizontalFiller(); 
    gbc = new GridBagConstraints(); 
    gbc.gridx = 1; 
    gbc.gridy = 0; 
    gbc.fill = GridBagConstraints.HORIZONTAL; 
    gbc.weightx = 1.0d; 
    rightChair.add(filler, gbc); //alla sedia di destra 
    gbc = new GridBagConstraints(); 
    gbc.gridx = 0; 
    gbc.gridy = 0; 
    rightChair.add(rightPlayerPanel, gbc); 
    gbc = new GridBagConstraints(); 
    gbc.gridx = 2; 
    gbc.gridy = 1; 
    panel.add(rightChair, gbc); //al panel 

    JPanel bottomChair = new JPanel(new GridBagLayout()); 
    filler = createVerticalFiller(); 
    gbc = new GridBagConstraints(); 
    gbc.gridx = 0; 
    gbc.gridy = 1; 
    gbc.fill = GridBagConstraints.VERTICAL; 
    gbc.weighty = 1.0d; 
    bottomChair.add(filler, gbc); //alla sedia in basso 
    gbc = new GridBagConstraints(); 
    gbc.gridx = 0; 
    gbc.gridy = 0; 
    bottomChair.add(bottomPlayerPanel, gbc); 
    gbc = new GridBagConstraints(); 
    gbc.gridx = 1; 
    gbc.gridy = 2; 
    panel.add(bottomChair, gbc); //al panel