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 16 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 16 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 16 Java Fundamentals Java2 Graphical User Interfaces (GUIs) Mon. 10/16/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 Event Delegation ä 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

4 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);...}

5 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();}...}

6 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)

7 Some Types of Java Events ä ActionEvent (e.g. for a JButton) ä ListSelectionEvent (e.g. for a JList) ä AdjustmentEvent ä ItemEvent ä ComponentEvent ä ContainerEvent ä FocusEvent ä PaintEvent ä WindowEvent ä Input Event ä KeyEvent ä MouseEvent

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

9 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

10 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...}

11 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()

12 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();}

13 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; } }

14 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,...

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

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

17 Some Swing GUI Components ä JLabel ä JTextField ä JButton ä JCheckBox ä JComboBox ä JComponent ä JFrame ä JPanel ä JList ä JScrollPane

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

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

20 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

21 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

22 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,...)

23 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)

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,...)


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

Similar presentations


Ads by Google