我的JLabel
组件的定位存在小问题。
代码:
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
的浮点数。我怎样才能解决这个问题?
答案 0 :(得分:2)
gbc.weighty = 1;
导致为组件提供布局完所有组件后剩余的所有剩余空间。您应该仅将label
提供给<{1}},而不是向label1
提供此约束
当您未向组件提供gridx
/ gridy
时,GridBagLayout
会将组件放在最后一个组件旁边,因为没有添加其他组件,{{1} }位于顶部,但您还提供label
/ gridx
gridy
0
到label1
,它位于同一位置。
相反,label
位于0x0
而label1
位于0x1
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
属性来影响组件在其单元格中对齐的位置。
请查看How to Use GridBagLayout了解更多详情
如果不完全知道你正在做什么,我可能会建议你看看How to Use Tables
答案 1 :(得分: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);