+ All Categories
Home > Documents > 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

Date post: 14-Jan-2016
Category:
Upload: shawn-walker
View: 214 times
Download: 0 times
Share this document with a friend
67
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1
Transcript
Page 1: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

1 / 671

COP 3503 FALL 2012SHAYAN JAVED

LECTURE 14

Programming Fundamentals using Java

Page 2: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

2 / 67

Graphical-User Interfaces

(GUIs)

Page 3: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

3 / 67

So far...

Only created text-based programs

No fancy graphics (buttons! menus! text-fields!)

Page 4: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

4 / 67

Graphical-User Interfaces (GUI)

Going to look at how to create GUIs in Java

Page 5: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

5 / 67

Graphical-User Interfaces (GUI)

Going to look at how to create GUIs in Java

Use the SWING API (for desktop-programs)

Page 6: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

6 / 67

Graphical-User Interfaces (GUI)

Going to look at how to create GUIs in Java

Use the SWING API (for desktop-programs)

Use the AWT API (for Java Applets – on the web)

Page 7: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

7 / 67

The SWING API

Used to create desktop applications.

Page 8: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

8 / 67

The SWING API

Used to create desktop applications.

Uses the Model-View-Controller software engineering design pattern.

Page 9: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

9 / 67

Model-View-Controller design

Model: Manages the behavior and data of the application. Changes state.

Page 10: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

10 / 67

Model-View-Controller design

Model: Manages the behavior and data of the application. Changes state.

View: Renders the model into a form for interaction.

(Button, textbox, checkbox, etc.)

Page 11: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

11 / 67

Model-View-Controller design

Model: Manages the behavior and data of the application. Changes state.

View: Renders the model into a form for interaction.

(Button, textbox, checkbox, etc.) Controller:

Receives user input and initiates a response by interacting with the model.

Page 12: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

12 / 67

The SWING API

Example:

Scrabble

Page 13: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

13 / 67

The GUI API

Use the NetBeans IDE for easy drag-and-drop creation.

Page 14: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

14 / 67

The GUI API

Use the NetBeans IDE for easy drag-and-drop creation.

But we are going to focus on basic code

Page 15: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

15 / 67

The GUI API

3 Groups of classes:

Component Classes: Buttons, Labels, TextFields, etc.

Page 16: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

16 / 67

The GUI API

3 Groups of classes:

Component Classes: Buttons, Labels, TextFields, etc.

Container Classes: Frames, Panels, Applets, etc.

Page 17: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

17 / 67

The GUI API

3 Groups of classes:

Component Classes: Buttons, Labels, TextFields, etc.

Container Classes: Frames, Panels, Applets, etc.

Helper Classes: Graphics, Color, Font, etc.

Page 18: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

18 / 67

The SWING API

Simple Window

Represented by the JFrame class

Page 19: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

19 / 67

The SWING API

import javax.swing.JFrame;

public static void main(String[] args) {

JFrame frame = new JFrame(“A Title”);

frame.setSize(400, 300);

frame.setLocation(10, 300);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

}

Page 20: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

20 / 67

The SWING API – add components

import javax.swing.*;

public static void main(String[] args) {

JFrame frame = new JFrame(“A Title”);

JButton button = new JButton(“OK”);

frame.add(button);

frame.setSize(400, 300);

...

}

Page 21: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

21 / 67

The SWING API – add components

A JFrame containts a content pane.

Page 22: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

22 / 67

The SWING API – add components

A JFrame containts a content pane.

Content pane = instance of java.awt.Container;

Page 23: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

23 / 67

The SWING API – add components

A JFrame containts a content pane.

Content pane = instance of java.awt.Container;

Objects are added to it frame.add( Component )

Page 24: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

24 / 67

The SWING API – add components

A JFrame containts a content pane.

Content pane = instance of java.awt.Container;

Objects are added to it frame.add( Component ) frame.remove( Component )

Page 25: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

25 / 67

Layout Managers

Components in content pane are laid out by layout managers.

Page 26: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

26 / 67

Layout Managers

Components in content pane are laid out by layout managers.

Multiple types: FlowLayout GridLayout BorderLayout

