Presentation is loading. Please wait.

Presentation is loading. Please wait.

Layout Mangers CSC 171 FALL 2001 LECTURE 14. History: The Transistor 1947 - William Shockley, John Bardeen, and Walter Brattain invent the transfer resistance.

Similar presentations


Presentation on theme: "Layout Mangers CSC 171 FALL 2001 LECTURE 14. History: The Transistor 1947 - William Shockley, John Bardeen, and Walter Brattain invent the transfer resistance."— Presentation transcript:

1 Layout Mangers CSC 171 FALL 2001 LECTURE 14

2 History: The Transistor 1947 - William Shockley, John Bardeen, and Walter Brattain invent the transfer resistance device, later to be known as the transistor that will revolutionize the computer and give it the reliability that could not achieved with vacuum tubes.

3 Layout Mangers Java supports several predefined ways to control and manage layout Several

4 FLOW LAYOUT Like a typewriter – Starts upper left – Moves right – Moves down

5 JFrame Application public class FlowLayoutDemo extends JFrame { public static void main( String args[] ){ FlowLayoutDemo application = new FlowLayoutDemo(); application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } …..

6 Instance Variables private JButton leftButton, centerButton, rightButton; private Container container; private FlowLayout layout;

7 Constructor public FlowLayoutDemo() { super( "FlowLayout Demo" ); layout = new FlowLayout(); container = getContentPane(); container.setLayout( layout );

8 Buttons – inner class listener leftButton = new JButton( "Left" ); leftButton.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent event ) { layout.setAlignment( FlowLayout.LEFT ); layout.layoutContainer( container ); } } // end anonymous inner class ); // end call to addActionListener container.add( leftButton ); // add it

9 Visibility Controls setSize( 300, 75 ); setVisible( true );

10 BOARDER LAYOUT Based on a “NORTH, SOUTH, EAST, WEST, CENTER” concept. Elements added to specific regions

11 BorderLayout layout = new BorderLayout( 5, 5 ); // get content pane and set its layout Container container = getContentPane(); container.setLayout( layout );

12 Placing Buttons // place buttons in BorderLayout; order not important container.add( buttons[ 0 ], BorderLayout.NORTH ); container.add( buttons[ 1 ], BorderLayout.SOUTH ); container.add( buttons[ 2 ], BorderLayout.EAST ); container.add( buttons[ 3 ], BorderLayout.WEST ); container.add( buttons[ 4 ], BorderLayout.CENTER );

13 Listener as Interface public class BorderLayoutDemo extends JFrame implements ActionListener { public void actionPerformed( ActionEvent event ) { for ( int count = 0; count < buttons.length; count++ ) if ( event.getSource() == buttons[ count ] ) buttons[ count ].setVisible( false ); else buttons[ count ].setVisible( true ); layout.layoutContainer( getContentPane() ); }

14 GRID LAYOUT Like a table Elements adder in row major order

15 Gridlayout grid1 = new GridLayout( 2, 3, 5, 5 );// 2x3 grid2 = new GridLayout( 3, 2 ); // 3x2 // Items added in order

16 Switching Layouts public void actionPerformed( ActionEvent event ) { if ( toggle ) container.setLayout( grid2 ); else container.setLayout( grid1 ); toggle = !toggle; // set toggle to opposite value container.validate(); }

17 Panels Using panels we can use different layout managers for different regions of the screen

18 Set up Panels // set up panel and set its layout bPanel2 = new JPanel(); bPanel2.setLayout( new BorderLayout( 10, 5) ); // set up panel and set its layout buttonPanel = new JPanel(); buttonPanel.setLayout( new GridLayout( 1, buttons.length ) );

19 Add Panels to frame Container container = getContentPane(); container.add(buttonPanel,BorderLayout.SOUTH); container.add( bPanel2, BorderLayout.CENTER); THINK: containment structure panels within panels within panels....


Download ppt "Layout Mangers CSC 171 FALL 2001 LECTURE 14. History: The Transistor 1947 - William Shockley, John Bardeen, and Walter Brattain invent the transfer resistance."

Similar presentations


Ads by Google