LAYOUT MANAGERS - Kansas State University

2y ago
19 Views
3 Downloads
240.51 KB
8 Pages
Last View : 1m ago
Last Download : 3m ago
Upload by : Genevieve Webb
Transcription

LAYOUT MANAGERSA layout manager controls how GUI components are organized within a GUI container. EachSwing container (e.g. JFrame, JDialog, JApplet and JPanel) is a subclass ofjava.awt.Container and so has a layout manager that controls .JDialogjavax.swing.JAppletLayout ManagersPage 1

Flow LayoutThe simplest layout manager is java.awt.FlowLayout, which adds components to thecontainer from left-to-right, top-to-bottom. It is the default layout for GUI container objects ofclasses Applet or JPanel.ExampleAssume that an application has built a window with the following code:JFrame win new JFrame( "Layout Demo" );win.setDefaultCloseOperation( JFrame.EXIT ON CLOSE );The code below builds a red label. By default, labels are transparent, so wemust make it opaque for the color to show. The displayed label is shown atright.JLabel east new JLabel( "EAST" );east.setOpaque( true );east.setBackground( Color.RED );Similarly, the code below makes four additional labels of varying colors:JLabel west new JLabel( "WEST" );west.setOpaque( true );west.setBackground( Color.BLUE );JLabel north new JLabel( "NORTH" );north.setOpaque( true );north.setBackground( Color.GREEN );JLabel south new JLabel( " SOUTH" );south.setOpaque( true );south.setBackground( Color.YELLOW );JLabel center new JLabel( " CENTER" );center.setOpaque( true );center.setBackground( Color.ORANGE );Using a flow layout manager for the window, the following code adds the five labels to thewindow.win.setLayout( new FlowLayout( ) );win.add( east );Layout ManagersPage 2

win.add(win.add(win.add(win.add(west );center );north );south );Flow layout adds the labels in the order specified by the code,left to right, top to bottom. The completed window is shownto the right.The flow layout manager makes each component no largerthan it needs to be to display its contents.Border LayoutAnother simple layout manager is java.awt.BorderLayout, which is the default layout forGUI container objects of classes JFrame or JDialog.Border layout splits the GUI container into five areas – east, west, north, south and center –oriented so that north is the top. In the call to the add method, you must specify to which area ofthe container the component is to be added, otherwise it is added to the center area by default.ExampleBorder layout adds the labels to thecontainer areas specified by the call tometh add. The window build by the codebelow is shown to the right.The border layout manager expands thesize of the component to fit its area.win.setLayout( new BorderLayout( ) );win.add( east, BorderLayout.EAST );win.add( west, BorderLayout.WEST );win.add( center, BorderLayout.CENTER );win.add( north, BorderLayout.NORTH );win.add( south, BorderLayout.SOUTH );Layout ManagersPage 3

Grid LayoutThe third simple layout manager is java.awt.GridLayout, which adds components to agrid in row-major order (i.e. left to right and top to bottom). The class has a choice of threeconstructors:Constructors for java.awt.GridLayoutGridLayout( int r, int c )// Build a grid with r rows and c columns.GridLayout( )// Build a grid with 1 row and a column for each added// component.GridLayout( int r, int c, int xgap, int ygap )// Build a grid with r rows and c columns. 'xgap' is the spacing// between columns and 'ygap' is the spacing between rows.ExampleGrid layout adds the labels to the containerusing the grid specified by the call to itsconstructor. The window build by the codebelow is shown to the right.Grid layout makes each component in thegrid equal sized.win.setLayout( new GridLayout( 2, 3, 6, 12 ) );win.add( east );win.add( west );win.add( center );win.add( north );win.add( south );Layout ManagersPage 4

Box Layoutjavax.swing.BoxLayout adds components to the container either vertically orhorizontally. The components do not wrap as with flow layout. For example, a horizontalarrangement stays horizontal even when the container is resized. Here is the class constructor:BoxLayout( Container container, int orientation )For the first argument, pass the content pane of the container that is to use the box layout. Thesecond argument is an integer code indicating vertical or horizontal orientation. TheBoxLayout class provides ease-of-use constants for this argument:Two of the Orientation Constants for BoxLayoutX AXISLay components out horizontally from left to right.Y AXISLay components out vertically from top to bottom.ExampleThe window build by the code below isshown to the right.Container box win.getContentPane( );box.setLayout( new BoxLayout( box, BoxLayout.X AXIS ) );win.add( east );win.add( west );win.add( center );win.add( north );win.add( south );Java provides the class javax.swing.Box, which allows you to build a container whoselayout manager is box layout. Most GUI programmers use this instead ofjavax.swing.BoxLayout because it provides handy methods for configuring the box. Formore information, see the topic javax.swing.Box.Layout ManagersPage 5

