2015-06-14 340 views
5

所以我試圖做的就是創建這樣的:enter image description here麻煩的GridBagLayout和麪板

我使用的是網格包佈局,這裏是我到目前爲止有:

public class board { 
public static void addComponentsToPane(Container pane) { 
    pane.setLayout(new GridBagLayout()); 
    GridBagConstraints c = new GridBagConstraints(); 

    JPanel leftTop = new JPanel(); 
    leftTop.setPreferredSize(new Dimension(251,300)); 
    leftTop.setBackground(Color.black); 
    c.fill = GridBagConstraints.HORIZONTAL; 
    c.gridx = 0; 
    c.gridy = 0; 

    pane.add(leftTop, c); 

    JPanel middleTop = new JPanel(); 
    middleTop.setPreferredSize(new Dimension(251,200)); 
    middleTop.setBackground(Color.green); 
    c.fill = GridBagConstraints.HORIZONTAL; 
    c.gridx = 1; 
    c.gridy = 0; 

    pane.add(middleTop, c); 

    JPanel rightTop = new JPanel(); 
    rightTop.setPreferredSize(new Dimension(251,600)); 
    rightTop.setBackground(Color.blue); 
    c.fill = GridBagConstraints.HORIZONTAL; 
    c.gridx = 2; 
    c.gridy = 0; 

    pane.add(rightTop, c); 

    JPanel leftBottom = new JPanel(); 
    leftBottom.setPreferredSize(new Dimension(251,300)); 
    leftBottom.setBackground(Color.red); 
    c.fill = GridBagConstraints.HORIZONTAL; 
    c.gridx = 0; 
    c.gridy = 1; 

    pane.add(leftBottom, c); 

    JPanel middleBottom = new JPanel(); 
    middleBottom.setPreferredSize(new Dimension(251,400)); 
    middleBottom.setBackground(Color.yellow); 
    c.fill = GridBagConstraints.HORIZONTAL; 
    c.gridx = 1; 
    c.gridy = 1; 

    pane.add(middleBottom, c); 
} 

private static void createAndShowGUI() { 
    JFrame frame = new JFrame("GridBagLayoutDemo"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    addComponentsToPane(frame.getContentPane()); 

    frame.pack(); 
    frame.setVisible(true); 
} 

public static void main(String[] args) { 
    javax.swing.SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      createAndShowGUI(); 
     } 
    }); 
} 
} 

它創建類似:enter image description here

我該如何推高面板,讓它們像我的第一張照片一樣接觸彼此。我查看了GridBagConstraints,但我找不到任何看起來可以工作的東西。謝謝!

+0

父面板可以調整大小嗎?如果是這樣,你如何期望你的組件增長和縮小 - 所有組件的比例調整大小,還是一些組件保持一個固定的大小,而其他組件的增長和縮小? –

回答

4

與一個佈局管理器試圖解決完整的佈局問題不同,嵌套佈局通常更簡單。例如,你的示例代碼可以修改爲使用水平網格佈局(以保持列的寬度相等 - 實際上我不知道是否要強制這樣做)如果不是,那麼FlowLayoutBoxLayout會更好),以及列使用一個BoxLayout各:

import java.awt.Color; 
import java.awt.Container; 
import java.awt.Dimension; 
import java.awt.GridLayout; 

import javax.swing.BoxLayout; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 

public class board { 
    public static void addComponentsToPane(Container pane) { 
     pane.setLayout(new GridLayout(1, 0)); 

     JPanel left = new JPanel(); 
     pane.add(left); 
     left.setLayout(new BoxLayout(left, BoxLayout.Y_AXIS)); 

     JPanel leftTop = new JPanel(); 
     leftTop.setPreferredSize(new Dimension(125, 150)); 
     leftTop.setBackground(Color.black); 
     left.add(leftTop); 

     JPanel leftBottom = new JPanel(); 
     leftBottom.setPreferredSize(new Dimension(125, 150)); 
     leftBottom.setBackground(Color.red); 
     left.add(leftBottom); 

     JPanel middle = new JPanel(); 
     pane.add(middle); 
     middle.setLayout(new BoxLayout(middle, BoxLayout.Y_AXIS)); 

     JPanel middleTop = new JPanel(); 
     middleTop.setPreferredSize(new Dimension(125, 100)); 
     middleTop.setBackground(Color.green); 
     middle.add(middleTop); 

     JPanel middleBottom = new JPanel(); 
     middleBottom.setPreferredSize(new Dimension(125, 200)); 
     middleBottom.setBackground(Color.yellow); 
     middle.add(middleBottom); 

     JPanel right = new JPanel(); 
     right.setPreferredSize(new Dimension(125, 300)); 
     right.setBackground(Color.blue); 

     pane.add(right); 
    } 

    private static void createAndShowGUI() { 
     JFrame frame = new JFrame("GridBagLayoutDemo"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     addComponentsToPane(frame.getContentPane()); 
     frame.pack(); 
     frame.setVisible(true); 
    } 

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

結果:

Screenshot of the result

(I修飾的優選尺寸有點縮小圖像作爲進一步注意它通常最好override getPreferredSize() rather than use setPreferredSize();有必要對setPreferredSize。 ()雖然方便快速的例子)

+0

對不起,我沒有回覆我睡着了,但非常感謝您的幫助! – user2747058