2016-06-13 39 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