Docsity

Java GUI Development: OOP II (CMSC 132) at UMD, College Park - Prof. Nelson Padua-Perez, Study notes of Computer Science

Toggle sidebar
bg1
1
CMSC 132:
Object-Oriented Programming II
GUI I
Department of Computer Science
University of Maryland, College Park
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Java GUI Development: OOP II (CMSC 132) at UMD, College Park - Prof. Nelson Padua-Perez and more Study notes Computer Science in PDF only on Docsity!

CMSC 132:

Object-Oriented Programming II

GUI I

Department of Computer Science

University of Maryland, College Park

Overview

Java GUI Classes JFrame Hierarchy JFrame Structure Event Dispatching Thread GUI Elements – Layout Java Layout Managers

GUI (Graphical User Interface)

You create GUIs by defining objects such as

Text fields Labels Buttons Checkboxes Radioboxes Menus Tables Etc.

Java GUI Classes

AWT (Abstract Window Toolkit) (java.awt.*)

Old GUI framework for Java (Java 1.1) Some reliance on native code counterparts Platform independence problems

Swing (javax.swing.*)

New GUI framework first introduced in Java 1.2 Includes AWT features plus many enhancements Pure Java components (no reliance on native code) Pluggable look and feel architecture

SWT (Standard Widget Toolkit; from Eclipse)

How to Create a GUI

Define a Frame or Applet to hold thecomponents. From now on we will use onlyframes. Add components to the frame Add actions to your GUI by adding listeners toGUI components

JFrame Hierarchy

Several super classes and well asimplemented interfaces Many, member methods includinginherited methods that allow foroperations such as resizing, settingproperties, adding components, etc.

Object Component^ Container

Window^ Frame^ JFrame

Examples

Examples in gui.jar

For now our examples have no action associatedwith them. Let’s see example in ex1 package ex2 is basically ex1 but reduced to one class

Event Dispatching Thread (From Wikipedia)

Background thread to process events from the AWTgraphical interface event queue These events are mainly update events that

Cause components to redraw themselves Represent input events

Swing uses a single-threaded painting model whereonly the Event Dispatching thread is the only validthread that can update user interface components Updating components from other threads is a source ofcommon bugs

GUI Elements – Layout

Definition

Arrangement of widgets in container

Layout specification

Logical terms (

nd

row, 1

st

column, left)

Preferred approach Actual coordinates (100 pixels, 5 inches)

Can be too rigid, limited to certain window sizes

Layout manager

Entity translating layout specifications into actualcoordinates at runtime, depending on conditions

Java Layout Managers

Flowlayout

Lays out components from left to right

GridLayout

Lays out components in a grid of user specified size

BorderLayout

Designates portions of the container as North,South, East, West, and Center

CardLayout

Adds components one on top of another

GridBagLayout

Customizable manager that can use rows andcolumns of varying lengths