简体   繁体   English

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

[英]Java Swing: Changing border width/height on BorderLayout

I am a newbie to Java Swing. 我是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. 我想让NORTH和EAST边框的宽度相同。 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. 我想知道在BorderLayout中是否有办法改变WEST / EAST边界的宽度或NORTH / SOUTH边界的高度。

Assuming you are already using BorderLayout, you can use panels to control the layout of your frame and create a border feel. 假设您已经在使用BorderLayout,您可以使用面板来控制框架的布局并创建边框感。 Then, you can request a preferred size using setPreferredSize(new Dimension(int, int)) where the (int, int) is width and height, respectively. 然后,您可以使用setPreferredSize(new Dimension(int,int))请求首选大小,其中(int,int)分别是width和height。 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). 这将是一个大小(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 您可以在此处找到另一个很好的setPreferredSize用法示例: https//stackoverflow.com/a/17027872

How about reading the documentation? 阅读文档怎么样?

http://docs.oracle.com/javase/6/docs/api/java/awt/BorderLayout.html#BorderLayout%28int,%20int%29 : 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. 水平间隙由hgap指定,垂直间隙由vgap指定。

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. GridBagLayout更灵活,但也更复杂。

Building Layouts in Swing is not always easy - maybe using MigLayout (a third party library) would simplify things for you: http://www.miglayout.com/ 在Swing中构建布局并不总是那么容易 - 也许使用MigLayout(第三方库)可以简化您的工作: http//www.miglayout.com/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM