【问题标题】:JLabel/JPanel positioning issues. (GridBagLayout)JLabel/JPanel 定位问题。 (网格包布局)
【发布时间】: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 = 0label1 会显示在标签顶部,使两个单独的碰撞在一起。

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

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

【问题讨论】:

    标签: java swing jlabel layout-manager gridbaglayout


    【解决方案1】:

    gbc.weighty = 1; 正在为组件提供所有组件布局后剩余的所有剩余空间。不要为 label 提供此约束,而应仅将其提供给 label1

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

    改为将label 放在0x0label1 放在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

    【讨论】:

    • 非常感谢您。一直以来,我总是在在这里提出问题之前使用 Java 教程,但直到有人在这里解释之前,我永远无法完全理解它。再次感谢。
    • 您好,我还有其他问题。请问你能帮我吗?
    • @bob9123 我通常会提供指向其他教程、文档或支持文档的链接,因为您不应该相信我的话;)。如果您有更多问题,或许可以考虑再问一个问题,只需提供一个指向此问题的链接,作为可能尚未阅读此问题的人的参考点;)
    • stackoverflow.com/questions/31416519/… 链接到新问题。再次感谢您的帮助:)
    【解决方案2】:
    //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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-31
      • 2023-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多