Java Swing:更改 BorderLayout 上的边框宽度/高度
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设您已经在使用 BorderLayout,您可以使用面板来控制框架的布局并创建边框感觉。然后,您可以使用 setPreferredSize(new Dimension(int, int)) 请求首选尺寸,其中 (int, int) 分别是宽度和高度。边界的代码将如下所示:
上面的示例要求所有边界具有相同的厚度,因为东边界和西边界的宽度与北边界和南边界的高度相匹配。这适用于尺寸为 (640, 480) 的框架。然后,您可以使用如下方式将按钮添加到框架中:
您可以在此处找到 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:
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:
You can find another good example of the setPreferredSize usage here: https://stackoverflow.com/a/17027872
据我所知,您不能直接设置边框区域的高度/宽度。
您只能指定放置在这些区域内的组件的大小。
但是,正如已经提到的,您可以指定区域之间的间隙。
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/
阅读文档怎么样?
http:// docs.oracle.com/javase/6/docs/api/java/awt/BorderLayout.html#BorderLayout%28int,%20int%29:
How about reading the documentation?
http://docs.oracle.com/javase/6/docs/api/java/awt/BorderLayout.html#BorderLayout%28int,%20int%29: