ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Java Swing GridBagLayout组件定位

2019-07-25 13:02:46  阅读:183  来源: 互联网

标签:gridbaglayout java swing position


我第一次使用Swing的GridBagLayout,到目前为止我只在容器中添加了两个组件,尽管我打算在垂直方向下添加更多组件.到目前为止,第一个组件(一个JLabel)正确定位在PAGE_START,我记得为组件的相应GridBagConstraints设置权重属性.然而,第二个组件(一个JTextField)没有按照我的意图定位,它在容器中居中而不是在JLabel下面向上移动.我试图使用多个锚定常量,包括FIRST_LINE_START,PAGE_START,NORTH&西北,但到目前为止没有任何工作.

因此,我再次呼吁stackoverflow的天才编码员寻求帮助.下面是代码片段,下面是图形问题的图像.

    // Instantiate components and configure their corresponding GridBagConstraints attributes
    // refPlusType properties
    refPlusType = new JLabel("<html><h3>"+"Reference"+" - "+"Job Type"+" </h3><hr /></html>");
    refPlusTypeGC = new GridBagConstraints();
    refPlusTypeGC.gridx = 0; // Grid position 
    refPlusTypeGC.gridy = 0;
    refPlusTypeGC.gridwidth = 2; // Number of colums occupied by component
    refPlusTypeGC.insets = new Insets(5, 10, 5, 10); // Specifies margin
    refPlusTypeGC.weightx = 0.1; // Required for anchor to work. 
    refPlusTypeGC.weighty = 0.1; // Required for anchor to work. 
    refPlusTypeGC.anchor = GridBagConstraints.PAGE_START; // Position in container

    // addressLine1 properties
    addressLine1 = new JTextField();
    addressLine1GC = new GridBagConstraints();
    addressLine1GC.gridx = 0; 
    addressLine1GC.gridy = 1;
    addressLine1GC.gridwidth = 2; 
    addressLine1GC.insets = new Insets(0, 10, 0, 10); 
    addressLine1GC.fill = GridBagConstraints.HORIZONTAL; // Specifies component fill Horizontal space
    addressLine1GC.weightx = 0.1; 
    addressLine1GC.weighty = 0.1; 
    addressLine1GC.anchor = GridBagConstraints.FIRST_LINE_START;

    // Add components to this HALLogisticsDetailsPanel
    this.add(refPlusType, refPlusTypeGC);
    this.add(addressLine1, addressLine1GC);

图片如下;

谢谢大家提供的任何帮助.

解决方法:

尝试将addressLine1的权重增加到更大的值.我做了一个快速测试,将其设置为1000:

addressLine1GC.weighty = 1000.0;

并将addressLine1字段向上推到标签下面,下面是空格.

标签:gridbaglayout,java,swing,position
来源: https://codeday.me/bug/20190725/1533383.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有