InterfaceGUI
Download
Report
Transcript InterfaceGUI
Java: An Eventful Approach
An innovative approach to teaching Java
in CS1†
Interfaces & GUI Components
Kim B. Bruce, Andrea Danyluk
& Tom Murtagh
Williams College
† Partially supported by NSF CCLI grant DUE-0088895
Interfaces
• Provide support for abstraction.
• Provide contract for implementing classes.
• Allow variables to hold values from
different classes.
• Necessary for standard Java event handling.
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.
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
GUI
• Components
– Button, Choice, Scrollbar
– Label, TextField, TextArea
• Associated Listeners
• Layout -- not emphasized
– FlowLayout, BorderLayout, GridLayout
• Panels
Layout Managers
• WindowController uses BorderLayout
add(component,BorderLayout.SOUTH);
“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.
Simple Listeners Only
• No inner classes
• Controller or WindowController is listener
– Analogy with mouse event handling methods
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){
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
}
}
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.
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
}
…
}
GUI cheat sheet
• For each component:
– Constructor
– GUI methods
– Listener and event methods
• Choice, Scrollbar, Label, TextField, TextArea
• Also cover key and mouse events
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();