Presentation is loading. Please wait.

Presentation is loading. Please wait.

Layout Managers Layout Manager—an object that decides how components will be arranged in a container Some types of layout managers: BorderLayout FlowLayout.

Similar presentations


Presentation on theme: "Layout Managers Layout Manager—an object that decides how components will be arranged in a container Some types of layout managers: BorderLayout FlowLayout."— Presentation transcript:

1 Layout Managers Layout Manager—an object that decides how components will be arranged in a container Some types of layout managers: BorderLayout FlowLayout GridLayout LAYOUT MANAGERS: Arranges the items you add according to certain rules. Different layout managers follow different rules, so you can have a good deal of control over how things are arranged in a windowing interface. DEFAULT LAYOUT MANAGERS: If you do not add a layout manager, then a default layout manager will be provided for you. BorderLayout If you specify that you want to add an object to the window and you do not specify where you want the object to be place, the object would be added to the CENTER.

2 The Border Layout Manager
Five regions that can each have one component added to them: BorderLayout.NORTH BorderLayout.SOUTH BorderLayout.CENTER BorderLayout. WEST EAST contentPane.setLayout(new BorderLayout()); . . . contentPane.add(label1, BorderLayout.NORTH); contentPane is the name of the Container object that you would have created further up in this program. The CENTER region grows the most when the container grows and shrinks the most when the container shrinks

3 Example of BorderLayout
setTitle("Layout Demonstration"); Container contentPane = getContentPane(); contentPane.setLayout(new BorderLayout()); JLabel label1 = new JLabel("First label here"); contentPane.add(label1, BorderLayout.NORTH); JLabel label2 = new JLabel("Second label here"); contentPane.add(label2, BorderLayout.SOUTH); JLabel label3 = new JLabel("Third label here"); contentPane.add(label3, BorderLayout.CENTER); JLabel label4 = new JLabel("4TH label here"); contentPane.add(label4, BorderLayout.WEST);

4 The Flow Layout Manager
The simplest layout manager Displays components from left to right in the order they are added to the container add method has one parameter which is the component to add Container contentPane = getContentPane(); contentPane.setLayout(new FlowLayout()); JLabel label1 = new JLabel(“First label here”); contentPane.add(label1); JLabel label2 = new JLabel(“Second label there”); contentPane.add(label2); FLOW LAYOUT: Simplest layout manager. The objects are arranged one after the other going from left to right

5 Example of FlowLayout setTitle("Layout Demonstration");
Container contentPane = getContentPane(); contentPane.setLayout(new FlowLayout()); JLabel label1 = new JLabel("First label here"); contentPane.add(label1); JLabel label2 = new JLabel("Second label here"); contentPane.add(label2); JLabel label3 = new JLabel("Third label here"); contentPane.add(label3); JLabel label4 = new JLabel("4TH label here"); contentPane.add(label4);

6 The Grid Layout Manager
Specify a number of rows and columns All regions in the grid are equal size When the container changes size, each region grows or shrinks by the same amount contentPane.setLayout(new GridLayout(2, 3)); . . . contentPane.add(label1); contentPane.add(label2); Creates a grid layout with two rows and three columns. Rows are filled before columns.

7 Example of GridLayout contentPane.setLayout(new GridLayout(2,2));
JLabel label1 = new JLabel("First label here"); contentPane.add(label1); JLabel label2 = new JLabel("Second label here"); contentPane.add(label2); JLabel label3 = new JLabel("Third label here"); contentPane.add(label3); JLabel label4 = new JLabel("4TH label here"); contentPane.add(label4);

8 EVENTS, LISTENERS & LISTENER METHODS
Event Class Listener Interface Listener Methods (Handlers) ActionEvent ActionListener actionPerformed (ActionEvent e) ItemEvent ItemListener itemStateChanged(ItemEvent e) WindowEvent WindowListener windowClosing(WindowEvent e) windowOpened(WindowEvent e) windowIconified(WindowEvent e) windowDeiconified(WindowEvent e) windowActivated(WindowEvent e) windowDeactivated(WindowEvent e) ContainerEvent ContainerListener componentAdded(ContainerEvent e) componentRemoved(ContainerEvent e)

9 Listener Methods (Handlers)
Event Class Listener Interface Listener Methods (Handlers) ComponentEvent FocusEvent TextEvent KeyEvent MouseEvent AdjustmentEvent


Download ppt "Layout Managers Layout Manager—an object that decides how components will be arranged in a container Some types of layout managers: BorderLayout FlowLayout."

Similar presentations


Ads by Google