cop 3503 fall 2012 shayan javed lecture 14

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

Upload: eliza

Post on 22-Feb-2016

52 views

Category:

Documents


0 download

DESCRIPTION

COP 3503 FALL 2012 Shayan Javed Lecture 14. Programming Fundamentals using Java. Graphical-User Interfaces (GUIs). So far. Only created text-based programs No fancy graphics (buttons! menus! text-fields!). Graphical-User Interfaces (GUI). Going to look at how to create GUIs in Java. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: COP 3503 FALL 2012 Shayan Javed Lecture 14

1 / 671

COP 3503 FALL 2012SHAYAN JAVED

LECTURE 14

Programming Fundamentals using Java

Page 2: COP 3503 FALL 2012 Shayan Javed Lecture 14

2 / 67

Graphical-User Interfaces

(GUIs)

Page 3: COP 3503 FALL 2012 Shayan Javed Lecture 14

3 / 67

So far...

Only created text-based programs

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

Page 4: COP 3503 FALL 2012 Shayan Javed Lecture 14

4 / 67

Graphical-User Interfaces (GUI)

Going to look at how to create GUIs in Java

Page 5: COP 3503 FALL 2012 Shayan Javed Lecture 14

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: COP 3503 FALL 2012 Shayan Javed Lecture 14

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: COP 3503 FALL 2012 Shayan Javed Lecture 14

7 / 67

The SWING API

Used to create desktop applications.

Page 8: COP 3503 FALL 2012 Shayan Javed Lecture 14

8 / 67

The SWING API

Used to create desktop applications.

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

Page 9: COP 3503 FALL 2012 Shayan Javed Lecture 14

9 / 67

Model-View-Controller design

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

Page 10: COP 3503 FALL 2012 Shayan Javed Lecture 14

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: COP 3503 FALL 2012 Shayan Javed Lecture 14

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: COP 3503 FALL 2012 Shayan Javed Lecture 14

12 / 67

The SWING API

Example:

Scrabble

Page 13: COP 3503 FALL 2012 Shayan Javed Lecture 14

13 / 67

The GUI API

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

Page 14: COP 3503 FALL 2012 Shayan Javed Lecture 14

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: COP 3503 FALL 2012 Shayan Javed Lecture 14

15 / 67

The GUI API

3 Groups of classes:

Component Classes: Buttons, Labels, TextFields, etc.

Page 16: COP 3503 FALL 2012 Shayan Javed Lecture 14

16 / 67

The GUI API

3 Groups of classes:

Component Classes: Buttons, Labels, TextFields, etc.

Container Classes: Frames, Panels, Applets, etc.

Page 17: COP 3503 FALL 2012 Shayan Javed Lecture 14

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: COP 3503 FALL 2012 Shayan Javed Lecture 14

18 / 67

The SWING API

Simple Window

Represented by the JFrame class

Page 19: COP 3503 FALL 2012 Shayan Javed Lecture 14

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: COP 3503 FALL 2012 Shayan Javed Lecture 14

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: COP 3503 FALL 2012 Shayan Javed Lecture 14

21 / 67

The SWING API – add components

A JFrame containts a content pane.

Page 22: COP 3503 FALL 2012 Shayan Javed Lecture 14

22 / 67

The SWING API – add components

A JFrame containts a content pane.

Content pane = instance of java.awt.Container;

Page 23: COP 3503 FALL 2012 Shayan Javed Lecture 14

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: COP 3503 FALL 2012 Shayan Javed Lecture 14

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: COP 3503 FALL 2012 Shayan Javed Lecture 14

25 / 67

Layout Managers

Components in content pane are laid out by layout managers.

Page 26: COP 3503 FALL 2012 Shayan Javed Lecture 14

26 / 67

Layout Managers

Components in content pane are laid out by layout managers.

Multiple types: FlowLayout GridLayout BorderLayout

Page 27: COP 3503 FALL 2012 Shayan Javed Lecture 14

27 / 67

Layout Managers - FlowLayout

Components arranged left to right in order.

One row fills up, a new row is started

Page 28: COP 3503 FALL 2012 Shayan Javed Lecture 14

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: COP 3503 FALL 2012 Shayan Javed Lecture 14

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: COP 3503 FALL 2012 Shayan Javed Lecture 14

30 / 67

Layout Managers - FlowLayout

public class ShowFlowLayout extends JFrame {public ShowFlowLayout() {

// set the flow layoutsetLayout(new FlowLayout(FlowLayout.LEFT,

10, 20);

add(new JButton(“Button”));add(new JTextField(8));

}}

Page 31: COP 3503 FALL 2012 Shayan Javed Lecture 14

31 / 67

Layout Managers - GridLayout

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

Page 32: COP 3503 FALL 2012 Shayan Javed Lecture 14

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: COP 3503 FALL 2012 Shayan Javed Lecture 14

33 / 67

Layout Managers - GridLayout

public class ShowGridLayout extends JFrame {public ShowGridLayout() {

// set the Grid layout – 3 rows, 2 columnssetLayout(new GridLayout(3, 2, 10, 10);

add(new JButton(“Button”));add(new JTextField(8));

}}

