Java 减少JFrame中的空白空间

Java 减少JFrame中的空白空间,java,swing,Java,Swing,我这里有一个已经完成的应用程序,但是屏幕上有大量的空白,我已经摆弄了一段时间了,但是下面的图片是我最想剪的 我假设我没有使用某种方法或实用工具,而我可能使用过,也可能根本没有使用过 代码如下: public ListWindow() { chooser = new JFileChooser(); this.setLayout(new GridLayout(3,1,1,1)); JPanel sortPanel = new JPanel(); JPanel d

我这里有一个已经完成的应用程序,但是屏幕上有大量的空白,我已经摆弄了一段时间了,但是下面的图片是我最想剪的

我假设我没有使用某种方法或实用工具,而我可能使用过,也可能根本没有使用过

代码如下:

public ListWindow() {

    chooser = new JFileChooser();

    this.setLayout(new GridLayout(3,1,1,1));
    JPanel sortPanel = new JPanel();
    JPanel displayPanel = new JPanel();
    JPanel btns = new JPanel();

    JLabel sortLabel = new JLabel("Sort by:");
    sortGame = new JRadioButton("Game");
    sortScore = new JRadioButton("Score");
    sortBtn = new JButton("Sort");

    ButtonGroup group = new ButtonGroup();
    group.add(sortGame);
    group.add(sortScore);

    list = new JList(reviewList.toArray());

    JScrollPane reviewPane = new JScrollPane(list);
    reviewPane.setPreferredSize(new Dimension(400, 150));

    windowBtn = new JButton("To Review Entry");

    buttonActions();

    sortPanel.add(sortLabel);
    sortPanel.add(sortGame);
    sortPanel.add(sortScore);
    sortPanel.add(sortBtn);
    displayPanel.add(reviewPane);
    btns.add(windowBtn);

    this.add(sortPanel);
    this.add(displayPanel);
    this.add(btns);
}
public static void main(String[] args) {

    ListWindow window = new ListWindow();
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setTitle("CriticalStrike.com - Review Database");
    window.pack();
    window.setVisible(true);
}
}

谢谢你们的帮助,伙计们

这是由于使用GridLayout造成的,这会导致添加到GridLayout的组件使用相同大小的空间。在一列三行的情况下,如果这三个组件从一列到下一列占用的空间不相等,则某些GridLayout区域将具有未使用的额外空间

我建议在这里使用边界布局。可以将第一个和第三个构件添加到南北位置,这将尝试使用最少的房间高度和最多的房间宽度,并且可以将较大的文本区域添加到中心位置,这将尝试使用最多的房间高度和宽度

因此,一些类似于

this.setLayout(new BorderLayout());
...
this.add(sortPanel, BorderLayout.NORTH);
this.add(displayPanel, BorderLayout.CENTER);
this.add(btns, BorderLayout.SOUTH);

还有另一种可能性,使用GridBagLayout将所有组件向上推,在窗口底部留下所有死区

你需要把这个添加到你的主菜单中

getContentPane().setLayout(new GridBagLayout());
然后在ListWindow()中需要类似的内容

然后,您将使用添加面板

this.add(panel, gBC);
现在的诀窍是把所有的东西都往上推——你可以在面板下面放一个空的JLabel来垂直调整大小。像这样:

JLabel jLabel1 = new JLabel();
gBC.fill = GridBagConstraints.VERTICAL;
gBC.weightx = 0.0;
gBC.weighty = 1.0;
this.add(jLabel1, gBC);

您可能还需要设置GridBagConstraints中每个组件的垂直位置,但我怀疑您是否按顺序添加它们。

谢谢您,先生!在我的大学课程中,我们几乎不涉及边界布局,所以我从来没有想过要使用它。1+投票@不要忘记尽快接受这个答案!另外,请查看,特别是布局管理器的视觉指南部分。
JLabel jLabel1 = new JLabel();
gBC.fill = GridBagConstraints.VERTICAL;
gBC.weightx = 0.0;
gBC.weighty = 1.0;
this.add(jLabel1, gBC);