2015-07-12 109 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);