Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 205 Programming II Lecture 7 AWT – Event Handling & Layout.

Similar presentations


Presentation on theme: "CSC 205 Programming II Lecture 7 AWT – Event Handling & Layout."— Presentation transcript:

1 CSC 205 Programming II Lecture 7 AWT – Event Handling & Layout

2 Recap component listener event 1.User performs an action Button b = new Button(“Exit”); 2.An event object is created 0. A listener is created and added to the component b.addActionListener ( listener ); 3. The listener responses to the action public void actionPerformed (ActionEvent evt){ System.exit(0); }

3 Callback A component may add appropriate event listener objects Register themselves to JVM They will get callback when events addressing to them are generated Event listeners are defined through interfaces

4 interface An interface is purely abstract All methods are abstract, and the keyword is not needed No variables may be defined except constants Subtypes need to implement all methods Perfect for type definition You may implement the actionPerformed method in whatever way you need

5 Adapter For each of the event listener interfaces with more than a few methods defined, an adapter class is provided WindowListener  WindowAdapter A WindowAdapter object does nothing except you override the methods You need to override only those methods you care about E.g., windowClosing(WindowEvent env)

6 Inner Classes Classes implementing listener interfaces (or extending adapters) may be specific to a single container class A class like this may be treated as an inner class  declared and implemented within the container class itself When compiled, a separate.class file will be generated with a prefix of the container class

7 Adding Listeners to Components Multiple listeners may be added to one component One listener may be added to multiple components In the event handling methods, the source of the event need to be detected

8 Layout The layout of components within a container remains a mystery. Consider the ChangeColor2 frame: Why are the buttons placed side by side? Why are the buttons centered within the frame?

9 Layout Manager Classes The java.awt package provides five layout manager classes: Class Name Behavior BorderLayout Arranges components along the sides of the container and in the middle. CardLayout Arrange components in “cards.” Only one card is visible at a time. FlowLayout Arranges components in variable-length rows. GridBagLayout Aligns components horizontally and vertically; components can be of different sizes. GridLayout Arranges components in fixed-length rows and columns.

10 The FlowLayout Class Suppose that a frame containing seven buttons uses FlowLayout as its layout manager. The number of buttons that can be squeezed into a row depends on how wide the frame is:

11 The GridLayout Class The GridLayout layout manager places components in rows, with each row (except possibly the last) having an equal number of components:

12 The GridLayout Class If a frame with a GridLayout is resized, the components within the frame change size as well:

13 The GridLayout Class The GridLayout constructor requires that the number of rows and columns be specified: setLayout(new GridLayout(4, 5)); Components will be arranged in four rows and five columns, with no space between components.

14 The BorderLayout Class The positions in a BorderLayout are named North, South, East, West, and Center:

15 The BorderLayout Class The no-arg version of the BorderLayout constructor leaves no space between components: setLayout(new BorderLayout()); A different constructor is used if space is needed between components: setLayout(new BorderLayout(20, 10));

16 If a frame with a BorderLayout is resized, the heights of the “North” and “South” components don’t change: The widths of the “East” and “West” components also remain the same. The BorderLayout Class

17 Panels Consider the problem of creating a frame with the following appearance:

18


Download ppt "CSC 205 Programming II Lecture 7 AWT – Event Handling & Layout."

Similar presentations


Ads by Google