Page 34: COP 3503 FALL 2012 Shayan Javed Lecture 14

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: COP 3503 FALL 2012 Shayan Javed Lecture 14

35 / 67

Layout Managers - BorderLayout

public class ShowBorderLayout extends JFrame {public ShowBorderLayout() {

// set the Border LayoutsetLayout(new BorderLayout(10, 10);

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

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

}

Page 36: COP 3503 FALL 2012 Shayan Javed Lecture 14

36 / 67

The SWING API

Looked at adding Components to the Window (Frame).

And how to lay them out.

Page 37: COP 3503 FALL 2012 Shayan Javed Lecture 14

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: COP 3503 FALL 2012 Shayan Javed Lecture 14

38 / 67

Using JPanels as Subcontainers

Panels are subcontainers.

Page 39: COP 3503 FALL 2012 Shayan Javed Lecture 14

39 / 67

Using JPanels as Subcontainers

Panels are subcontainers.

Can add components to them

Page 40: COP 3503 FALL 2012 Shayan Javed Lecture 14

40 / 67

Using JPanels as Subcontainers

Panels are subcontainers.

Can add components to them

Also set layouts (default: FlowLayout)

Page 41: COP 3503 FALL 2012 Shayan Javed Lecture 14

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: COP 3503 FALL 2012 Shayan Javed Lecture 14

42 / 67

Using JPanels// set the Border Layout of the JFramesetLayout(new BorderLayout(10, 10);

// add a Panel to the West and EastJPanel 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 panelp2.add(new JTextField(8)); p2.add(new JButton(“Button1”));p2.add(new JTextField(8)); p2.add(new JButton(“Button2”));

// one button to the west panelp1.add(new JButton(“Button3”));

Page 43: COP 3503 FALL 2012 Shayan Javed Lecture 14

43 / 67

Adding Components

Need to add components for interaction.

Page 44: COP 3503 FALL 2012 Shayan Javed Lecture 14

44 / 67

Adding Components

Need to add components for interaction.

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

Page 45: COP 3503 FALL 2012 Shayan Javed Lecture 14

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: COP 3503 FALL 2012 Shayan Javed Lecture 14

46 / 67

Interaction and Events

Need to handle events from various GUI components.

Page 47: COP 3503 FALL 2012 Shayan Javed Lecture 14

47 / 67

Interaction and Events

Need to handle events from various GUI components.

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

Page 48: COP 3503 FALL 2012 Shayan Javed Lecture 14

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: COP 3503 FALL 2012 Shayan Javed Lecture 14

49 / 67

Interaction and Events

Components generate different kinds of Events

Page 50: COP 3503 FALL 2012 Shayan Javed Lecture 14

50 / 67

Interaction and Events

Components generate different kinds of Events

ActionEvent, ItemEvent, ChangeEvent, MouseEvent, KeyEvent

Page 51: COP 3503 FALL 2012 Shayan Javed Lecture 14

51 / 67

Interaction and Events

Components generate different kinds of Events

ActionEvent, ItemEvent, ChangeEvent, MouseEvent, KeyEvent

Example: JButton generates ActionEvent

Page 52: COP 3503 FALL 2012 Shayan Javed Lecture 14

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: COP 3503 FALL 2012 Shayan Javed Lecture 14

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: COP 3503 FALL 2012 Shayan Javed Lecture 14

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: COP 3503 FALL 2012 Shayan Javed Lecture 14

55 / 67

Interaction and Events

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

Page 56: COP 3503 FALL 2012 Shayan Javed Lecture 14

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: COP 3503 FALL 2012 Shayan Javed Lecture 14

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: COP 3503 FALL 2012 Shayan Javed Lecture 14

58 / 67

ActionListener interface

class AClass implements ActionListener {public void actionPerformed(ActionEvent e) {

// do whatever you want}

}

Page 59: COP 3503 FALL 2012 Shayan Javed Lecture 14

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: COP 3503 FALL 2012 Shayan Javed Lecture 14

60 / 67

ActionListener interface

ActionEvent methods:

Page 61: COP 3503 FALL 2012 Shayan Javed Lecture 14

61 / 67

ActionListener interface

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

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

Page 62: COP 3503 FALL 2012 Shayan Javed Lecture 14

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: COP 3503 FALL 2012 Shayan Javed Lecture 14

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: COP 3503 FALL 2012 Shayan Javed Lecture 14

64 / 67

ItemListener interface

ItemEvent methods:

Object getItem(): Component-specific object.

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

Page 65: COP 3503 FALL 2012 Shayan Javed Lecture 14

65 / 67

Interaction and Events

Look at some other useful Events:

MouseEvent KeyEvent WindowEvent

Page 66: COP 3503 FALL 2012 Shayan Javed Lecture 14

66 / 67

Interaction and Events

Look at some other useful Events:

MouseEvent KeyEvent WindowEvent

Experiment!

Page 67: COP 3503 FALL 2012 Shayan Javed Lecture 14

67 / 67

Summary

Windows and Panels Layouts Add Components

Add Listeners Handle Events