Presentation is loading. Please wait.

Presentation is loading. Please wait.

Kim B. Bruce, Andrea Danyluk & Tom Murtagh Williams College † Partially supported by NSF CCLI grant DUE-0088895 Java: An Eventful Approach An innovative.

Similar presentations


Presentation on theme: "Kim B. Bruce, Andrea Danyluk & Tom Murtagh Williams College † Partially supported by NSF CCLI grant DUE-0088895 Java: An Eventful Approach An innovative."— Presentation transcript:

1 Kim B. Bruce, Andrea Danyluk & Tom Murtagh Williams College † Partially supported by NSF CCLI grant DUE-0088895 Java: An Eventful Approach An innovative approach to teaching Java in CS1 † Interfaces & GUI Components

2 Interfaces Provide support for abstraction. Provide contract for implementing classes. Allow variables to hold values from different classes. Necessary for standard Java event handling.

3 Interfaces for Abstraction Variable with class type can only hold objects from class or its extension. Classes implementing same interface need not share any code. Like purely abstract class but more flexible as class can implement several interfaces. Laundry Demo.

4 The Need for Input or Interaction Useful for examples –Frogger with mouse clicks –Anticipating array examples: Snake, Simon –No way to get text input up to this point Midterm / Test Program time –Nice break –Less conceptually demanding than other topics

5 GUI Components –Button, Choice, Scrollbar –Label, TextField, TextArea Associated Listeners Layout -- not emphasized –FlowLayout, BorderLayout, GridLayout Panels

6 Layout Managers WindowController uses BorderLayout add(component,BorderLayout.SOUTH);

7 “How to” GUI Checklist Create and initialize the component. Install the component in a container (window or panel). Associate listener with component and ensure its class implements appropriate listener interface. Write listener method.

8 Simple Listeners Only No inner classes Controller or WindowController is listener –Analogy with mouse event handling methods

9 Button Constructor takes label as parameter. –new Button(“button label”) Associated with ActionListener –addActionListener(this); –class … implements ActionListener; Listener method is actionPerformed taking ActionEvent as a parameter. –public void actionPerformed(ActionEvent evt){

10 Button public class SimpleGUI extends WindowController implements ActionListener { // 3 private Button myButton; // 1 public void begin() { myButton = new Button(“Push me!”); // 1 add(myButton, BorderLayout.SOUTH); // 2 myButton.addActionListener(this); // 3 } public void actionPerformed(ActionEvent evt) { new Text(“That tickles!”,150,75,canvas); // 4 }

11 Panels Organize window w/nested Panels. Fixes stretching problem of BorderLayout. Add GUI components to panel, add panel to window. Panel uses FlowLayout by default –Add GUI components left-to-right in order added. Start new row when out of space.

12 Button in Panel public class SimpleGUI extends WindowController implements ActionListener { // 3 private Button myButton; // 1 public void begin() { Panel southPanel = new Panel(); myButton = new Button(“Push me!”); // 1 southPanel.add(myButton); add(southPanel, BorderLayout.SOUTH); // 2 myButton.addActionListener(this); // 3 } … }

13 GUI cheat sheet For each component: –Constructor –GUI methods –Listener and event methods Choice, Scrollbar, Label, TextField, TextArea Also cover key and mouse events

14 Choice Menu Constructor takes label as parameter. –new Choice(); –add(“item name”); Associated with ItemListener –addItemListener(this); –class … implements ItemListener; Listener method is itemStateChanged: –public void itemStateChanged(ItemEvent evt){ Get label with getSelectedItem() –String item = myChoice.getSelectedItem();


Download ppt "Kim B. Bruce, Andrea Danyluk & Tom Murtagh Williams College † Partially supported by NSF CCLI grant DUE-0088895 Java: An Eventful Approach An innovative."

Similar presentations


Ads by Google