java Swing 中的锚定和停靠控件

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

Anchoring and Docking Controls in java Swing

javaswing

提问by Arpan

In .net there is a control called anchoring that is used to resize controls dynamically with the form. When a control is anchored to a form and the form is resized, the control maintains the distance between the control and the anchor positions.

在 .net 中有一个称为锚定的控件,用于随表单动态调整控件的大小。当控件锚定到窗体并调整窗体大小时,控件会保持控件和锚点位置之间的距离。

My question is that is there any controls in java that does same functionality as anchoring in .net.
As for an example i have selected a textfield and put it on the panel and resized it properly. Now when i change the size of window(JFrame) or maximize the window the textfield will not maintain the same distance as it was previously. I have been using netbeans and i havent found any properties in pallete manager that answers my question. Please explain me with an example or some links.

我的问题是 Java 中是否有任何控件具有与 .net 中的锚定功能相同的功能。
例如,我选择了一个文本字段并将其放在面板上并正确调整其大小。现在,当我更改窗口(JFrame)的大小或最大化窗口时,文本字段将不会保持与以前相同的距离。我一直在使用 netbeans,但我没有在托盘管理器中找到任何可以回答我的问题的属性。请用一个例子或一些链接来解释我。

采纳答案by Telcontar

Java Swing uses Layout Managers to manage the size and postion of visual components. This is the official java tutorial on how to use this Layout managers:

Java Swing 使用布局管理器来管理可视组件的大小和位置。这是关于如何使用此布局管理器的官方 Java 教程:

http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html

http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html

And there is a brief description of the most common layout managers

并且有最常见的布局管理器的简要说明

FlowLayout (default): it disposes the components left to right and up to down.

FlowLayout(默认):从左到右,从上到下布置组件。

BorderLayout: it divides the container in NORTH, SOUTH, WEST, EAST center CENTER. Only one component by position. Components on border expands and the center component uses the space avaiable

BorderLayout:将容器划分为NORTH、SOUTH、WEST、EAST中心CENTER。按位置只有一个组件。边框上的组件扩展,中心组件使用可用空间

GridLayout: you initialice the manager indicating how many rows and cols the grid is going to have. Each cell has same size and you start adding component on the top left cell.

GridLayout:您初始化管理器,指示网格将有多少行和列。每个单元格都有相同的大小,您开始在左上角的单元格中添加组件。

GridBagLayout: the MOST fine grained layout manager, you can do anything with this, but is a bit complicated, see the java documentation for it.

GridBagLayout:最细粒度的布局管理器,你可以用它做任何事情,但有点复杂,请参阅java文档。

NullLayout (when you nullify the container's layout manayer): no layout manager, components uses the location and size properties to show on components.

NullLayout(当您取消容器的布局管理器时):没有布局管理器,组件使用位置和大小属性在组件上显示。

And of course, containers inside in other containers can use a different layout manager than their parent. Combining layout managers is a difficult art to learn.

当然,其他容器中的容器可以使用与其父容器不同的布局管理器。结合布局管理器是一门难学的艺术。

回答by Adamski

Netbeans has inbuilt support for designing GUIs using the Matisse GUI builder. This is the closest you'll get to the drag & drop style form design that's available with environments such as VB.

Netbeans 内置支持使用 Matisse GUI 构建器设计 GUI。这是最接近可用于 VB 等环境的拖放样式表单设计的方法。

(Personally I'm not a huge fan of this approach though as it autogenerates a lot of code that quickly becomes unmaintainable, especially if not everyone in your development team is using Netbeans.)

(就我个人而言,我不是这种方法的忠实拥护者,因为它会自动生成很多很快变得无法维护的代码,特别是如果您的开发团队中的每个人都没有使用 Netbeans。)

回答by Nick Holt

If you've only got a single control then using java.awt.BorderLayoutand adding your control with BorderLayout.CENTERis your best bet.

如果您只有一个控件,那么使用java.awt.BorderLayout和添加控件BorderLayout.CENTER是您最好的选择。

If you've multiple controls then you need to use java.awt.GridBagLayout.

如果您有多个控件,则需要使用java.awt.GridBagLayout.

You need to set the following:

您需要设置以下内容:

gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
gridBagConstraints.fill = GridBagConstraints.BOTH;
gridBagConstraints.weightx = 1;
gridBagConstraints.weighty = 1;

when adding the control you want to re-size.

添加要调整大小的控件时。

There's a tutorial about it here:

这里有一个关于它的教程:

http://java.sun.com/docs/books/tutorial/uiswing/layout/gridbag.html

http://java.sun.com/docs/books/tutorial/uiswing/layout/gridbag.html