【发布时间】:2015-07-12 21:40:49
【问题描述】:
我的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 一起使用。我怎样才能解决这个问题?
【问题讨论】:
标签: java swing jlabel layout-manager gridbaglayout