對我的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);
}
}
現在現在這個位置JLabel
(label
)在這是我想要的框架的頂部。問題是JLabel
(label1
)我想label1
略低於標籤。
如果我設置gbc.gridy = 0
,label1
顯示在標籤的頂部使兩個單獨的碰撞在一起。
當我設置gbc.gridy = 1
時,label1
一直走到框架的底部。
現在我想要label1
只是略低於標籤,但我不能使用漂浮與gridy
。我怎樣才能解決這個問題?
謝謝這非常重要。我一直在問這裏一個問題之前總是使用Java教程,但直到有人在這裏解釋之後,我才能完全理解它。再次感謝。 – bob9123
你好,還有一件事我遇到了問題。你能幫我解答嗎? – bob9123
@ bob9123我通常會提供指向其他教程,文檔或支持文檔的鏈接,因爲您不應該把我的話當做事情;)。如果你有更多的問題,也許考慮問另一個問題,只需提供一個鏈接回到這個作爲參考點的人誰可能還沒有讀過這個;) – MadProgrammer