JScrollPane 中组件的面向列的布局

发布于 2025-01-01 23:28:57 字数 876 浏览 0 评论 0原文

我正在开发我的第一个 Swing 应用程序,并且在为用户创建用于在某些字段中输入值的对话框窗口时遇到布局问题。由于显示的字段数量根据用户选择而变化,因此我在对话框中使用 JScrollPane,将其视口设置为向其中添加字段组件的面板。

对于要显示的每个字段,我创建并添加三个组件:

  1. “字段名称”标签
  2. 字段组件(通常是 JTextField,但也可以是 JComboBox 或 JDateChooser 控件)
  3. “字段类型”标签,

名称标签: | ____| (字符串)

名称2标签:|__| (数字)

所有这三个组件的长度都可以不同,因此我的挑战是找到一种整齐的方式来布局这些组件。我一直在做的是将主窗格的布局管理器设置为使用 y 轴的 BoxLayout(即垂直布局组件)。然后,我为每个字段创建一个窗格,设置该窗格的布局管理器,然后将所有三个字段组件添加到该窗格。我已经为各个窗格尝试了 FlowLayout 和 BoxLayout,但我在使用这两个布局管理器时都遇到了问题。

我将 FlowLayout 管理器设置为使用左对齐,但由于组件的长度不同,这导致了弯曲的列布局。我将 BoxLayout 设置为使用 X 轴(即水平布局),但随后的组件居中导致每个组件之间的间距很大。在使用单独的窗格之前,我尝试使用 GridLayout,但我始终无法让它满足我的三列要求,导致字段跨行拆分。我还简单地浏览了一篇关于 GroupLayout 管理器的文章,但它看起来很吓人:)

有没有人对如何以整洁、紧凑的方式在 JScrollPane 中布局不同长度的三个组件的不同行数有任何建议?预先感谢...

Sheldon R.

I'm working on my first Swing application and I'm having a layout problem concerning a dialog window I created for my users to enter values in certain fields. Because the number of fields displayed varies based on a user choice, I use a JScrollPane in my dialog, setting its viewport to a panel to which I add my field components.

For every field to be displayed, I create and add three components:

  1. "field name" label
  2. Field component (usually a JTextField, but it also could be a JComboBox or a JDateChooser control)
  3. "field type" label

i.e.

namelabel: |____| (String)

name2label: |__| (Number)

All three of these components can be of varying lengths, so my challenge has been to find a tidy way to layout these components. What I've been doing is setting the layout manager for the main pane to be a BoxLayout that uses the y-axis (i.e. it lays out components vertically). I then create a pane for each field, set the layout manager for that pane then add all three field components to that pane. I've tried both a FlowLayout and BoxLayout for the individual panes, and I've had issues with both of those layout managers.

I set the FlowLayout manager to use a left justification, but due to the varying lengths of the components, this led to a crooked-column layout. I set the BoxLayout to use the X-axis (i.e. lay things out horizontally) but the consequent centering of the components resulted in a vast spacing between each component. And prior to using individual panes, I tried to use GridLayout but I was never able to get it to honour my three-column requirement, causing the fields to be split across rows. I also looked briefly at an article about the GroupLayout manager but it seemed intimidating :)

Does anyone have any suggestions on how to layout a varying number of rows of three components of varying length within a JScrollPane in a neat, compact way? Thanks in advance...

Sheldon R.

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

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

发布评论

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

评论(3

给不了的爱 2025-01-08 23:28:57

这是一个常见问题: MiGLayout 是一个不错的选择。或者, BoxLayout 此处进行了说明,并且组布局 显示 这里

It's a common problem: MiGLayout is a good choice. Alternatively, BoxLayout is illustrated here, and Group Layout is shown here.

无风消散 2025-01-08 23:28:57

另请查看 SpringLayout

Also take a look at SpringLayout.

儭儭莪哋寶赑 2025-01-08 23:28:57

更新:我的三列想法没有成功,原因与我的大多数其他想法没有成功的原因相同,即 BoxLayout,像大多数布局管理器一样,倾向于扩展组件以填充尽可能多的空间,所以我的字段被渲染得非常巨大:)

所以我硬着头皮尝试找出 GroupLayout,基于 @trashgod 所示的示例,该示例与我想要实现的目标类似。在弄清楚如何以 GroupLayout 方式完成我想要的事情之后,我最初遇到了相同的扩展字段问题。然后,Oracle GroupLayout 教程向我展示了如何防止组件调整大小,即使用 addcomponent 方法的四参数版本:addComponent(field, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)我试过了,效果很神奇。再次感谢 @trashcan 为我指明了正确的方向,并感谢其他提出想法的人......

Sheldon R.

Update: My three-column idea didn't work out for the same reason most of my other ideas didn't work i.e. BoxLayout, like most of the layout managers, tends to expand the component to fill as much space as it can, so my fields were being rendered as enormous :)

So I bit the bullet and tried to figure out GroupLayout, based on the example shown by @trashgod being similar to what I was trying to achieve. After figuring out how to do what I wanted in the GroupLayout way, I initially ran into the same expanding-field issue. Then the Oracle GroupLayout tutorial showed me how to keep the components from being resized i.e. using the four-argument version of the addcomponent method: addComponent(field, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) I tried that and it worked like a charm. Thanks again, @trashcan for pointing me in the right direction, and thanks to everyone else who chimed in with ideas...

Sheldon R.

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