java SetLayout 函数主要用途

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30947870/
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-11-02 17:52:27  来源:igfitidea点击:

SetLayout Function main purpose

javaawtlayout-manager

提问by Ahmed Nabil

 public void init_numSolvers() {
    for (x = 0; x < 9; x++) {
        n++;
        num[x] = new Button("" + n);
        add(num[x]);
        num[x].setBounds(num_x, num_y, 40, 40);
        setLayout(null);
        num_x += 40;
    }

why setBounds() function doesn't work without setLayout(null) i just want to understand the main purpose of setLayout(null) function

为什么 setBounds() 函数在没有 setLayout(null) 的情况下不起作用我只想了解 setLayout(null) 函数的主要用途

回答by Hovercraft Full Of Eels

You will want to read the tutorials and the API as they clearly spell out what setBounds and setLayout does, but briefly:

您将需要阅读教程和 API,因为它们清楚地说明了 setBounds 和 setLayout 的作用,但简要说明:

  • The setLayout(...)method allows you to set the layout of the container, often a JPanel, to say FlowLayout, BorderLayout, GridLayout, null layout, or whatever layout desired. The layout manager helps lay out the components held by this container.
  • The setBounds(...)method is used to set the location and size of a single component, and is only useful if nulllayout is used by the container that holds this component.
  • When you set a layout to null, you tell the container that it is using no layout at all, and so you the programmer are thus completely responsible for setting all the sizes and positions of components that are added to this container.
  • Having said this, you should strive to avoid using nulllayouts and setBounds(...). While to a newbie Swing programmer, this may seem the easiest way to create complex layouts, when using them you create GUI's that might look good on one platform only. More importantly, they create GUI's that are nearly impossible to maintain and upgrade without causing subtle bugs. Just don't use them if at all possible.
  • Please read the official layout manager tutorialson this as it is well explained there.
  • setLayout(...)方法允许您设置容器的布局,通常是一个 JPanel,比如 FlowLayout、BorderLayout、GridLayout、空布局或任何所需的布局。布局管理器帮助布局这个容器持有的组件。
  • setBounds(...)方法用于设置单个组件的位置和大小,并且仅null在包含该组件的容器使用布局时才有用。
  • 当您将布局设置为 null 时,您告诉容器它根本不使用任何布局,因此程序员完全负责设置添加到此容器的组件的所有大小和位置。
  • 话虽如此,您应该努力避免使用null布局和setBounds(...). 虽然对于新手 Swing 程序员来说,这似乎是创建复杂布局的最简单方法,但在使用它们时,您可以创建仅在一个平台上看起来不错的 GUI。更重要的是,他们创建的 GUI 几乎不可能在不引起细微错误的情况下进行维护和升级。如果可能,请不要使用它们。
  • 请阅读官方的布局管理器教程,因为它在那里有很好的解释。


Note that your code repeatedly sets the container's layout to null within the for loop. I have no idea why it does this repeatedly since you only need to and want to set a container's layout once.

请注意,您的代码在 for 循环中反复将容器的布局设置为 null。我不知道为什么它会重复执行此操作,因为您只需要并希望设置容器的布局一次