2011-05-04 33 views
0
import java.awt.*; 
import javax.swing.*; 
import java.awt.event.*; 
import java.net.URI; 

class MainPageTypo { 
    JFrame fr; 
    JButton easy, medium, tough; 
    JLabel Contact; 

    MainPageTypo() { 
     buildGUI(); 
     hookUpEvents(); 
    } 

    public void buildGUI() { 
     fr = new JFrame("TypoMaster"); 
     JPanel mainP = new JPanel(); 
     mainP.setLayout(new FlowLayout()); 
     JPanel LevelPanel = new JPanel(); 
     LevelPanel.setLayout(new GridLayout(3, 0, 50, 50)); 
     easy = new JButton("Easy"); 
     medium = new JButton("Medium"); 
     tough = new JButton("Tough"); 
     Contact = new JLabel("Visit my Blog"); 
     fr.add(mainP); 
     LevelPanel.add(easy); 
     LevelPanel.add(medium); 
     LevelPanel.add(tough); 
     LevelPanel.setBackground(Color.magenta); 
     LevelPanel.setBorder(BorderFactory.createEmptyBorder(50, 50, 50, 50)); 
     mainP.add(LevelPanel); 
     mainP.setBackground(Color.lightGray); 
     fr.setSize(500, 500); 
     fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     fr.setVisible(true); 
     // fr.setResizable(false); 
    } 

    public void hookUpEvents() { 

    } 

    public static void main(String args[]) { 
     new MainPageTypo(); 
    } 
} 

這是我的完整code.I想休假的JPanel()的頂部垂直空間。我使用LevelPanel.setBorder(BorderFactory.createEmptyBorder(50,50,50,50)); ,但無法獲得垂直gap.How可我得到這個?不能離開垂直間隙

+0

duplcate:http://stackoverflow.com/questions/5879992/leaving-gap-from-top-in-面板 - 相比之下,就我所見,沒有什麼新意。 – kleopatra 2011-05-04 12:46:31

+1

@kleopatra與以前一樣的問題,但確實有新的東西:現在有代碼!這不是很大的區別嗎? – jfpoilpret 2011-05-04 13:24:47

+0

@jfpoilpret - 錯過了顯而易見的:-) – kleopatra 2011-05-04 13:39:18

回答

4

從你說的話,我只是猜測,你可能會通過設置在主面板上的邊界得到的東西后:

mainP.setBorder(BorderFactory.createEmptyBorder(50, 50, 50, 50)); 

請給我們更多的細節。因爲你正在縮小差距。也許畫一個快速和骯髒的圖片。 :)

推薦 請遵循Java命名約定,即變量名稱應該從小寫字母開始。

+0

@ Boro 是的,這是我想要的,但爲什麼'LevelPanel.setBorder(BorderFactory.createEmptyBorder(50,50,50,50));'不產生結果 – saplingPro 2011-05-04 12:15:50

+1

@Meprogrammer,它確實會產生結果。當你在mainP上設置邊框時,你所尋找的就不一樣了。將它們並排比較,您會看到框架中的按鈕高度相同。區別在於空間放置的位置。 – jzd 2011-05-04 12:21:43

+0

@jzd當我寫'LevelPanel.setBorder(BorderFactory.createEmptyBorder(50,50,50,50))'爲什麼LevelPanel沒有向下移動? – saplingPro 2011-05-04 12:25:55

2

基本上,你要明白其中的問題的一部分是負責什麼

  • 容器(這裏levelPanel)上設置邊境增加空間要求的容器本身(如@Boro已經解釋過):適用於該容器的LayoutManager將按照邊框的要求僅在容器內佈置容器的子項。這就是您在levelPanel中看到的,第一個按鈕上方的紅色,最後一個按鈕下方(以及所有按鈕的兩側)

  • 在支持此設置的LayoutManager中設置x/y間隙屬性,完全取決於經理本身的決定,沒有辦法讀取具體經理的api文檔。

的網格佈局API文檔:爲FlowLayout中

* In addition, the horizontal and vertical gaps are set to the 
* specified values. Horizontal gaps are placed between each 
* of the columns. Vertical gaps are placed between each of 
* the rows. 

API文檔:

* @param  hgap the horizontal gap between components 
*      and between the components and the 
*      borders of the <code>Container</code> 
* @param  vgap the vertical gap between components 
*      and between the components and the 
*      borders of the <code>Container</code> 

從你的代碼,我猜你有望實現網格佈局具有相同的間隙行爲作爲FlowLayout :-)

由於levelPanel的父級(parent == mainP)的LayoutManager是FlowLayout,所以可以 - 作爲替代的一個邊界設置爲mainP - 設置的FlowLayout的間隙:

mainP.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 50)); 
+0

+1這是對的,我忘記了。更好地爲佈局本身做到這一點。儘管很難說哪種解決方案是「最好的」,因爲像生活中的所有情況一樣,這取決於你究竟在做什麼。如果您僅在頂部出現間隙,請按照@kleopatra的說明爲父級設置邊框。 – Boro 2011-05-04 14:31:36

+0

@ kleopatra +1很好解釋 – saplingPro 2011-05-05 08:50:27