2013-03-19 91 views
1

不久,你可以看我的代碼,看到了放在我把它作爲一個圖片添加JPanels到JFrame的室內用它的默認佈局

/* 
* Suppose I have 4 buttons vertically on the right hand side in First PAnel 
* and 4 buttons on bottom horizantally in second Panel 
* and 4 text fiedls in the center in 4 rows in third Panel 
* Using Frame's default border 
*/ 
     JPanel p1= new JPanel(); 
     for (int i = 0; i < right.length; i++) { 
      right[i]=new JButton("right "+(i+1)); 
      p1.add(right[i]); 
     } 
     JPanel p2 = new JPanel(); 
     for (int i = 0; i < down.length; i++) { 
      down[i] = new JButton("Down "+(i+1)); 
      p2.add(down[i]); 
     } 

     JPanel p3=new JPanel(); 
     for(int i = 0 ; i<text.length;i++){ 
      text[i]=new JTextField(30); 
      p3.add(text[i]); 
     } 
     Container c =getContentPane(); 
     c.add(p1,"East"); 
     c.add(p2,"South"); 
     c.add(p3,"Center"); 
     setSize(300,400); 
     setVisible(true); 
     setDefaultCloseOperation(3); 

輸出: enter image description here

我想讓它這樣 enter image description here

注第二輸出我用空佈局和方法的setBounds

有何建議?

+2

你可能想看看MigLayout。 – Aboutblank 2013-03-19 17:11:29

回答

1

首先,你應該總是使用一個或多個佈局管理器。

標籤和字段將被添加到帶有GridBagLayout的JPanel。

第一個,上一個,下一個和最後一個按鈕將被添加到帶有BoxLayout,LINE_AXIS的JPanel。按鈕應該按照我給出的順序,首先,上一個,下一個和最後一個。這就是用戶習慣的。

使用BoxLayout,PAGE_AXIS將添加,清除,編輯,保存和刪除按鈕添加到JPanel。編輯和保存按鈕應該是第一個,然後是添加,清除和刪除按鈕。我會在保存和添加按鈕之間以及清除和刪除按鈕之間放置一些空間,以便在視覺上分離這些功能並最小化意外點擊刪除按鈕。我還會在刪除按鈕上放置一個「Are you sure」對話框。

退出按鈕將被添加到帶有FlowLayout的JPanel。

將使用GridBagLayout將四個JPanel添加到主JPanel。

主JPanel將被添加到JFrame中。

編輯添加:這是我的代碼,用於將四個JPanel添加到主JPanel。

protected static final Insets spaceInsets = new Insets(10, 10, 4, 10); 

protected JPanel panel; 

protected JPanel formPanel; 
protected JPanel nextPanel; 
protected JPanel editPanel; 
protected JPanel exitPanel; 

protected void createPartControl() { 
    panel = new JPanel(); 
    panel.setLayout(new GridBagLayout()); 

    int gridy = 0; 
    gridy = createPanelLayout(gridy); 
} 

protected int createPanelLayout(int gridy) { 
    addComponent(panel, formPanel, 0, gridy, 1, 1, spaceInsets, 
      GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL); 

    addComponent(panel, editPanel, 1, gridy++, 1, 1, spaceInsets, 
      GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL); 

    addComponent(panel, nextPanel, 0, gridy, 1, 1, spaceInsets, 
      GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL); 

    addComponent(panel, exitPanel, 1, gridy++, 1, 1, spaceInsets, 
      GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL); 

    return gridy; 
} 

protected void addComponent(Container container, Component component, 
     int gridx, int gridy, int gridwidth, int gridheight, 
     Insets insets, int anchor, int fill) { 
    GridBagConstraints gbc = new GridBagConstraints(gridx, gridy, 
      gridwidth, gridheight, 1.0D, 1.0D, anchor, fill, insets, 0, 0); 
    container.add(component, gbc); 
} 
+0

謝謝我得到了使用許多'佈局管理器'的想法,但我遇到了BoxLayout中按鈕之間的空格問題,請給我一個例子嗎? – Azad 2013-03-19 18:46:35

+1

當然。 Box.createRigidArea(new Dimension(width,height)); – 2013-03-19 18:48:15

+0

在兩個panel.add語句之間創建一個空格。 – 2013-03-19 18:48:58