2011-05-04 60 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