Java 什么是 setBounds 以及如何使用它?

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

What is setBounds and how do I use it?

javasetbounds

提问by JeremyF

I cannot find anything on setBounds, what it is for, or what its function actually is. Could someone clear me up on this? Thanks so much!

我在 setBounds 上找不到任何东西,它的用途是什么,或者它的实际功能是什么。有人可以澄清我吗?非常感谢!

回答by tianz

You can use setBounds(x, y, width, height)to specify the position and size of a GUI component if you set the layoutto null. Then (x, y)is the coordinate of the upper-left corner of that component.

setBounds(x, y, width, height)如果将 设置为layout,则可以使用来指定 GUI 组件的位置和大小null。然后(x, y)是该组件左上角的坐标。

回答by nanofarad

This is a method of the java.awt.Componentclass. It is used to set the position and size of a component:

这是java.awt.Component类的方法。它用于设置组件的位置和大小:

setBounds

public void setBounds(int x,
                  int y,
                  int width,
                  int height) 

Moves and resizes this component. The new location of the top-left corner is specified by x and y, and the new size is specified by width and height. Parameters:

  • x - the new x-coordinate of this component
  • y - the new y-coordinate of this component
  • width - the new width of this component
  • height - the new height of this component

setBounds

public void setBounds(int x,
                  int y,
                  int width,
                  int height) 

移动并调整此组件的大小。左上角的新位置由 x 和 y 指定,新大小由宽度和高度指定。参数:

  • x - 此组件的新 x 坐标
  • y - 此组件的新 y 坐标
  • width - 此组件的新宽度
  • height - 此组件的新高度

xand yas above correspond to the upper left corner in most (all?) cases.

xy如上对应在大多数(所有?)情况下的左上角。

It is a shortcut for setLocationand setSize.

这是一个快捷方式setLocationsetSize

This generally only works if the layout/layout manager are non-existent, i.e. null.

这通常仅在布局/布局管理器不存在时才有效,即空。

回答by MadProgrammer

setBoundsis used to define the bounding rectangle of a component. This includes it's position and size.

setBounds用于定义组件的边界矩形。这包括它的位置和大小。

The is used in a number of places within the framework.

用于框架内的许多地方。

  • It is used by the layout manager's to define the position and size of a component within it's parent container.
  • It is used by the paint sub system to define clipping bounds when painting the component.
  • 布局管理器使用它来定义组件在其父容器中的位置和大小。
  • 在绘制组件时,绘制子系统使用它来定义裁剪边界。

For the most part, you should never call it. Instead, you should use appropriate layout managers and let them determine the best way to provide information to this method.

在大多数情况下,你永远不应该调用它。相反,您应该使用适当的布局管理器,并让他们确定向此方法提供信息的最佳方式。

回答by Gee Bee

Actually, a Swing component does have multiple dimensions, as:

实际上,Swing 组件确实有多个维度,例如:

  • current size - setSize() and setBounds() sets this
  • minimum size - setMinimumSize() sets this
  • preferred size - setPerferredSize() sets this
  • maximum size - setMaximumSize() sets this.
  • 当前大小 - setSize() 和 setBounds() 设置这个
  • 最小尺寸 - setMinimumSize() 设置这个
  • 首选大小 - setPerferredSize() 设置这个
  • 最大尺寸 - setMaximumSize() 设置这个。

SetBounds is a shortcut for setting current size plus location of the widget ifyou don't use any layout manager.

如果您不使用任何布局管理器,SetBounds 是设置小部件当前大小和位置的快捷方式。

If you use a layout manager, it is the responsibility of the layout manager to lay out your components, taking into account the preferred size you set, and ensuring that the comonent never gets smaller than its minimumSize or bigger than its maximumSize.

如果您使用布局管理器,则布局管理器负责布置您的组件,考虑您设置的首选大小,并确保组件永远不会小于它的 minimumSize 或大于其 maximumSize。

In this case, the layoutManager will call setSize (or setBounds), and you can not really control the position or dimension of the component.

在这种情况下,layoutManager 会调用 setSize(或 setBounds),您无法真正控制组件的位置或尺寸。

The whole point of using a layout manager is to have a platform and window-size independent way of laying out your components automatically, therefore you don't expect to call setSize from your code.

使用布局管理器的全部意义在于拥有一种独立于平台和窗口大小的自动布局组件的方式,因此您不希望从代码中调用 setSize。

