2015-07-12 132 views
2

对我的JLabel组件的定位存在小问题。JLabel/JPanel定位问题。 (GridBagLayout)

代码:

public class delete { 


    public static void main(String[] args){ 
     GridBagConstraints gbc = new GridBagConstraints(); 

     //Adding the JPanels. Panel for instructions 
     JPanel panel = new JPanel(); 
     panel.setLayout(new GridBagLayout()); 

     //JLabel for the Instructions. 
     JLabel label = new JLabel("<html> Instructions: Type in the grades you’ve received, along with the weights they’ll have in the determination of your overall average. <br> After you press ‘Calculate’, the results will show your average so far. <br> Every grade you enter must be a non-negative number, and every percentage/weight you enter must be a positive number :)</html>"); 
     gbc.gridwidth = 2; 
     gbc.fill = GridBagConstraints.HORIZONTAL; 
     gbc.anchor = GridBagConstraints.PAGE_START; 
     gbc.weighty = 1; 
     panel.add(label, gbc); 


     //JLabel1 for Assingment/Grade/Weight(Percent) 
     JLabel label1 = new JLabel("<html><pre>Assingment\t\t\t\t\tWeight\t\t\t\t\tPercentage</pre></html>"); 
     gbc.gridx = 0; 
     gbc.gridy = 0; 
     panel.add(label1, gbc); 


     //New frame set 
     JFrame frame = new JFrame("Grade Calculator"); 
     frame.setVisible(true); 
     frame.setSize(750,500); 
     frame.setDefaultCloseOperation(EXIT_ON_CLOSE); 
     frame.add(panel); 



    } 


} 

现在现在这个位置JLabellabel)在这是我想要的框架的顶部。问题是JLabellabel1)我想label1略低于标签。

如果我设置gbc.gridy = 0,label1显示在标签的顶部使两个单独的碰撞在一起。

当我设置gbc.gridy = 1时,label1一直走到框架的底部。

现在我想要label1只是略低于标签,但我不能使用漂浮与gridy。我怎样才能解决这个问题?

回答

2

gbc.weighty = 1;正在为组件提供所有组件布置后留下的所有剩余空间。相反,提供label这一限制的,你应该给label1

当你不提供旁边的最后一个组件,因为没有其他成分被添加,label一个gridx/gridy到组件,GridBagLayout地方组件放置在顶部,但是您还要提供0gridx/gridylabel1,它放置在相同的位置。

相反,放置在label0x0label10x1

JLabel label = new JLabel("<html> Instructions: Type in the grades you’ve received, along with the weights they’ll have in the determination of your overall average. <br> After you press ‘Calculate’, the results will show your average so far. <br> Every grade you enter must be a non-negative number, and every percentage/weight you enter must be a positive number :)</html>"); 

gbc.gridwidth = 2; 
gbc.fill = GridBagConstraints.HORIZONTAL; 
gbc.gridy = 0; 
panel.add(label, gbc); 

//JLabel1 for Assingment/Grade/Weight(Percent) 
JLabel label1 = new JLabel("<html><pre>Assingment\t\t\t\t\tWeight\t\t\t\t\tPercentage</pre></html>"); 
gbc.gridx = 0; 
gbc.gridy = 1; 
gbc.anchor = GridBagConstraints.NORTH; 
gbc.weighty = 1; 
panel.add(label1, gbc); 

您也可以影响该组件将调整到内它是通过使用anchor物业小区的位置。

GridBagLayout

看一看How to Use GridBagLayout的一些细节

不知道“正是”你在做什么,我可能会建议也有看How to Use Tables

+0

谢谢这非常重要。我一直在问这里一个问题之前总是使用Java教程,但直到有人在这里解释之后,我才能完全理解它。再次感谢。 – bob9123

+0

你好,还有一件事我遇到了问题。你能帮我解答吗? – bob9123

+0

@ bob9123我通常会提供指向其他教程,文档或支持文档的链接,因为您不应该把我的话当做事情;)。如果你有更多的问题,也许考虑问另一个问题,只需提供一个链接回到这个作为参考点的人谁可能还没有读过这个;) – MadProgrammer

0
//JLabel for the Instructions. 
JLabel label = new JLabel("<html> Instructions: Type in the grades you’ve received, along with the weights they’ll have in the determination of your overall average. <br> After you press ‘Calculate’, the results will show your average so far. <br> Every grade you enter must be a non-negative number, and every percentage/weight you enter must be a positive number :)</html>"); 
gbc.fill = GridBagConstraints.HORIZONTAL; 
gbc.weighty = 1; 
gbc.gridx = 0; 
gbc.gridy = 0; 
panel.add(label, gbc); 


//JLabel1 for Assingment/Grade/Weight(Percent) 
JLabel label1 = new JLabel("<html><pre>Assingment\t\t\t\t\tWeight\t\t\t\t\tPercentage</pre></html>"); 
gbc.gridx = 0; 
gbc.gridy = 1; 
panel.add(label1, gbc);