Grid Bag Layoutjava.awt.GridBagLayout allows very sophisticated placement of components within acontainer. It’s essentially a grid, but the grid rows and columns may have varying heights andwidths and components may span more than one grid cell. Consequently, it’s a rather challenginglayout manager to learn and use. If you wish to tackle it, see How to Use GridBagLayout in TheJava Tutorials (google java tutorial gridbaglayout).Other Layout Managersjavax.swing.GroupLayout and javax.swing.SpringLayout were created to beused by GUI builder tools such as that found in NetBeans, although you can use them manually.See How to Use GroupLayout and How to Use SpringLayout in The Java Tutorials.Layout ManagersPage 6

Exercises1.Using the code from the firstexample, write a complete Javaprogram (application or applet) thatuses a flow layout manager to buildand display the GUI shown to theright.2.Modify your solution to exercise #1so that it uses a border layoutmanager to build and display theGUI shown to the right.3.In your solution to exercise #2, remove the second parameter from the first two calls to theframe’s add method. Recompile and rerun the application. Observe the displayed GUI.Explain its appearance.4.Modify your solution to exercise #1so that it uses the two-parameterGridLayout constructor to build agrid layout manager that displaysthe GUI shown to the right.5.In your solution to exercise #4,replace the two-parameterGridLayout constructor by thefour-parameter constructor. Theprogram should display the GUIshown to the right. The grid hasvertical padding of 10 andhorizontal padding of 5.6.In your solution to exercise #4, replace the two-parameter GridLayout constructor bythe constructor with no parameters. Recompile and rerun the program. Observe thedisplayed GUI. Explain its appearance.Layout ManagersPage 7

7.Modify your solution to exercise #1so that it uses the grid layoutmanager to build and display theGUI shown to the right.8.Modify your your solution toexercise #1 so that it uses the boxlayout manager to build ahorizontally oriented GUI as shownto the right.9.Modify your your solution toexercise #1 so that it uses the boxlayout manager to build a verticallyoriented GUI as shown to the right.Layout ManagersPage 8

Layout Managers Page 1 LAYOUT MANAGERS A layout manager controls how GUI components are organized within a GUI container. Each Swing container (e.g. JFrame, JDialog, JApplet and JPanel) is a subclass of java.awt.Container and so has a layout manager that controls it. javax.swing.JComponent javax.

Related Documents:

All 13 Layouts use White Daisy CS for bases, so you will need 26 sheets for your layouts. Whisper CS #3 4 x 12 Layout B 4 x 12 Layout B 4 x 12 Layout C Whisper CS #4 4 x 12 Layout C 4 x 12 Layout C 4 x 12 Layout C Saffron Letter B&T #1 (letters facing sideways) 6 x 10 ½ Layout A 6 x 8 Layout A 6 x 4 Layout K 6 x 1 ½ Cricut

ty of Missouri–Kansas City and a historical consultant on Kansas City regional history. He is the author of J. C. Nichols and the Shaping of Kansas City(1993). 1. Craig Miner, Kansas: The History of the Sunflower State, 1854–2000 (Lawrence: University Press of Kansas, 200

Oct 30, 2014 · EE501 Lab 6 Layout and Post-layout Simulation Report due: Oct. 30, 2014 Objective: 1. Practice analog layout techniques 2. Practice post-layout simulation Tasks: 1. Layout the two stage amplifier designed in Lab 4(As shown in Fig 1) Common centroid layout of the fi

701 N 7th Street Room 646 Kansas City, Kansas 66101-3064 (913) 573-5660 Fax (913) 573-5006 Kansas City, Kansas Michael York Chief of Police POLICE DEPARTMENT PATROL OFFICER Job Description and requirements: This is an entry level position responsible for answering calls for service, patrolling, conducting accident investigations and compla

to be thrifty where possible. We are also being cautious about . Eugene Nickel Jefferson City Byron Shaw Jefferson City John Pruss Kansas City . Kansas City Yinan Qi Kansas City Gregory Larson Kansas City Joseph Kaufman Kansas City Meghan Hemenway Kansas City Jay Bettis Kearney Rob Dal

Kansas dust in the wind ukulele tabs Author: Mufotuvo Tihusodu Subject: Kansas dust in the wind ukulele tabs. Kansas dust in the wind ukulele chords. Kansas - dust in the wind chords. Kansas dus Created Date: 1/4/2021 8:17:30 AM

Hispanic Heritage in Kansas City 16 2011 Kansas Archeology Training Program 19 Blue Earth Kansa Indian Village 22 KAA Fall Fling 24 future. National Conservation Award Kansas Preservation Conference The 2012 Kansas Preservation Conference is planned in partnership with the Regional Energy Sustainability Summit and Fair January 25-28, 2012, in .

0452 ACCOUNTING 0452/21 Paper 2, maximum raw mark 120 This mark scheme is published as an aid to teachers and candidates, to indicate the requirements of the examination. It shows the basis on which Examiners were instructed to award marks. It does not indicate the details of the discussions that took place at an Examiners’ meeting before marking began, which would have considered the .