java - GridBagLayout 向列添加额外空间

标签 java swing gridbaglayout

我有一个 JPanel ,其中 GridBagLayout 作为布局管理器,我正在尝试进行这种安排:

https://i.sstatic.net/ZZmVH.png

忽略边框的额外深蓝色空间。

我总共有 5 列和 3 行,所有组件都将 setPreferredSize() 设置为精确的值,以完美适合 JPanel,它也有一个首选尺寸(170 x 115)。

问题是 GridBagLayout 似乎在最后一列宽度上添加了 30 pxls,因为仅向 JPanel 的宽度添加了 30 pxls(总共 200 个)组件正确显示,如下所示:

https://i.sstatic.net/GzjDz.png

但由于额外的空间,最后一列被分开。

它增加了 30 pxls,因为向 JPanel 的宽度添加 29 pxls 会得到以下结果:

https://i.sstatic.net/tdmYI.png

根据我的经验,可用空间太小,无法显示所有组件,然后使用 setMinimumSize()

我不知道这 30 个 pxls 来自哪里,请问谁能告诉我如何使这些组件适合?

代码如下所示,目前给出的结果是:

https://i.sstatic.net/p77am.png

忽略JFrame的额外黑色空间。

您可以在第 34 行更改 JPanel 的宽度。

import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;

public class Test{
    public static void main (String[] args){
        JFrame f;
        SizeProperties p;

        f = new JFrame();
        p = new SizeProperties();

        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().setPreferredSize(new Dimension(250,200));
        f.getContentPane().setBackground(Color.BLACK);
        f.setLayout(new FlowLayout());

        f.add(p);
        f.pack();
        f.setVisible(true);
    }
}

final class SizeProperties extends JPanel{
    private GridBagConstraints c;
    private PropertiesLabel xL,yL,wL,hL;
    private PropertiesField xF,yF,wF,hF;
    private ProportionToggleButton ptb;

    SizeProperties(){
        setBackground(new Color(18,101,142));
        setPreferredSize(new Dimension(170,115));//Change width here
        setLayout(new GridBagLayout());

        xL = new PropertiesLabel("x:",25,25);
        xF = new PropertiesField();
        yL = new PropertiesLabel("y:",25,25);
        yF = new PropertiesField();
        wL = new PropertiesLabel("Width:",80,25);
        wF = new PropertiesField();
        hL = new PropertiesLabel("Height:",80,25);
        hF = new PropertiesField();
        ptb = new ProportionToggleButton();

        c = new GridBagConstraints(0,0,1,1,0,0,GridBagConstraints.CENTER,GridBagConstraints.NONE,new Insets(10,10,10,0),0,0);
        add(xL,c);

        c = new GridBagConstraints(1,0,1,1,0,0,GridBagConstraints.CENTER,GridBagConstraints.NONE,new Insets(10,0,10,10),0,0);
        add(xF,c);

        c = new GridBagConstraints(2,0,1,1,0,0,GridBagConstraints.CENTER,GridBagConstraints.NONE,new Insets(10,0,10,0),0,0);
        add(yL,c);

        c = new GridBagConstraints(3,0,2,1,0,0,GridBagConstraints.CENTER,GridBagConstraints.NONE,new Insets(10,0,10,10),0,0);
        add(yF,c);

        c = new GridBagConstraints(0,1,2,1,0,0,GridBagConstraints.CENTER,GridBagConstraints.NONE,new Insets(0,10,10,0),0,0);
        add(wL,c);

        c = new GridBagConstraints(2,1,2,1,0,0,GridBagConstraints.CENTER,GridBagConstraints.NONE,new Insets(0,0,10,10),0,0);
        add(wF,c);

        c = new GridBagConstraints(0,2,2,1,0,0,GridBagConstraints.CENTER,GridBagConstraints.NONE,new Insets(0,10,10,0),0,0);
        add(hL,c);

        c = new GridBagConstraints(2,2,2,1,0,0,GridBagConstraints.CENTER,GridBagConstraints.NONE,new Insets(0,0,10,10),0,0);
        add(hF,c);

        c = new GridBagConstraints(4,1,1,2,0,0,GridBagConstraints.CENTER,GridBagConstraints.NONE,new Insets(0,0,10,10),0,0);
        add(ptb,c);

    }
}

final class PropertiesLabel extends JLabel{
    PropertiesLabel(String label,int w,int h){
        setText(label);
        setPreferredSize(new Dimension(w,h));
        setBackground(Color.BLACK);
        setOpaque(true);
        setForeground(Color.WHITE);
        setFont(new Font("SansSerif",Font.PLAIN,14));
        setHorizontalAlignment(SwingConstants.CENTER);
        setVerticalAlignment(SwingConstants.CENTER);
    }
}

final class PropertiesField extends JTextField{
    private int validNumber = 0;

    PropertiesField(){
        setPreferredSize(new Dimension(45,25));
        setBackground(new Color(202,226,255));
        setForeground(Color.BLACK);
        setFont(new Font("SansSerif",Font.PLAIN,14));
        setHorizontalAlignment(JTextField.CENTER);
        setText("999");
    }
}

final class ProportionToggleButton extends JToggleButton{
    ProportionToggleButton(){
        setPreferredSize(new Dimension(15,60));
    }
}

提前致谢。

最佳答案

I have in total 5 columns

据我所知并非如此。您不能只将组件分配给列。实际上,您需要在一行中包含 5 个组件才能创建 5 列的网格。

根据您的图片,您的第一行有 4 列,最后两行有 3 列。

因此,根据您的图片,您需要重新考虑您的设计。我看到的是:

  1. 基于最后两行,您有三列。
  2. 现在,在第一行,x/999 将是一个带有两个组件的单个面板。该面板将位于第一列(带有宽度/高度标签)。
  3. 此外,在第一行中,y/999 将是一个包含两个组件的单个面板,将从第二列开始,网格宽度为 2。
  4. 第二行和第三行将包含第二列中的 999 组件
  5. 按钮从第 2 行开始,网格高度为 2,并将包含在第 3 列

此外,您不应该设置组件的首选大小。每个组件将确定其自己的首选大小。然后,GridBagLayout 将根据行/列中的组件确定每个单元格的大小。

关于java - GridBagLayout 向列添加额外空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54897340/

相关文章:

java - Apache Tomcat 上的应用程序抛出 HTTP 405 错误

java - "Delete Where"在 Hibernate 中级联删除?

java - 如何在Java中导入使用其他类的抽象方法的Python子类?

java - 将数据从 netbeans 中的另一个表单添加到 JTable

Java Swing 保留按钮的外观和感觉,但保留背景颜色

java - GridBagLayout 的奇怪行为

java - gridbaglayout 中的 JTextfield 全宽

java - 如何解析多个整数

java - 在java中获取组件的坐标

java - 除了使用 GridBagConstraints 之外,还有更精确的方法来放置 JButton 吗?