2013-06-28 73 views
3

我是GridBagLayout的新手,但我嘗試使用標準約定,我可以找到哪一個是在一張網格紙上繪製我的想法,然後嘗試將網格值轉換爲網格包...GridBagLayout無法獲得佈局正確

我的目標是使佈局像你見下文:

Desired layout

它目前看起來是這樣的:

Actual layout

任何想法是爲什麼?

精確的尺寸我在找,如果你認爲一個網格與左上角是0,0 爲

  1. 在目標圖像面板紅色爲:開始在列0,跨度爲10列,高度爲1行
  2. 對於黑色面板:從第0列開始,第1行,跨度爲10列,高度爲20行
  3. 對於藍色面板:開始於第0列,第21行,第10行列,高度爲1
  4. 綠色列:從第10列第0行開始,跨度16列,高7
  5. 爲紫色柱:開始於第10欄,第7行,跨度16列,與高度16

這裏是我的源代碼:

GBC是一個幫手延伸的GridBagConstraints類,所使用的構造方法是

GBC(INT startingX,INT startingY,INT寬度,INT高度)

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 

import java.awt.Color; 
import java.awt.EventQueue; 
import java.awt.GridBagLayout; 
import java.awt.Rectangle; 
import javax.swing.JApplet; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 

public class Demo extends JApplet 
{ 

    JPanel panel1 = new JPanel(); 
    JPanel panel2 = new JPanel(); 
    JPanel panel3 = new JPanel(); 
    JPanel panel4 = new JPanel(); 
    JPanel panel5 = new JPanel(); 

    public void init() 
    { 
     EventQueue.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       initComponents(); 
      } 
     }); 
    } 

    public void initComponents() 
    { 
     //set the layout of the content pane to gridbag layout 
     GridBagLayout gridBag = new GridBagLayout(); 
     getContentPane().setLayout(gridBag); 

     Rectangle rect = getContentPane().getBounds(); 

     panel1.setBackground(Color.green); 
     panel2.setBackground(Color.black); 
     panel3.setBackground(Color.red); 
     panel4.setBackground(Color.orange); 
     panel5.setBackground(Color.yellow); 

     add(panel4, new GBC(10, 0, 16, 7).setFill(GBC.BOTH).setWeight(1.0, 1.0)); 
     add(panel1, new GBC(0, 0, 10, 1).setFill(GBC.BOTH).setWeight(1.0, 1.0)); 
     add(panel3, new GBC(0, 21, 10, 1).setFill(GBC.BOTH).setWeight(1.0, 1.0)); 
     add(panel2, new GBC(0, 1, 10, 20).setFill(GBC.BOTH).setWeight(1.0, 1.0)); 
     add(panel5, new GBC(10, 7, 16, 16).setFill(GBC.BOTH).setWeight(1.0, 1.0)); 
    } 
} 

任何幫助竟被d謝謝(但請解釋你的邏輯)

+1

GridBagLayout在默認情況下不會使列的大小相等。根據放置在主JPanel上的Swing組件的內容來設置列的大小。您應該首先創建5個JPanel中的每一個的內容,然後嘗試使用GridBagLayout將它們組合在主JPanel上。 –

+0

好吧,對抗直觀的xD,你會認爲你想先設計佈局。無論如何,謝謝你,我會試一試。 – nachos

+0

