为什么 GridLayout 的垂直间距一直在增加?

Why is the vertical gap of GridLayout keeping on increasing?

我是 JAVA 和 GUI 的新手。我正在为我的项目制作一个 GUI 屏幕。

我做了一个有 2 行的 GridLayout。第一行有一个 FlowLayout,第二行有一个 BoxLayout。带有 FlowLayout 的面板在整个程序中将保持不变,而带有 BoxLayout 的面板可能会有所不同。我用 GridBagLayoutBoxLayout 面板中包含了另一个面板。每当我向 BoxLayout 添加另一个面板时,GridLayout 的第一行和第二行之间的 space 正在增加。

  1. 谁能告诉我我应该怎么做才能阻止这种情况发生?

  2. 有什么办法可以把radioButton放在面板的中央吗?

代码如下:

 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
/**
 *
 * @author arindamchowdhury
 */
public class ScreenTwo {
     JRadioButton[] radioButton;


    public ScreenTwo() {
        start();
    }

    private void start() {
        JFrame frame = new JFrame();
        JPanel panel1 = new JPanel();
        frame.getContentPane().add(panel1);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Added a panel to the frame with GridLayout. It has 2 rows, and 1 column.
        panel1.setLayout(new GridLayout(2, 1, 1, 1));
        Font font =  new Font("Times New Roman", Font.PLAIN, 25);

        JPanel panel2 = new JPanel();
        panel2.setLayout(new FlowLayout(FlowLayout.CENTER, 50, 1));

        // *** Making a header section using FlowLayout *** //
        panel2.add(new JLabel("Flight ID"));
        panel2.add(new JLabel("Departure"));
        panel2.add(new JLabel("Arrival"));
        panel2.add(new JLabel("Total Duration                       "));        
        changeFont(panel2, font);
        panel1.add(panel2);

        // Making the 2nd row of GridLayout, a BoxLayout, so that components are added vertically.
        JPanel panel3 = new JPanel();
        panel3.setLayout(new BoxLayout(panel3, BoxLayout.Y_AXIS));
        panel3.setBorder(BorderFactory.createLineBorder(Color.BLACK));        
        GridBagConstraints c = new GridBagConstraints();

        // When I increase comboSize, the space increases
        int comboSize = 1, i;
        JPanel panelCombo[] = new JPanel[comboSize];
        ButtonGroup group = new ButtonGroup();
        radioButton = new JRadioButton[comboSize];
        for(i=0; i<comboSize; i++) {
            panelCombo[i] = new JPanel();
            panelCombo[i].setLayout(new GridBagLayout());
            c.gridx = 0;            
            c.gridy = 0;
            c.insets = new Insets(0, 0, 0, 100);
            panelCombo[i].add(new JLabel("Hi"), c);

            c.gridx++;
            panelCombo[i].add(new JLabel("Hi"), c);

            c.gridx++;
            panelCombo[i].add(new JLabel("Hi"), c);

            c.gridx++;
            c.gridheight = 4;
            panelCombo[i].add(new JLabel("Hi"), c);

            // ***  Added a RadioButton *** //
            c.gridx++;         
            radioButton[i] = new JRadioButton();
            panelCombo[i].add(radioButton[i]);
            group.add(radioButton[i]);

            c.gridheight = 1;
            c.gridx = 1;
            c.gridy++;
            panelCombo[i].add(new JLabel("Hi"), c);

            c.gridx++;
            panelCombo[i].add(new JLabel("Hi"), c);

            c.gridx = 0;
            c.gridy++;
            panelCombo[i].add(new JLabel("Hi"), c);

            c.gridx++;
            panelCombo[i].add(new JLabel("Hi"), c);

            c.gridx++;
            panelCombo[i].add(new JLabel("Hi"), c);

            c.gridx = 1;
            c.gridy++;
            panelCombo[i].add(new JLabel("Hi"), c);

            c.gridx++;
            panelCombo[i].add(new JLabel("Hi"), c);

            panel3.add(panelCombo[i]);
            changeFont(panelCombo[i], font);
            panelCombo[i].setBorder(BorderFactory.createLineBorder(Color.BLACK));
        }            
        panel1.add(panel3);

        frame.pack();
        frame.setVisible(true);

    }

    // *** A function to change the font of all components of a container *** //
    private void changeFont ( Component component, Font font ) {
        component.setFont ( font );
        if ( component instanceof Container )
        {
            for ( Component child : ( ( Container ) component ).getComponents () )
            {
                changeFont ( child, font );
            }
        }
    }

    public static void main(String[] args)
    {
        ScreenTwo sc = new ScreenTwo();
    }
} 

事情是这样的:

comboSize等于1时:

Whenever I'm adding another panel to the BoxLayout, the space between the 1st row and the 2nd row of the GridLayout is increasing.

顶级面板的 GridLayout space 个组件均匀分布。这意味着带有 FlowLayout 的顶部面板将始终与带有 BoxLayout 的底部面板具有相同的 space,即 combined space 您放入的所有 GridBagLayout 个面板。

视觉上,vertical space of red panel = vertical space of blue + green + yellow panels

如您所见,不是第 1 行和第 2 行之间 space 增加(space 是 1px,您实际上可以看到it), 就是顶部面板的space.

如果您希望顶部面板的 space 保持不变,您需要将 GridBagLayout 面板直接添加到顶级 GridLayout 面板,而不需要底部 BoxLayout 面板:

gridPanel.setLayout(new GridLayout(0, 1, 1, 1));
// ...
gridPanel.add(topFlowPanel);
// ...
for (...) {
    // ...
    gridPanel.add(panelCombo[i]);
    // ...
}

请注意,现在所有行之间都添加了 1px 的垂直间距。

Is there any way such that the radioButton can be placed in the CENTRE of the panel?

如果你的意思是垂直居中那么你只是在将它添加到面板时忘记添加 GridBagConstraints:

panelCombo[i].add(radioButton[i], c);
                                  ^