gpt4 book ai didi

java - 在 Swing 中使用哪个面板

转载 作者:行者123 更新时间:2023-11-29 10:18:24 25 4
gpt4 key购买 nike

我希望在一个面板中有一个 JTable 组件,以占用窗口允许的尽可能多的空间。然而,在下面,我想添加另一个固定大小的面板,其中有几个按钮。使用哪种布局允许底部面板具有固定大小,同时允许 JTable 面板根据窗口大小进行拉伸(stretch)?

最佳答案

很难想象您到底想要什么,但这里有一段来自 Oracle 的摘录可能对您有所帮助:

场景:您需要在尽可能多的空间内显示一个组件。

If it is the only component in its container, use GridLayout or BorderLayout. Otherwise, BorderLayout or GridBagLayout might be a good match.

If you use BorderLayout, you will need to put the space-hungry component in the center. With GridBagLayout, you will need to set the constraints for the component so that fill=GridBagConstraints.BOTH. Another possibility is to use BoxLayout, making the space-hungry component specify very large preferred and maximum sizes.

场景:您需要以自然大小在紧凑的行中显示几个组件。

Consider using a JPanel to group the components and using either the JPanel's default FlowLayout manager or the BoxLayout manager. SpringLayout is also good for this.

场景:您需要在行和列中显示一些相同大小的组件。

GridLayout is perfect for this.

场景:您需要在一行或一列中显示一些组件,它们之间可能有不同的间距、自定义对齐方式或自定义组件大小。

BoxLayout is perfect for this.

场景:您需要显示对齐的列,如在类似表单的界面中,其中一列标签用于描述相邻列中的文本字段。

SpringLayout is a natural choice for this. The SpringUtilities class used by several Tutorial examples defines a makeCompactGrid method that lets you easily align multiple rows and columns of components.

场景:您有一个包含许多组件的复杂布局。

Consider either using a very flexible layout manager such as GridBagLayout or SpringLayout, or grouping the components into one or more JPanels to simplify layout. If you take the latter approach, each JPanel might use a different layout manager.

来源: Oracle: Using Layout Managers

关于java - 在 Swing 中使用哪个面板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11779530/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com