Java Swing:更改 BorderLayout 上的边框宽度/高度

发布于 2025-01-05 22:19:32 字数 153 浏览 1 评论 0原文

我是 Java Swing 的新手。我正在尝试制作一个包含三个按钮的框架;一个在中间,另一个在顶部,最后一个在右侧。我想让北边界和东边界具有相同的宽度。但现在,东部边界比北部边界更宽。

我想知道是否有办法在 BorderLayout 中更改西/东边框的宽度或北/南边框的高度。

I am a newbie to Java Swing. I am trying to make a frame containing three buttons; one in the center, another on the top, and the last one on the right. I want to make the NORTH and EAST borders the same width. But right now, the EAST border is wider than the NORTH border.

I was wondering if there was a way of changing the width of the WEST/EAST borders or the height of the NORTH/SOUTH borders, in BorderLayout.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

慢慢从新开始 2025-01-12 22:19:32

假设您已经在使用 BorderLayout,您可以使用面板来控制框架的布局并创建边框感觉。然后,您可以使用 setPreferredSize(new Dimension(int, int)) 请求首选尺寸,其中 (int, int) 分别是宽度和高度。边界的代码将如下所示:

JPanel jLeft = new JPanel();
JPanel jRight = new JPanel();
JPanel jTop = new JPanel();
JPanel jBottom = new JPanel();

add(jLeft, "West");
jLeft.setPreferredSize(new Dimension(40, 480));

add(jRight, "East");
jRight.setPreferredSize(new Dimension(40, 480));

add(jTop, "North");
jTop.setPreferredSize(new Dimension(640, 40));

add(jBottom, "South");
jBottom.setPreferredSize(new Dimension(640, 40));

上面的示例要求所有边界具有相同的厚度,因为东边界和西边界的宽度与北边界和南边界的高度相匹配。这适用于尺寸为 (640, 480) 的框架。然后,您可以使用如下方式将按钮添加到框架中:

JButton button = new JButton();
jTop.add(button);
button.setPreferredSize(new Dimension(60, 20));

您可以在此处找到 setPreferredSize 用法的另一个很好的示例:https://stackoverflow。 com/a/17027872

Assuming you are already using BorderLayout, you can use panels to control the layout of your frame and create a border feel. Then, you can request a preferred size using setPreferredSize(new Dimension(int, int)) where the (int, int) is width and height, respectively. The code for the borders would then look something like this:

JPanel jLeft = new JPanel();
JPanel jRight = new JPanel();
JPanel jTop = new JPanel();
JPanel jBottom = new JPanel();

add(jLeft, "West");
jLeft.setPreferredSize(new Dimension(40, 480));

add(jRight, "East");
jRight.setPreferredSize(new Dimension(40, 480));

add(jTop, "North");
jTop.setPreferredSize(new Dimension(640, 40));

add(jBottom, "South");
jBottom.setPreferredSize(new Dimension(640, 40));

The example above requests all borders to have the same thickness, since the width of the East and West borders matches the height of the North and South borders. This would be for a frame of size (640, 480). You can then add your buttons to the frame using something like this:

JButton button = new JButton();
jTop.add(button);
button.setPreferredSize(new Dimension(60, 20));

You can find another good example of the setPreferredSize usage here: https://stackoverflow.com/a/17027872

听不够的曲调 2025-01-12 22:19:32

据我所知,您不能直接设置边框区域的高度/宽度。
您只能指定放置在这些区域内的组件的大小。

但是,正如已经提到的,您可以指定区域之间的间隙。

GridBagLayout 更灵活,但也更复杂。

在 Swing 中构建布局并不总是那么容易 - 也许使用 MigLayout(第三方库)会为您简化事情:http:// www.miglayout.com/

As far as I know, you cannot directly set the height/width of the Border areas.
You can only specify the size of the components you place within those areas.

But, as already mentioned, you can specify the gap between the areas.

GridBagLayout is more flexible, but also more complicated.

Building Layouts in Swing is not always easy - maybe using MigLayout (a third party library) would simplify things for you: http://www.miglayout.com/

浸婚纱 2025-01-12 22:19:32

阅读文档怎么样?

http:// docs.oracle.com/javase/6/docs/api/java/awt/BorderLayout.html#BorderLayout%28int,%20int%29

用组件之间的指定间隙构造边框布局。
水平间隙由 hgap 指定,垂直间隙为
由vgap指定。

How about reading the documentation?

http://docs.oracle.com/javase/6/docs/api/java/awt/BorderLayout.html#BorderLayout%28int,%20int%29:

Constructs a border layout with the specified gaps between components.
The horizontal gap is specified by hgap and the vertical gap is
specified by vgap.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文