Presentation is loading. Please wait.

Presentation is loading. Please wait.

UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 15 Java Fundamentals Java2 Graphical User Interfaces.

Similar presentations


Presentation on theme: "UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 15 Java Fundamentals Java2 Graphical User Interfaces."— Presentation transcript:

1 UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 15 Java Fundamentals Java2 Graphical User Interfaces (GUIs) Fri. 10/13/00

2 Homework Status 1 Fri, 9/8 Fri, 9/15 Part 1 Mon, 9/18 Part 2 Mon, 9/18 Part 2 2Fri, 9/15 Fri, 9/22 Part 1 & Part 2 3Fri, 9/22 Fri, 9/29Part 1 & Part 2 4Fri, 10/6Fri, 10/13Part 1 & Part 2 5Fri, 10/13Fri, 10/20Part 1 & Part 2 HW# Assigned Due Content

3 Overview ä Events ä Widgets (GUI components) ä Containers ä Labels ä Buttons ä Text fields ä Select lists

4 GUIs Are Important ä GUIs need to provide a skillful blend of: ä User productivity - tasks are accomplished quickly ä User safety - mistakes are minimized ä Intuitiveness - training/learning effort is minimized

5 GUIs Are Hard to Build ä Event loop processing is a new, somewhat complicated paradigm ä Testing is a challenge (hard to exercise all user options) ä Layout requires task-based human factors considerations

6 Events ä The user interacts with your program by initiating “events” ä Mouse move ä Mouse button (up/down) ä Keyboard key (up/down)

7 Event-driven Programming ä Set up the user environment ä Create and initialize widgets ä Register event handlers with specific widgets ä Enter the “event loop” ä Wait for an event (listen) ä Handle the event (respond)

8 Handling Events ä Get the user input (if appropriate) ä Perform appropriate processing based on user action ä Refresh the user interface

9 Event-driven Programming in Java ä The event loop is hidden from you (you show() the top-level window) ä Java puts event information into an instance of a class derived from AWTEvent (depending on event type) ä Event instances are “sent” to widgets automatically ä Widgets pass off events to the appropriate handler(s)

10 Java Uses Event Delegation ä Widgets are represented by instances of various Java library classes ä Widgets maintain a list of event handlers (an EventListenerList) ä Event handlers are EventListeners (i.e., implement an EventListener interface) and are keyed to specific event types (they “listen” for certain types of events)

11 Event Delegation (concluded) ä EventListeners are programmer-selected classes that are distinct from the widget ä EventListeners are interfaces ä Widgets attach implementations of these listener interfaces ä Such implementations are often done via an anonymous inner class ä These implementations “handle” the event

