Download presentation
Presentation is loading. Please wait.
Published byGervais Nicholson Modified over 9 years ago
1
March 2005 1R. Smith - University of St Thomas - Minnesota QMCS 230: Today in Class Project Proposals: Due Today!Project Proposals: Due Today! –Identify your task and –(if any) your team mates Layout ManagersLayout Managers –Flow –Border –Grid (We’re skipping Section 7.3)(We’re skipping Section 7.3)
2
March 2005 2R. Smith - University of St Thomas - Minnesota “Why, why why?” Why do we have to put a ‘panel’ inside a ‘window’ when we create a GUI?Why do we have to put a ‘panel’ inside a ‘window’ when we create a GUI? In a sensible world, why bother?In a sensible world, why bother? Both are in fact “Container” objectsBoth are in fact “Container” objects Why isn’t the window enough by itself?Why isn’t the window enough by itself?
3
March 2005 3R. Smith - University of St Thomas - Minnesota Layout Managers The answer to this little mysteryThe answer to this little mystery A ‘standard’ window uses the ‘Border’ layout by default.A ‘standard’ window uses the ‘Border’ layout by default. A ‘standard’ panel uses the ‘Flow’ layout by default.A ‘standard’ panel uses the ‘Flow’ layout by default. We’ve been using ‘Flow.’ What does it do?We’ve been using ‘Flow.’ What does it do?
4
March 2005 4R. Smith - University of St Thomas - Minnesota How ‘Flow’ Works Each GUI component (button, label, text box, etc.) is added left-to-right, top-to-bottomEach GUI component (button, label, text box, etc.) is added left-to-right, top-to-bottom The layout adapts to the window sizeThe layout adapts to the window size Each component keeps its natural sizeEach component keeps its natural size The only ‘controls’ available: alignment, gapsThe only ‘controls’ available: alignment, gaps –FlowLayout.LEFT, FlowLayout.RIGHT, FlowLayout.CENTER –Horizontal and vertical minimum gaps between items
5
March 2005 5R. Smith - University of St Thomas - Minnesota Controlling the Layout Choosing the Layout ManagerChoosing the Layout Manager –JPanel panel = new JPanel(); –panel.setLayout(new BorderLayout()); –A layout manager is an object: you create the kind of manager (object) you want and give it to your container/panel/window Setting parametersSetting parameters –Usually done with the object constructor
6
March 2005 6R. Smith - University of St Thomas - Minnesota FlowLayout constructors FlowLayout(FlowLayout.LEFT) – alignmentFlowLayout(FlowLayout.LEFT) – alignment Alignment affects every item in the containerAlignment affects every item in the container With 3 arguments: FlowLayout(A, B, C)With 3 arguments: FlowLayout(A, B, C) –A: the desired alignment –B: the horizontal gap in pixels –C: the vertical gap in pixels
7
March 2005 7R. Smith - University of St Thomas - Minnesota BorderLayout Manager BorderLayout manages five regions where components can be placed.
8
March 2005 8R. Smith - University of St Thomas - Minnesota BorderLayout Manager New windows are BorderLayout by defaultNew windows are BorderLayout by default A component placed into a container that is managed by a BorderLayout must be placed into one of five regions:A component placed into a container that is managed by a BorderLayout must be placed into one of five regions: –BorderLayout.NORTH –BorderLayout.SOUTH –BorderLayout.EAST –BorderLayout.WEST –BorderLayout.CENTER By default it goes in the center regionBy default it goes in the center region
9
March 2005 9R. Smith - University of St Thomas - Minnesota How BorderLayout Works Each item goes into a specific regionEach item goes into a specific region –What’s the default again? Each region holds ONLY ONE item at a timeEach region holds ONLY ONE item at a time Each item is RESIZED to fit the regionEach item is RESIZED to fit the region –If too small, it expands to fill the region –If too large, it shows as much as it can How do we get around all this?How do we get around all this? –Hint: what’s the default layout for ALL windows?
10
March 2005 10R. Smith - University of St Thomas - Minnesota GridLayout Manager GridLayout creates a grid with rows and columns, much like a spreadsheet. A container that is managed by a GridLayout object is divided into equally sized cells. columns rows
11
March 2005 11R. Smith - University of St Thomas - Minnesota Setting up a GridLayout Constructor takes 2 argumentsConstructor takes 2 arguments –#Rows –#Columns One or other may be 0, but not bothOne or other may be 0, but not both Like BorderLayoutLike BorderLayout –Each region contains ONLY ONE object –The object in a region is RESIZED to fill the region Question: how do we get around this problem?Question: how do we get around this problem?
12
March 2005 12R. Smith - University of St Thomas - Minnesota GridLayout Manager 2524232221 2019181716 1514131211 109876 54321 Components are added to a GridLayout in the following order (for a 5×5 grid):Components are added to a GridLayout in the following order (for a 5×5 grid): Example: GridWindow.java GridWindow.java GridLayout also accepts nested components: Example: GridPanelWindow.java GridPanelWindow.java
13
March 2005 13R. Smith - University of St Thomas - Minnesota That’s it. Questions?Questions? Creative Commons License This work is licensed under the Creative Commons Attribution-Share Alike 3.0 United States License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/us/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.