(Personal comment: There are buggy layout managers, I personally hate all of them and rolled my own, which offers the flexibility of MigLayout without the learning curve.)

(个人评论:有错误的布局管理器,我个人讨厌所有这些并推出了我自己的,它提供了 MigLayout 的灵活性而无需学习曲线。)

回答by Prafulla Kumar Sahu

There is an answer by @hexafraction , He had specified the x and y to be top right corner which is wrong, those are top left corner .

@hexafraction 有一个答案,他将 x 和 y 指定为右上角,这是错误的,那些是左上角。

I have also provided the source please check it.

我也提供了来源,请查收。

public void setBounds(int x,
             int y,
             int width,
             int height)

Moves and resizes this component. The new location of the top-left corner is specified by x and y, and the new size is specified by width and height. This method changes layout-related information, and therefore, invalidates the component hierarchy.

移动并调整此组件的大小。左上角的新位置由 x 和 y 指定,新大小由宽度和高度指定。此方法更改与布局相关的信息,因此使组件层次结构无效。

Parameters:

参数:

x - the new x-coordinate of this component

y - the new y-coordinate of this component

width - the new width of this component

height - the new height of this component

x - 此组件的新 x 坐标

y - 此组件的新 y 坐标

width - 此组件的新宽度

height - 此组件的新高度

source:- setBounds

来源:- setBounds

回答by Axel Podehl

The way that Java Swing UIs work is that for each JPanel there is always a LayoutManagerthat decides on where to exactly place your components. Each layout managers works differently, so if you use for example a BorderLayout, setBounds() is not used by the LayoutManager, instead component placement is decided by East,West,South,North,Center.

Java Swing UI 的工作方式是,对于每个 JPanel,始终有一个LayoutManager来决定将组件准确放置在何处。每个布局管理器的工作方式不同,因此如果您使用 BorderLayout,则 LayoutManager 不使用 setBounds(),而是由 East、West、South、North、Center 决定组件放置。

For the NullLayoutManager (in case you used new JPanel(null)) however, each component has to have an x and y coordinate. Stupid Sidenote: if your UI would be three-dimensional there would also be a z coordinate.

new JPanel(null)但是,对于 NullLayoutManager(如果您使用),每个组件都必须具有 x 和 y 坐标。愚蠢的旁注:如果您的 UI 是三维的,那么也会有 az 坐标。

So with public void Component.setBounds(int x, int y, int width, int height)you specify where your component is placed and how many pixel it is wide and high.

因此,public void Component.setBounds(int x, int y, int width, int height)您可以指定组件的放置位置以及它的宽度和高度有多少像素。

Here's an example:

下面是一个例子:

import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class JTableInNullLayout
{
  public static void main(String[] argv) throws Exception {

      JPanel panel = new JPanel(null);

      JLabel helloLabel = new JLabel("Hello world!");
      helloLabel.setBounds( 10, 50, 60, 20 ); // x, y, width, height
      panel.add(helloLabel);

      JFrame frame = new JFrame();
      frame.add(panel);
      frame.setPreferredSize( new Dimension(200,200));
      frame.pack();
      frame.setVisible(true);
  }
}

回答by DrSimplisist

here's a short paragraph from this article How to Make Frames (Main Windows) - The Java Tutorials - Oraclethat explains what setBounds method does in addition to some other similar methods:

这是本文如何制作框架(主窗口)-Java 教程-Oracle中的一小段,它解释了 setBounds 方法除了其他一些类似方法之外的作用:

The pack method sizes the frame so that all its contents are at or above their preferred sizes. An alternative to pack is to establish a frame size explicitly by calling setSize or setBounds (which also sets the frame location). In general, using pack is preferable to calling setSize, since pack leaves the frame layout manager in charge of the frame size, and layout managers are good at adjusting to platform dependencies and other factors that affect component size.

pack 方法调整框架的大小,使其所有内容都等于或大于其首选大小。pack 的另一种方法是通过调用 setSize 或setBounds (也设置帧位置)显式建立帧大小。一般来说,使用 pack 比调用 setSize 更可取,因为 pack 让框架布局管理器负责框架大小,而布局管理器擅长调整平台依赖性和其他影响组件大小的因素。

the parameters of setBounds are (int x, int y, int width, int height) x and y are define the position/location and width and height define the size/dimension of the frame.

setBounds 的参数是 (int x, int y, int width, int height) x 和 y 定义位置/位置,宽度和高度定义框架的大小/尺寸。