12 Example of an Event Listener public class MyFrame extends JFrame {... // inside of a constructor, for example jTextField1 = new JTextField(); jTextField1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { public void actionPerformed(ActionEvent evt) { jTextField1ActionPerformed(evt); jTextField1ActionPerformed(evt); } } ); // end of addActionListener ); // end of addActionListenergetContentPane().add(jTextField1);...}

13 Example of an Event Listener public class MyFrame extends JFrame {... private void jTextField1ActionPerformed (ActionEvent evt) { String inputString = jTextField1.getText(); String inputString = jTextField1.getText(); myColor = myStringToColor(inputString); myColor = myStringToColor(inputString); updateWidgets(); updateWidgets(); Container c = getContentPane(); Container c = getContentPane(); c.repaint(); c.repaint();}...}

14 Java Events ä Java aggregates low-level user events to make life easier for the developer ä Depending on the “focus” (“active” widget), e.g.: ä Button selected ä Text entered ä List item selected ä Menu item selected (we won’t discuss Java menus in this course) ä Item dragged (we won’t discuss Java drag and drop in this course)

15 Types of Java Events ä ActionEvent ä AdjustmentEvent ä ItemEvent ä ComponentEvent ä ContainerEvent ä FocusEvent ä PaintEvent ä WindowEvent ä Input Event ä KeyEvent ä MouseEvent

16 Types of Java Event Listeners ä ActionListener ä AdjustmentListener ä ItemListener ä ComponentListener ä ContainerListener ä FocusListener ä WindowListener ä KeyListener ä MouseListener ä MouseMotionListener ä TextListener

17 Event Adapters ä Convenience classes that implement event listener interfaces ä Provide “stubs” for methods you don’t want to implement yourself ä You just override the implementation of event handlers of interest ä For example, the WindowListener interface declares windowActivated, windowClosed, windowClosing, windowDeactivated, windowIconified, windowDeiconified, windowOpened methods that the WindowAdapter implements

18 Example of an Event Adapter public class MyFrame extends JFrame {... // inside of a constructor, for example addWindowListener (new WindowAdapter () { public void windowClosing (WindowEvent evt) { public void windowClosing (WindowEvent evt) { System.exit (0); System.exit (0); } } ); // end of addWindowListener call ); // end of addWindowListener call...}

19 Processing GUI Events in Java ä Implement an event handler ä Define a class that implements an event-listener interface (i.e., defines the appropriate event handling method(s)) ä Create an instance of the class ä Register the event handler with the widget, e.g.: ä addActionListener() ä addItemListener()

20 Another Example // define a public class MyFrame that extends JFrame private JButton myButton1; private boolean state1; // instance variables // in the constructor myButton1 = new JButton(“Press Me”); ButtonHandler handler = new ButtonHandler(); myButton1.addActionListener (handler); addWindowListener (new WindowAdapter () { public void windowClosing (WindowEvent evt) {System.exit(0); } public void windowClosing (WindowEvent evt) {System.exit(0); } } ); ); // elsewhere in the class, define method main for class MyFrame public static void main(String args[]) { MyFrame myFrame1 = new MyFrame(); MyFrame myFrame1 = new MyFrame(); myFrame1.show(); myFrame1.show();}

21 Another Example (2) // elsewhere in the class, define an inner class private class ButtonHandler implements ActionListener { public void actionPerformed(ActionEvent evt) public void actionPerformed(ActionEvent evt) { if (state1) { if (state1) { myButton1.setText("State2”); myButton1.setText("State2”); state1 = false; state1 = false; } else { else { myButton1.setText("State1”); myButton1.setText("State1”); state1 = true; state1 = true; } }

22 java.awt.Component ä The parent class for almost all user interface widgets ä Includes methods for: ä Adding various event listeners ä Getting/setting location & size ä Handling text (fonts,...) ä Painting a Graphics object ä Processing focus, visibility, colors, cursors,...

23 javax.swing.JComponent ä Parent class for most Swing components ä Supports different look-and-feels ä Mechanisms for tool tips, mnemonics, internationalization, borders, scrolling,...

24 java.awt.Container ä A collection of (holder for) other widgets ä You usually work with one of its subclasses (like JFrame) ä You add() other widgets to a container ä Can have a layout policy ä Set before add widgets ä Only one layout policy at a time ä Has benefits and disadvantages

25 Layout Policies ä Set with setLayout() method ä FlowLayout (left to right, top to bottom) ä BorderLayout (5 regions) ä GridLayout (arbitrary sized matrix) ä Others (Box, Card, GridBag,...)

26 java.awt.Frame ä Top-level window (implemented via native window) ä Has a title ä Can have a menubar ä Controls cursors

27 javax.swing.JFrame ä Extends Frame ä Platform-neutral window ä Children must be added to the content pane (Container) ä Has default close operation

28 javax.swing.JPanel ä Panels are sub-containers (containers within containers) ä Used to organize user interface into major sections ä Fairly lightweight

29 javax.swing.JLabel ä Extends JComponent ä Non-editable text and/or Image ä Generally does not change even under program control ä Often used with a text entry field ä Very important human factors element

30 javax.swing.JButton ä Extends AbstractButton ä Implements a simple user selection paradigm ä Typically has a title ä Can also have an Icon ä Several types ä Simple push button (command button) ä Select one of many radio button ä Two-state check box

31 javax.swing.JTextField ä Extends JTextComponent ä Basic one-line text entry ä Can be uneditable, typically editable ä Can set size by number of characters ä Includes text processing mechanisms (font, alignment,...)

32 javax.swing.JList ä Extends JComponent ä Multiple or sing-select, potentially scrolla ble, list ä Need to embed in a JScrollPane to get scrollbars ä Includes mechanisms to set, clear selected item(s), set number of visible items, modify colors, convert point to list index, modify list data,... ä Different selection modes (single, single contiguous, multiple discrete)


Download ppt "UMass Lowell Computer Science 91.460 Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 15 Java Fundamentals Java2 Graphical User Interfaces."

Similar presentations


Ads by Google