Presentation is loading. Please wait.

Presentation is loading. Please wait.

G5BUID - Java Swing - 2005 1 Laying out components Manage realized components Determine size and position Each container has a layout manager (usually)

Similar presentations


Presentation on theme: "G5BUID - Java Swing - 2005 1 Laying out components Manage realized components Determine size and position Each container has a layout manager (usually)"— Presentation transcript:

1 G5BUID - Java Swing Laying out components Manage realized components Determine size and position Each container has a layout manager (usually)

2 G5BUID - Java Swing Layout managers – general aspects Creating a layout manager Consulting managers Types of managers Choosing managers Other features of component layout All Covered very well here:

3 G5BUID - Java Swing Creating a layout manager Default layout managers JFrame, JDialog, JApplet have BorderLayout JPanel has FlowLayout Except when used as a Content Pane (Border Layout) Setting the layout manager for a container JFrame frame = new JFrame(); frame.setLayout(new FlowLayout()); JPanel contentPane = new JPanel(); contentPane.setLayout(new BorderLayout());

4 G5BUID - Java Swing Consulting layout managers (1) Consulted automatically when container may need to change its appearance. These methods result in consultation, but DONT trigger new layout add(), remove(), removeAll() getAlignmentX(), getAlignmentY() getPreferredSize(), getMinimumSize(), getMaximumSize()

5 G5BUID - Java Swing Consulting layout managers (2) These methods actually result in the manager performing layout. JFrame.pack(); Causes this Window to be sized to fit the preferred size and layouts of its subcomponents. JFrame.show() & JFrame.setVisible(true); Shows the component JComponent.revalidate(); This method will automatically be called on this component when a property value changes. Looks for all dependent components and calls validate() on them. Validate() causes a container to lay out its subcomponents again

6 G5BUID - Java Swing Layout managers - types BorderLayout BoxLayout FlowLayout GridLayout GridBagLayout CardLayout

7 G5BUID - Java Swing BorderLayout Five areas NORTH, SOUTH, EAST, WEST and CENTER Not all areas must be used Do not assume a default area for components Centre gets as much area as possible Specify location as argument of add method pane.setLayout(new BorderLayout()); pane.add(new JButton(Button 1 (NORTH)), BorderLayout.NORTH); Setting gaps between components (default = 0) BorderLayout.setHgap(int gap); BorderLayout.setVgap(int gap); BorderLayout(int horizontalGap, int verticalGap) - Constructor

8 G5BUID - Java Swing BoxLayout (1) Components on top / next to each other Direction is your choice Tries to size components at preferred height for Y_AXIS or width for X_AXIS Width as largest component width See above picture

9 G5BUID - Java Swing FlowLayout Very simple - JPanels default Components in row(s) At preferred size Alignment FlowLayout.LEFT FlowLayout.CENTRE FlowLayout.RIGHT Gaps Default = 5 Specifying - setter hGap vGap methods or via constructor

10 G5BUID - Java Swing GridLayout Grid of cells - all same size Components take all space in a cell Gaps default = 5 use setter methods hGap and vGap or via arguments to constructor Re-sizing Cells resize to be as large as possible in given window / container


Download ppt "G5BUID - Java Swing - 2005 1 Laying out components Manage realized components Determine size and position Each container has a layout manager (usually)"

Similar presentations


Ads by Google