@GilbertLeBlanc它仍然看起來完全相同後,添加組件:(任何想法爲什麼這些行和列值不起作用? – nachos

回答

4

那麼你可以很容易地通過使用GridBagLayout來實現這一點。

考慮到contentPaneGridBagLayout作爲它的佈局,您可以將JPanel分爲三部分。一個單一的左側JPanel(使用GridBagLayout的,並且其將包含那些REDBLACKBLUEJPanel S),和右側包括兩個JPanel S,即GREENMAGENTA

------------------------ 
| left | green JPanel | 
| JPanel |______________| 
| with |magenta Jpanel| 
|3 JPanel |    | 
|________________________| 

現在左JPanel將放置REDBLACKBLUE本身JPanel,與GridBagLayoutLayout Manager具有RED and BLUEweighty = 0.1,並具有BLACKweighty = 0.8

請參見本示例代碼:

import java.awt.*; 
import javax.swing.*; 

public class GridBagExample { 
    private JPanel leftPanel; 
    private JPanel redPanel; 
    private JPanel blackPanel; 
    private JPanel bluePanel; 
    private JPanel greenPanel; 
    private JPanel magentaPanel; 

    private GridBagConstraints gbc; 

    public GridBagExample() { 
     gbc = new GridBagConstraints(); 
     gbc.anchor = GridBagConstraints.FIRST_LINE_START; 
    } 

    private void displayGUI() { 
     JFrame frame = new JFrame("GridBagLayout Example"); 
     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 

     JPanel contentPane = new JPanel(new GridBagLayout()); 

     leftPanel = getPanel(Color.WHITE); 
     leftPanel.setLayout(new GridBagLayout()); 
     redPanel = getPanel(Color.RED.darker()); 
     blackPanel = getPanel(Color.BLACK); 
     bluePanel = getPanel(Color.CYAN.darker().darker()); 
     greenPanel = getPanel(Color.GREEN.darker().darker()); 
     magentaPanel = getPanel(Color.MAGENTA); 
     /** 
      * @param : 
      * leftPanel : JPanel (with GridBagLayout), on which 
      *    all other components will be placed. 
      * redPanel : JPanel, which will be added to the leftPanel 
      * 0 : specifies the grid X, which in this case is 0 
      * 0 : specifies the grid Y, which in this case is 0 
      * 1 : specifies the width for this grid (cell), we keeping 
      *  this default as 1 
      * 1 : specifies the height for this grid (cell), we keeping 
      *  this default as 1 
      * GridBagConstraints.BOTH : allows JPanel to expand in both 
      *  directions as the containing container expands (in 
      *  this case redPanel will expand both HORIZONTALLY and 
      *  VERTICALLY, as leftPanel will expand) 
      * weightx : This is the actual width the redPanel will occupy 
      *   relative to all other components on the leftPanel 
      * weighty : This is the actual height the redPanel will occupy 
      *   relative to all other components on the leftPanel 
      */ 
     addComp(leftPanel, redPanel, 0, 0, 1, 1, 
         GridBagConstraints.BOTH, 1.0, 0.1); 
     addComp(leftPanel, blackPanel, 0, 1, 1, 1, 
         GridBagConstraints.BOTH, 1.0, 0.8); 
     addComp(leftPanel, bluePanel, 0, 2, 1, 1, 
         GridBagConstraints.BOTH, 1.0, 0.1); 
     addComp(contentPane, leftPanel, 0, 0, 1, 2, 
         GridBagConstraints.BOTH, 0.5, 1.0); 
     addComp(contentPane, greenPanel, 1, 0, 1, 1, 
         GridBagConstraints.BOTH, 0.5, 0.3); 
     addComp(contentPane, magentaPanel, 1, 1, 1, 1, 
         GridBagConstraints.BOTH, 0.5, 0.7); 

     frame.setContentPane(contentPane); 
     /* 
     * Once you will add components to these 
     * JPanels, then use pack(), instead of 
     * setSize(). The use of the latter is 
     * just for illustration purpose only 
     */ 
     //frame.pack(); 
     frame.setSize(300, 300); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    private void addComp(JPanel panel, JComponent comp, 
          int x, int y, int width, int height, 
          int fill, double weightx, double weighty) { 
     gbc.gridx = x; 
     gbc.gridy = y; 
     gbc.gridwidth = width; 
     gbc.gridheight = height; 
     gbc.fill = fill; 
     gbc.weightx = weightx; 
     gbc.weighty = weighty; 

     panel.add(comp, gbc); 
    } 

    private JPanel getPanel(Color backColor) { 
     JPanel panel = new JPanel(); 
     panel.setOpaque(true); 
     panel.setBackground(backColor); 

     return panel; 
    } 

    public static void main(String[] args) { 
     Runnable runnable = new Runnable() { 
      @Override 
      public void run() { 
       new GridBagExample().displayGUI(); 
      } 
     }; 
     EventQueue.invokeLater(runnable); 
    } 
} 

以下是相同的輸出:

GRIDBAGLAYOUTIMAGE