gpt4 book ai didi

java - 使用布局将面板设置在屏幕中央

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:18:16 25 4
gpt4 key购买 nike

我尝试通过使用将子面板的位置设置在父面板的中心

parent_panel.setLayout(new BorderLayout());
parent_panel.add(child_panel, BorderLayout.CENTER);

但是它被添加在水平屏幕的中间但垂直在顶部。

我需要做什么才能将它垂直和水平地添加到屏幕中央?

最佳答案

如果我没理解错的话,你想要一个类似这样的界面:

+-------- Parent panel --------+|                              ||                              ||    +--- Child panel ----+    ||    |                    |    ||    |                    |    ||    |                    |    ||    |                    |    ||    +--------------------+    ||                              ||                              |+------------------------------+

...and you have no other components being added to the parent panel.

If this is the case, you have two choices that I know of (based on this question, which I apparently answered):

  1. Use a GridBagLayout with an empty GridBagConstraints object, like this:

    parent_panel.setLayout(new GridBagLayout());
    parent_panel.add(child_panel, new GridBagConstraints());
  2. 使用 BoxLayout,像这样:

    parent_panel.setLayout(new BoxLayout(parent_panel, BoxLayout.PAGE_AXIS));
    Box horizontalBox = Box.createHorizontalBox();
    horizontalBox.add(Box.createHorizontalGlue());
    horizontalBox.add(child_panel);
    horizontalBox.add(Box.createHorizontalGlue());
    Box verticalBox = Box.createVerticalBox();
    verticalBox.add(Box.createVerticalGlue());
    verticalBox.add(horizontalBox); // one inside the other
    verticalBox.add(Box.createVerticalGlue());

关于java - 使用布局将面板设置在屏幕中央,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2411197/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com