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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2411197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 21:00:56  来源:igfitidea点击:

Setting panel at center of screen by using layout

javaswinglayout

提问by stillStudent

I tried setting the position of child-panel at the center of parent-panel by using

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

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

But it's getting added at the middle of horizontal screen but vertically at top.

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

What do I need to do to add it at center of screen vertically and horizontally?

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

回答by Michael Myers

If I understand correctly, you want an interface something like this:

如果我理解正确,你想要一个这样的界面:

+-------- 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 GridBagLayoutwith an empty GridBagConstraintsobject, like this:

    parent_panel.setLayout(new GridBagLayout());
    parent_panel.add(child_panel, new GridBagConstraints());
    
  2. Use a BoxLayout, like this:

    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()); 
    
  1. 将 aGridBagLayout与空GridBagConstraints对象一起使用,如下所示:

    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());