JScrollPane中组件的面向列的布局 [英] Column-oriented Layout for Component in JScrollPane

查看:133
本文介绍了JScrollPane中组件的面向列的布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发我的第一个Swing应用程序,但是我遇到了一个与对话框窗口有关的布局问题,该对话框窗口是我为用户创建的,以便在某些字段中输入值.由于显示的字段数根据用户的选择而有所不同,因此我在对话框中使用了JScrollPane,将其视口设置为要向其中添加字段组件的面板.

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. 字段名称"标签
  2. 字段组件(通常是JTextField,但也可以是JComboBox或JDateChooser控件)
  3. 字段类型"标签

名称标签:| _ __ _ | (字符串)

namelabel: |____| (String)

name2label:| __ | (数量)

name2label: |__| (Number)

这三个组件的长度都可以变化,所以我面临的挑战是找到一种布局这些组件的整洁方法.我一直在做的是将主窗格的布局管理器设置为使用y轴的BoxLayout(即,它垂直放置组件).然后,我为每个字段创建一个窗格,为该窗格设置布局管理器,然后将所有三个字段组件添加到该窗格.我已经为单个窗格尝试了FlowLayout和BoxLayout,而这两个布局管理器都遇到了问题.

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.

我将FlowLayout管理器设置为使用左对齐,但是由于组件的长度不同,这导致了弯曲的列布局.我将BoxLayout设置为使用X轴(即,水平放置东西),但是组件的居中结果导致每个组件之间的间距很大.在使用单个窗格之前​​,我尝试使用GridLayout,但是我始终无法获得它来满足我的三列要求,从而导致字段被分成几行.我还简要地看了一篇有关GroupLayout管理器的文章,但它看起来很吓人:)

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 :)

对于在JScrollPane中如何以一种整洁,紧凑的方式布置可变长度的三个不同长度的组件的行,有没有人有任何建议?预先感谢...

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...

谢尔顿·R.

推荐答案

这是一个常见问题: MiGLayout 是个不错的选择.另外,还显示了 BoxLayout ="https://stackoverflow.com/a/3175280/230513">此处和此处显示"rel ="nofollow noreferrer"> Group Layout .

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

这篇关于JScrollPane中组件的面向列的布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