Java GUI Layouts

别等时光非礼了梦想. 提交于 2019-12-01 01:13:41

Been a while since I worked with Swing, but it looks like the architecture is something like this:

  1. You have a panel in the bottom which is BorderLayout
  2. Inside that, you add a total of 4 new panels, NORTH, WEST, CENTER and EAST
  3. in BorderLayout.NORTH you add a panel which have FlowLayout.LEFT
  4. in BorderLayout.WEST you add a panel which have GroupLayout.YAXIS. this panel contains the labels for names etc and the ENTER button
  5. in BorderLayout.CENTER you add the textfields that corresponds with the labels
  6. in BorderLayout.EAST you add the JSCrollpane.

This might give you an idea and you can play around with these different panels to achive what you want

Use multiple nested layout managers

The example looks like a top-level BorderLayout with scroll pane in the CENTER location, a row of buttons (using FlowLayout) in the NORTH location and a GridLayout for the text fields in the WEST location. The latter could be improved by using a GroupLayout, which allows rows and columns to be sized individually but is somewhat complex to use.

There's a great tutorial on using layout managers (unfortunately it seems to have disappeared from Oracle's servers and the link points to a probably transient copy).

I would just use MigLayout for the high level page layout, and then dropdown to the simple layout managers for the . It's essentially a grid layout, but it's very easy to use. Miglayout cannot wrap items though, and this is apparently a design issue in Swing. WrapLayout is a layout manager that gives you that functionality, but it can have issues.

http://tips4java.wordpress.com/2008/11/06/wrap-layout/

Looking at the screen shot you've provided I've mocked up the way you would want to divide it up. You'd have 9 rows total (0-8) and 3 columns total (0-2). For stuff like the controls at the very top, you will "span" them across all three columns.

For the text, you just put the text inside of it's individual box in row 1, column 1 for First Name, or row 2, column 1 for Last Name, etc.

You do the same thing with the input boxes.

In the picture below the blue are the columns and the orange are the rows.

So to summarize;

  • Use Miglayout for your high level layout. It's like using a table in HTML.
  • Use other layout managers to layout the items inside of the grid boxes that Miglayout provides.

The best choise is using Gridbag layout as you have a bit complex UI and GridBag layout provides all the support you needed to achive the exact UI.You will have to use a parent panel and then the child panels in it.Each panel you will have to add seperate GridBag layouts.You can add insets and necessary growing to achieve what you want.

Gill Rowan

As stated in the previous comment, I would use MigLayout in this project. As you can use split, span and wrap after each field or textbox in order to get the correct layout. You can also debug Miglayout and see where your layout its right or wrong.

download the latest version of MigLayout here : http://www.migcalendar.com/miglayout/versions/

if using eclipse,

  • save it in a folder called Lib in the project folder.
  • configure the project build path but adding the jar file to the classpath.
  • Then you should be able to set the layout to MigLayout.

panel.setLayout(new MigLayout());

or to debug the layout - panel.setLayout(new MigLayout("debug"));

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!