ICS 22
Klefstad
Week 4
Java Graphical User Interfaces

Graphical User Interfaces

Class Component

Class Container

Class Panel

Class Applet

Class Window

Class Frame

Events

Event Delivery

ActionEvents

Important Listener interfaces and Their Event Methods

Using inner classes for events

Label

Buttons

Check Boxes

Checkbox Groups

List

Scrollbar

TextComponent

TextField

TextArea

GUI Object Layout

Class FlowLayout

Class GridLayout

Classes GridBagLayout & GridBagConstraints

Class BorderLayout

Class CardLayout

Example
import java.awt.*; import java.awt.event.*; // The main class, builds user interface to a List public class ListTest extends Frame { // Allows Exit via clicking on Windows X-box // in upper right corner class WindowDestroyer extends WindowAdapter { public void windowClosing(WindowEvent e) { System.exit(0); } } ListTest() { addWindowListener(new WindowDestroyer()); setSize(800,700); setLayout(new GridLayout(2,1)); add(new ListManager(new LinkedList())); setVisible(true); } public static void main(String[] argv) { ListTest t = new ListTest(); } }
Example (Continued)
class ListManager extends Panel { private List myList; TextArea output; class Inserter extends Panel implements ActionListener { TextField t; Button b; Inserter() { add(b = new Button("Insert")); add(t = new TextField("Hello")); // so my actionPerformed will be called b.addActionListener(this); } public void actionPerformed(ActionEvent e) { String s = t.getText().trim(); if ( s.length() > 0 && !myList.isFull() ) myList.insert(s); redrawList(); } } class Remover extends Panel implements ActionListener { TextField t; Button b; Remover() { add(b = new Button("Remove")); add(t = new TextField(20)); // so my actionPerformed will be called b.addActionListener(this); } public void actionPerformed(ActionEvent e) { if ( !myList.isEmpty() ) { t.setText((String)myList.pop()); redrawList(); } } } void redrawList() { output.setText(myList.toString()); } ListManager(List s) { setLayout(new GridLayout(3,1)); myList = s; add(new Inserter()); add(new Remover()); output = new TextArea(); add(output); } }
Example (Continued)
abstract class List { abstract void insert(Object o); abstract Object remove(); abstract boolean isEmpty(); abstract boolean isFull(); } class LinkedList extends List { private static class ListNode { Object info; Node next; ListNode(Object newInfo, Node newNext) { info = newInfo; next = newNext; } } private ListNode head; LinkedList() { head = null; } void insert(Object o) { head = new Node(o, head); } Object remove() { Object temp = head.info; head = head.next; return temp; } boolean isEmpty() { return head == null; } boolean isFull() { return false; } public String toString() { String result = ""; for (Node p = head; p != null; p = p.next) result += " " + p.info; return result; } }