{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

1 when an event occurs it generates an actionevent

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: onding to events, continued Uses event delegation model of JDK 1.1 When an event occurs, it generates an ActionEvent object WindowListener interface observes events triggered by Window object, such as closing it, and responds in corresponding methods ActionListener interface listens for a particular ActionEvent Responds in its actionPerformed method Program now has a live Button: actionPerformed method rings a bell Most Components in the AWT have corresponding Listeners Also a live close window button, which performs System.exit(0) Adapter Classes Time consuming to define all interface methods WindowListener has seven methods What if we only want to use one? Required to define all methods in interface Implements an interface Default implementation ({ }, empty body) for all methods You then extend adapter class, overriding methods for events you care about, such as windowClosing. Has "is a" relationship with interface WindowAdapter is a WindowListener MouseAdapter is a MouseListener Adapter class Sketchpad example See Sketchpad.java (H:\oose\java, java Sketchpad) Layout managers JDK provides a set of generic layout manager classes a 800x600 screen Sketchpad uses hard-coded layout, which depends on Arrange Component objects within a Container object in predictable ways FlowLayout (the default) add components one after another in rows: GridLayout places components in cells of a grid: setLayout(new FlowLayout(FlowLayout.LEFT,10,10); for (int counter=1; counter <= 6; counter++) setLayout(new Button(String.valueOf(counter))); add(new GridLayout(3,2,5,5); //3 rows, 2 columns, 5 pixel gaps for (int counter=1; 1 2 3 <= 6; counter++) counter add(new Button(String.valueOf(counter))); 4 1 3 5 5 6 2 4 6 BorderLayout arranges components using along four sides (North, South, East West) and Center positions Swing overview Defined in package javax.swing Original GUI components from AWT in java.awt Heavyweight components - rely on local platform's windowing system for look and feel Swing components are lightweight Not weighed down by GUI capabilities of platform More portable than heavyweight components Swing components allow programmer to specify look and feel Can change depending on platform Can be same across all platforms Swing component inhe...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online