JLabel/JPanel定位问题。 (网格袋布局)

JLabel/JPanel positioning issues. (GridBagLayout)

我的 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 = 0label1 显示在标签顶部,使两个单独的碰撞在一起。

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

现在我希望 label1 稍微低于标签,但是我不能将浮点数与 gridy 一起使用。我怎样才能解决这个问题?

gbc.weighty = 1; 导致组件在所有组件布局后留下所有剩余的 space。不要向 label 提供此约束,而应仅将其提供给 label1

当您没有为组件提供 gridx/gridy 时,GridBagLayout 会将组件放在最后一个组件的旁边,因为没有添加其他组件,label 放置在顶部,但您还提供了 0gridx/gridylabel1,它放置在同一位置。

相反,将 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 了解更多详情

不知道 "exactly" 你在做什么,我建议你也看看 How to Use Tables

//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);