Page 27: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

27 / 67

Layout Managers - FlowLayout

Components arranged left to right in order.

One row fills up, a new row is started

Page 28: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

28 / 67

Layout Managers - FlowLayout

java.awt.FlowLayout

Properties: alignment: int (CENTER/LEFT/RIGHT/etc.) hgap, vgap: int (the gaps – default: 5 pixels)

Page 29: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

29 / 67

Layout Managers - FlowLayout

java.awt.FlowLayout

Properties: alignment: int (CENTER/LEFT/RIGHT/etc.) hgap, vgap: int (the gaps – default: 5 pixels)

Constructors: FlowLayout() FlowLayout(alignment, hgap, vgap)

Page 30: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

30 / 67

Layout Managers - FlowLayout

public class ShowFlowLayout extends JFrame {

public ShowFlowLayout() {

// set the flow layout

setLayout(new FlowLayout(FlowLayout.LEFT, 10, 20);

add(new JButton(“Button”));

add(new JTextField(8));

}

}

Page 31: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

31 / 67

Layout Managers - GridLayout

Arrange components in a grid (matrix) formation. Placed left-to-right.

Page 32: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

32 / 67

Layout Managers - GridLayout

Arrange components in a grid (matrix) formation. Placed left-to-right.

Properties: rows, columns: int hgap, vgap: int (the gaps – default: 5 pixels)

Page 33: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

33 / 67

Layout Managers - GridLayout

public class ShowGridLayout extends JFrame {

public ShowGridLayout() {

// set the Grid layout – 3 rows, 2 columns

setLayout(new GridLayout(3, 2, 10, 10);

add(new JButton(“Button”));

add(new JTextField(8));

}

}

Page 34: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

34 / 67

Layout Managers - BorderLayout

Default Layout for ContentPanes (Jframe)

Divides container into 5 areas: East, West, South, North, Center Components are added into one of these areas

Properties: hgap, vgap: int (the gaps – default: 5 pixels)

Page 35: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

35 / 67

Layout Managers - BorderLayout

public class ShowBorderLayout extends JFrame {

public ShowBorderLayout() {

// set the Border Layout

setLayout(new BorderLayout(10, 10);

add(new JButton(“Button”), BorderLayout.EAST);

add(new JTextField(8), BorderLayout.WEST);

}

}

Page 36: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

36 / 67

The SWING API

Looked at adding Components to the Window (Frame).

And how to lay them out.

Page 37: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

37 / 67

The SWING API

Looked at adding Components to the Window (Frame).

And how to lay them out.

But often need “sub-windows” to create more complex interfaces.

Page 38: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

38 / 67

Using JPanels as Subcontainers

Panels are subcontainers.

Page 39: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

39 / 67

Using JPanels as Subcontainers

Panels are subcontainers.

Can add components to them

Page 40: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

40 / 67

Using JPanels as Subcontainers

Panels are subcontainers.

Can add components to them

Also set layouts (default: FlowLayout)

Page 41: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

41 / 67

Using JPanels as Subcontainers

Panels are subcontainers.

Can add components to them

Also set layouts (default: FlowLayout)

JPanel panel = new JPanel();

panel.add(new JButton(“OK”));

Page 42: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

42 / 67

Using JPanels

// set the Border Layout of the JFrame

setLayout(new BorderLayout(10, 10);

// add a Panel to the West and East

JPanel p1 = new JPanel();

add(p1, BorderLayout.WEST);

JPanel p2 = new JPanel(); p2.setLayout(new GridLayout(2, 2, 5, 5));

add(p2, BorderLayout.EAST);

// add components to the east panel

p2.add(new JTextField(8)); p2.add(new JButton(“Button1”));

p2.add(new JTextField(8)); p2.add(new JButton(“Button2”));

// one button to the west panel

p1.add(new JButton(“Button3”));

Page 43: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

43 / 67

Adding Components

Need to add components for interaction.

Page 44: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

44 / 67

Adding Components

Need to add components for interaction.

Some useful ones: JButton JTextField JCheckBox JComboBox JMenuBar etc...

Page 45: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

45 / 67

Model-View-Controller design

Model: (ALREADY IMPLEMENTED) Manages the behavior and data of the application. Changes state.

View: DONE Renders the model into a form for interaction.

(Button, textbox, checkbox, etc.) Controller:

Receives user input and initiates a response by interacting with the model.

Page 46: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

46 / 67

Interaction and Events

Need to handle events from various GUI components.

Page 47: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

47 / 67

Interaction and Events

Need to handle events from various GUI components.

Button clicks, text field changes, menu selection, etc.

Page 48: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

48 / 67

Interaction and Events

Need to handle events from various GUI components.

Button clicks, text field changes, menu selection, etc.

Event-driven programming

Page 49: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

49 / 67

Interaction and Events

Components generate different kinds of Events

Page 50: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

50 / 67

Interaction and Events

Components generate different kinds of Events

ActionEvent, ItemEvent, ChangeEvent, MouseEvent, KeyEvent

Page 51: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

51 / 67

Interaction and Events

Components generate different kinds of Events

ActionEvent, ItemEvent, ChangeEvent, MouseEvent, KeyEvent

Example: JButton generates ActionEvent

Page 52: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

52 / 67

Interaction and Events

Components generate different kinds of Events

ActionEvent, ItemEvent, ChangeEvent, MouseEvent, KeyEvent

Example: JButton generates ActionEvent Mouse pressed/moved/dragged: MouseEvent

Page 53: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

53 / 67

Interaction and Events

Components generate different kinds of Events

ActionEvent, ItemEvent, ChangeEvent, MouseEvent, KeyEvent

Example: JButton generates ActionEvent Mouse pressed/moved/dragged: MouseEvent JCheckBox: ItemEvent, ActionEvent

Page 54: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

54 / 67

Interaction and Events

Need to “listen” for Events.

Source object fires an event, and an object interested in the event handles it. Latter object called a “listener”

Page 55: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

55 / 67

Interaction and Events

For an object to be a listener, it needs to implement an interface.

Page 56: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

56 / 67

Interaction and Events

For an object to be a listener, it needs to implement an interface.

Interface should correspond to the Event. ActionListener for ActionEvent MouseListener for MouseEvent KeyListener for KeyEvent

Page 57: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

57 / 67

Interaction and Events

For an object to be a listener, it needs to implement an interface.

Interface should correspond to the Event. ActionListener for ActionEvent MouseListener for MouseEvent KeyListener for KeyEvent

object.addXListener(...)

Page 58: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

58 / 67

ActionListener interface

class AClass implements ActionListener {

public void actionPerformed(ActionEvent e) {

// do whatever you want

}

}

Page 59: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

59 / 67

ActionListener interface

class AClass implements ActionListener {

public void actionPerformed(ActionEvent e) {

// do whatever you want

}

}

JButton button = new JButton(“OK”);

button.addActionListener(new AClass());

Page 60: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

60 / 67

ActionListener interface

ActionEvent methods:

Page 61: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

61 / 67

ActionListener interface

ActionEvent methods: Object getSource(): returns the object on which

the Event initially occurred. (in java.util.EventObject)

Page 62: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

62 / 67

ActionListener interface

ActionEvent methods: Object getSource(): returns the object on which

the Event initially occurred. (in java.util.EventObject)

String getActionCommand(): returns the command string

(text of the button for ex.)

Page 63: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

63 / 67

ItemListener interface

Used for check boxes, toggle buttons, etc.

class AClass implements ItemListener {

public void itemStatechanged(ItemEvent e) {

// do whatever you want

}

}

Page 64: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

64 / 67

ItemListener interface

ItemEvent methods:

Object getItem(): Component-specific object.

int getStateChange(): The new state of the associated object.

Page 65: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

65 / 67

Interaction and Events

Look at some other useful Events:

MouseEvent KeyEvent WindowEvent

Page 66: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

66 / 67

Interaction and Events

Look at some other useful Events:

MouseEvent KeyEvent WindowEvent

Experiment!

Page 67: 1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.

67 / 67

Summary

Windows and Panels Layouts Add Components

Add Listeners Handle Events


Recommended