简体   繁体   English

如何获得BorderLayout的西方和东方?

[英]How to get the west and east of a BorderLayout attached?

This is how a panel with a BorderLayout usually looks : 这是带有BorderLayout的面板通常的外观:

在此处输入图片说明

I dont want to put anything in the center, so the east and west are separated with a space. 我不想在中心放置任何东西,所以东西之间用空格隔开。 How to get them attached? 如何使它们附着? If there isn't a way, what other Layout should I use instead? 如果没有办法,我应该使用其他什么布局? Thank you 谢谢

You can style this several ways without using BorderLayout, which is what I would recommend since BorderLayout is designed with content in the center in mind. 您可以使用多种方式设置样式,而无需使用BorderLayout,这是我的建议,因为BorderLayout的设计考虑了内容的中心。

The least complex to present West and East together with no Center is a GridLayout with 1 row and 2 columns. 在没有中心的情况下显示西方和东方的最简单的方法是具有1行2列的GridLayout

If you have a North and South you still want to include, you can then place the GridLayout from above into the 2nd row of a 1 column x 3 row GridLayout. 如果您仍然需要包含北部和南部,则可以从上方将GridLayout放入1列x 3行GridLayout的第二行中。

If you want more complexity and flexibility, you can use a GridBagLayout , which is akin to a super GridLayout. 如果您想要更多的复杂性和灵活性,可以使用GridBagLayout ,它类似于超级GridLayout。 You can then set a 2 col, 3 row setup, and give North and south 2 columns in their respective rows, and then East and West get a single cell of their own. 然后,您可以设置一个2列,3行的设置,并在各自的行中分别给North和South 2列,然后East和West获得自己的单个单元格。

You can simply add graphical elements in the desired sections and ignore the others: 您可以在所需的部分中简单地添加图形元素,而忽略其他元素:

add(element1, BorderLayout.WEST);
add(element2, BorderLayout.EAST);

If you want a space in the center, add it explicitly: 如果要在中间留一个空间,请明确添加:

add(new JLabel(" "), BorderLayout.CENTER);

Add a panel to the Centre position of your border layout with the grid layout with 2 columns and then add the east and west contents to the panel. 将面板添加到具有2列的网格布局的边框布局的“中心”位置,然后将东西方的内容添加到面板中。

This approach is good if you want both the West and East to take up half the display each in terms of width. 如果您希望西方和东方都占据宽度一半的显示,这种方法很好。

Similar to Compass' answer. 类似于指南针的答案。

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

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