130 likes | 262 Views
COSC-4840 Software Engineering. Lab Session 5 Layout Managers in Java Or How Do I Get My GUI to Look Right? Prepared by Nadya Kuzmina Based on Creating a GUI with JFC/Swing Trail in The Java Tutorial. Layout Managers.
E N D
COSC-4840 Software Engineering Lab Session 5 Layout Managers in Java Or How Do I Get My GUI to Look Right? Prepared by Nadya Kuzmina Based on Creating a GUI with JFC/Swing Trail in The Java Tutorial
Layout Managers • A layout manager determines positions of visual components within their container. • It is specified on the container.
BorderLayout [1] • A BorderLayout places components in up to five areas: top, bottom, left, right, and center. • All extra space is placed in the center area.
BoxLayout [1] • The BoxLayout class puts components in a single row or column. • It respects the components' requested maximum sizes and also lets you align components.
CardLayout [1] • The CardLayout class lets you implement an area that contains different components at different times. • A CardLayout is often controlled by a combo box, with the state of the combo box determining which panel (group of components) the CardLayout displays.
FlowLayout [1] • FlowLayout is the default layout manager for every JPanel. • It simply lays out components in a single row, starting a new row if its container isn't sufficiently wide.
GridBagLayout [1] • GridBagLayout is a sophisticated, flexible layout manager. • It aligns components by placing them within a grid of cells, allowing some components to span more than one cell. • The rows in the grid can have different heights, and grid columns can have different widths.
GridLayout [1] • GridLayout simply makes a bunch of components equal in size and displays them in the requested number of rows and columns.
FlowLayout [1] • FlowLayout puts components in a row, sized at their preferred size. • If the horizontal space in the container is too small to put all the components in one row, FlowLayout uses multiple rows. • If the container is wider than necessary for a row of components, the row is, by default, centered horizontally within the container.
BorderLayout [1] • A BorderLayout has five areas. • These areas are specified by the BorderLayout constants NORTH, WEST, EAST, SOUTH, and CENTER. • It is recommended that you specify the component's location (for example, BorderLayout.SOUTH) as one of the arguments to the add method. If you leave it out, the component will be added to the center, but your code will be much less clear. • If you find that a component is missing from a container controlled by a BorderLayout, make sure that you specified the component's location and that you didn't put another component in the same location.
GridBagLayout [1] • GridBagLayout is one of the most flexible — and complex — layout managers the Java platform provides. • A GridBagLayout places components in a grid of rows and columns, allowing specified components to span multiple rows or columns. • Not all rows necessarily have the same height. Similarly, not all columns necessarily have the same width. • Essentially, GridBagLayout places components in rectangles (cells) in a grid, and then uses the components' preferred sizes to determine how big the cells should be.
GridBagLayout Constraints • GridBagConstraints gridBagConstraints4 = new GridBagConstraints(); • gridBagConstraints4.insets = new Insets(20, 1, 22, 6); • gridBagConstraints4.gridx = 1; • gridBagConstraints4.gridy = 2; • gridBagConstraints4.ipadx = 23; • gridBagConstraints4.ipady = 6; • gridBagConstraints4.gridwidth = 2;
References. [1] The Java Tutorial: Creating a GUI with JFC/Swing Trail (http://java.sun.com/docs/books/tutorial/reallybigindex.html#uiswing).