简体   繁体   中英

How do I neatly indent some components using Java Swing layouts

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.

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

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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