简体   繁体   English

GridBagLayout为列添加额外的空间

[英]GridBagLayout adding extra space to column

I have a JPanel with GridBagLayout as the layout manager and I'm trying to get this arrangement: 我有一个使用GridBagLayout作为布局管理器的JPanel ,我正在尝试获得这种安排:

https://i.stack.imgur.com/ZZmVH.png

Ignore the extra dark blue space of the border. 忽略边框的额外深蓝色空间。

I have in total 5 columns and 3 rows and all the components have setPreferredSize() set to the exact value to fit perfectly in the JPanel which also has a preferred size (170 x 115). 我总共有5列和3行,并且所有组件都将setPreferredSize()设置为确切的值,以完美适合JPanel ,该JPanel也具有首选的大小(170 x 115)。

The problem is that GridBagLayout appears to be adding 30 pxls to the last column width, because only adding 30 pxls to the width of the JPanel (200 in total) the components are shown properly, like this: 问题是GridBagLayout似乎在最后一列宽度上增加了30像素,因为仅在JPanel的宽度(总共200)上增加了30像素,这样才能正确显示组件,如下所示:

https://i.stack.imgur.com/GzjDz.png

but with the last column separated because of the extra space. 但由于多余的空间,最后一列分开了。

It adds 30 pxls because adding 29 pxls to the JPanel's width gives this result: 它增加了30个像素,因为在JPanel的宽度上添加29个像素会得到以下结果:

https://i.stack.imgur.com/tdmYI.png

which in my experience tells that the space available is too small to show all components and then uses the setMinimumSize() . 根据我的经验,这表明可用空间太小,无法显示所有组件,然后使用setMinimumSize()

I don't know where those 30 pxls came from, please can anyone tell me how to make the components fit? 我不知道那30像素来自何处,请谁能告诉我如何使组件合适?

The code is shown below and currently gives this result: 该代码如下所示,当前可提供此结果:

https://i.stack.imgur.com/p77am.png

Ignore the extra black space of the JFrame . 忽略JFrame的多余黑色空间。

You can change the width of the JPanel in line 34. 您可以在第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));
    }
}

Thanks in advance. 提前致谢。

I have in total 5 columns 我总共有5列

Not from what I can see. 从我所看不到的。 You can't just assign a component to a column. 您不能仅将组件分配给列。 You actually need to have 5 components in a single row to create a grid of 5 columns. 实际上,您需要在一行中包含5个组件才能创建5列的网格。

Based on your picture you first row has 4 columns and the last two rows have 3 columns. 根据您的图片,第一行有4列,最后两行有3列。

So based on your picture you need to rethink your design. 因此,根据您的图片,您需要重新考虑您的设计。 What I see is: 我看到的是:

  1. you have three columns based on the last two rows. 您有基于最后两行的三列。
  2. now on the first row the x/999 will be a single panel with two components. 现在,第一行的x / 999将是一个包含两个组件的面板。 This panel will be in the 1st column (with the width/height labels). 该面板将在第一栏中(带有宽度/高度标签)。
  3. Also in the first row the y/999 will be a single panel with two components and will start in the 2nd column and have a gridwidth of 2. 同样在第一行中,y / 999将是一个包含两个组件的单个面板,并且将从第二列开始,并且网格宽度为2。
  4. The 2nd and 3rd row will contain the 999 component in the second column 第二行和第三行在第二列中将包含999组件
  5. the button start in the 2nd row and have a gridheight of 2 and will be contained in the 3rd column 按钮从第二行开始,网格高度为2,将包含在第三列中

Also, you should not be setting the preferred size of the components. 另外,您不应设置组件的首选大小。 Each component will determine its own preferred size. 每个组件将确定自己的首选大小。 The GridBagLayout will then determine the size of each cell based on the components in the row/column. 然后,GridBagLayout将基于行/列中的组件确定每个单元格的大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM