如何使用 Java Swing 布局整齐地缩进一些组件

发布于 2024-10-10 16:54:41 字数 338 浏览 0 评论 0原文

使用 Swing,在复选框或单选按钮下方缩进某些组件的最佳方法是什么?我需要按照 Firefox 3.6 的“选项”->“隐私”对话框的样式进行一些操作,其中一些复选框在“主”复选框下缩进。我可以使用任何标准 AWT/Swing 布局,包括 GroupLayout。我还可以使用 JGoodies FormLayout。我一开始尝试在 FormLayout 中使用 setLeadingColumn 偏移量,但似乎除非我在分隔符下缩进,否则它不会很好地工作。也许我只是做错了?

有没有像SWT GridLayout 的horizo​​ntalIndent 设置一样的东西?那将是完美的。

我正在使用 JDK1.6.0_23。

Using Swing, what is the best way to indent some components underneath a checkbox or radio button? I need to make something in the style of Firefox 3.6's Options->Privacy dialog where some checkboxes are indented under a "main" checkbox. I can use any of the standard AWT/Swing layout's including GroupLayout. I also have JGoodies FormLayout available to me. I tried using setLeadingColumn offset in FormLayout at first, but it seemed like it was not going to work well unless I was indenting under a Separator. Maybe I was just doing it wrong?

Is there anything like SWT GridLayout's horizontalIndent setting? That would be perfect.

I am working with JDK1.6.0_23.

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

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

发布评论

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

评论(4

望笑 2024-10-17 16:54:41

为子组件创建 JPanel。然后,您可以向面板添加一个具有所需缩进的 EmptyBorder。

Create a JPanel for the sub components. Then you can add an EmptyBorder to the panel with the required indentation.

月隐月明月朦胧 2024-10-17 16:54:41

有多种方法可以执行此操作:

  1. 将每个组件边框设置为:new EmptyBorder (0, 10, 0, 0)

  2. 使用 GridBagLayout 并使用 Inset (0, 10, 0, 0) 填充左侧。

  3. 使用 GridBagLayout 并使主复选框跨越两列,而子复选框通过将它们放置在最右边的列中来偏移。

  4. 提供在左侧边缘添加一些空白区域的自定义复选框图标。

我的建议是学习 GridBagLayout - 它使用起来有点笨拙,但它确实为您提供了几乎所有您想要的布局功能 当您需要特定的自动列大小调整行为时,JGoodies 的东西非常有用,而如果您没有一些额外的代码,GBL 不会为您提供这种行为。

There are several ways to do this:

  1. Set each components border to: new EmptyBorder (0, 10, 0, 0).

  2. Use a GridBagLayout and use an Inset (0, 10, 0, 0) to pad the left side.

  3. Use a GridBagLayout and have the main checkbox span two columns, whilst the sub checkboxes are offset by placing them in the rightmost column.

  4. Supply custom checkbox icons that have some empty space added to their left hand edges.

etc.

My advice would be to learn the GridBagLayout - it is somewhat unwieldy to use but it does give you pretty much all the layout power you could want. The JGoodies stuff is useful for when you want particular automatic column sizing behaviour that GBL won't give you without some additional code on your part.

時窥 2024-10-17 16:54:41

您应该能够使用 FormLayout 很好地完成此任务,只需为子项添加另一列,并使主项跨越 2 列。

You should be able to accomplish this just fine with FormLayout, just add another column for the sub items, and have the main item span 2 columns.

嘿看小鸭子会跑 2024-10-17 16:54:41

另一种选择是使用 SpringLayout 并添加填充。

Another option is to use SpringLayout and add padding.

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