LayoutManagers Overview

     

LayoutManager.getDefinition() : //returns: Layout managers provide different metaphors for organizing the atomic components within a content pane. Layout Managers include FlowLayout, BorderLayout, BoxLayout, CardLayout, GridLayout, and GridBagLayout. You can create your own layout managers, or use a null layout manager, which requires you to position each component using absolute pixel values. FlowLayout is used as the default layout manager if you do not specify a particular one you'd like to use.

LayoutManager.stepInto() { We will step into layout managers for a moment here so that we can forget about them for the rest of the topic. They are the underlying arranger of the space, so it is appropriate to start with them. Just don't get too bogged down by them. I'll try to keep it peppy.

If you have used HTML to make a Web page, using layout managers is a bit like using tables and CSS to position different elements on your page. Layout managers dictate (usually) the size and position of components within the UI.

You set the layout of a panel using the setLayout() method, as in the following:

 

 //get the main content pane of your frame: Container pane = frame.getContentPane(); //make this pane to use border layout pane.setLayout(new BorderLayout()); //put a component in a region pane.add(new JtextField(10), BorderLayout.EAST); 

Let's look at each layout manager. To do so, we will create a totally generic frame using the BasicFrame class so we don't have to retype a bunch of code for each example.

BasicFrame.java

 

 package net.javagarage.demo.swing.layouts; import javax.swing.JFrame; /** <p> Just for use in demo-ing different Layout managers. Implements the singleton pattern. **/ public class BasicFrame extends JFrame { private static BasicFrame frame; private BasicFrame() { super("My Frame"); } public static BasicFrame getInstance() { if (frame == null) frame = new BasicFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); return frame; } } 



Java Garage
Java Garage
ISBN: 0321246233
EAN: 2147483647
Year: 2006
Pages: 228
Authors: Eben Hewitt

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net