32
IT2033: Object-Oriented Programming (OOP)

IT2033: Object-Oriented Programming (OOP) · Java GUIHistory • Abstract Windowing Toolkit (AWT): Sun's initial effort to create a set of cross-platformGUIclasses. (JDK 1.0 -1.1)

  • Upload
    others

  • View
    9

  • Download
    0

Embed Size (px)

Citation preview

Page 1: IT2033: Object-Oriented Programming (OOP) · Java GUIHistory • Abstract Windowing Toolkit (AWT): Sun's initial effort to create a set of cross-platformGUIclasses. (JDK 1.0 -1.1)

IT2033: Object-Oriented Programming

(OOP)

Page 2: IT2033: Object-Oriented Programming (OOP) · Java GUIHistory • Abstract Windowing Toolkit (AWT): Sun's initial effort to create a set of cross-platformGUIclasses. (JDK 1.0 -1.1)

GRAPHICAL USER INTERFACES

(GUIS) WITH SWING/AWT

Chapter 6

Page 3: IT2033: Object-Oriented Programming (OOP) · Java GUIHistory • Abstract Windowing Toolkit (AWT): Sun's initial effort to create a set of cross-platformGUIclasses. (JDK 1.0 -1.1)

Java GUIHistory

• Abstract Windowing Toolkit (AWT): Sun's initial effort to create a set of cross-platform GUIclasses. (JDK 1.0 -1.1)– Maps general Java code to each operating system's real GUIsystem.

– Problems: Limited to lowest common denominator; clunky touse.

• Swing: Anewer GUI library written from the ground up thatallows much more powerful graphics and GUI construction.(JDK1.2+)– Paints GUIcontrols itself pixel-by-pixel rather than handing off to OS.

– Benefits: Features; compatibility; OOdesign.

– Problem: Both exist in Java now; easy to getthem mixed up; still have to use both in various places.

3

Page 4: IT2033: Object-Oriented Programming (OOP) · Java GUIHistory • Abstract Windowing Toolkit (AWT): Sun's initial effort to create a set of cross-platformGUIclasses. (JDK 1.0 -1.1)

GUITerminology

• Window: A first-class citizen of the graphical desktop.– Also called a top-level container.

– examples: frame, dialog box,applet

• Component: A GUI widget that resides in a window.– Also called controls in many other languages.

– examples: button, text box,label

• Container: A logical grouping for storing components.– examples: panel, box

JLabelJTextField

JButton4

Page 5: IT2033: Object-Oriented Programming (OOP) · Java GUIHistory • Abstract Windowing Toolkit (AWT): Sun's initial effort to create a set of cross-platformGUIclasses. (JDK 1.0 -1.1)

Components

5

Page 6: IT2033: Object-Oriented Programming (OOP) · Java GUIHistory • Abstract Windowing Toolkit (AWT): Sun's initial effort to create a set of cross-platformGUIclasses. (JDK 1.0 -1.1)

Components(2)

6

Page 7: IT2033: Object-Oriented Programming (OOP) · Java GUIHistory • Abstract Windowing Toolkit (AWT): Sun's initial effort to create a set of cross-platformGUIclasses. (JDK 1.0 -1.1)

Swing Inheritance Hierarchy

• Component (AWT)– Window

7

• Frame• JFrame (Swing)• JDialog

– Container• JComponent

• JButton• JComboBox

• JMenuBar

(Swing)JColorChooser JLabel JOptionPane

JFileChooser JList JPanel

• JPopupMenu JProgressBar JScrollbar• JScrollPane JSlider JSpinner• JSplitPane JTabbedPane JTable• JToolbar

• JTextFieldJTree JTextArea...

import java.awt.*;

import javax.swing.*;

Page 8: IT2033: Object-Oriented Programming (OOP) · Java GUIHistory • Abstract Windowing Toolkit (AWT): Sun's initial effort to create a set of cross-platformGUIclasses. (JDK 1.0 -1.1)

Componentproperties

• Eachhas aget (or is) accessor and asetmodifier method.

– examples: getColor, setFont, setEnabled,isVisible

name type description

background Color background color behindcomponent

border Border border line aroundcomponent

enabled boolean whether it can be interactedwith

focusable boolean whether key text can be typed onit

font Font font used for text incomponent

foreground Color foreground color of component

height, width int component's current size in pixels

visible boolean whether component can beseen

tooltip text String text shown when hoveringmouse

size, minimum / maximum/ preferredsize

Dimension various sizes, size limits, or desired sizes that the component maytake

8

Page 9: IT2033: Object-Oriented Programming (OOP) · Java GUIHistory • Abstract Windowing Toolkit (AWT): Sun's initial effort to create a set of cross-platformGUIclasses. (JDK 1.0 -1.1)

JFrame

• A graphical window to hold othercomponents.

• Call setVisible(true) to make a frame appear on thescreen

after creating it.

• Customize Icon of the frame

Note: image file should be in the main folder.

9

Page 10: IT2033: Object-Oriented Programming (OOP) · Java GUIHistory • Abstract Windowing Toolkit (AWT): Sun's initial effort to create a set of cross-platformGUIclasses. (JDK 1.0 -1.1)

JButton

• A clickable region for causing actions to occur.

• public JButton(String text)

Creates a new button with the given string as itstext.

• public String getText()

Returns the text showing on thebutton.

• public void setText(String text)

Sets button's text to be thegiven string.

10

Page 11: IT2033: Object-Oriented Programming (OOP) · Java GUIHistory • Abstract Windowing Toolkit (AWT): Sun's initial effort to create a set of cross-platformGUIclasses. (JDK 1.0 -1.1)

Containers and Layout

• Place components in a container; add the container to a frame.

– container: An object that stores components and governstheir

positions, sizes, and resizing behavior.

11

Page 12: IT2033: Object-Oriented Programming (OOP) · Java GUIHistory • Abstract Windowing Toolkit (AWT): Sun's initial effort to create a set of cross-platformGUIclasses. (JDK 1.0 -1.1)

JOptionPane: Message Dialog

• JOptionPane provides support for laying out standarddialogs,

– providing icons,

– specifying the dialog title andtext,

– customizing the button text.

12

Page 13: IT2033: Object-Oriented Programming (OOP) · Java GUIHistory • Abstract Windowing Toolkit (AWT): Sun's initial effort to create a set of cross-platformGUIclasses. (JDK 1.0 -1.1)

JOptionPane: Message Dialog (2)

13

Page 14: IT2033: Object-Oriented Programming (OOP) · Java GUIHistory • Abstract Windowing Toolkit (AWT): Sun's initial effort to create a set of cross-platformGUIclasses. (JDK 1.0 -1.1)

JOptionPane: Option Dialog

14

Page 15: IT2033: Object-Oriented Programming (OOP) · Java GUIHistory • Abstract Windowing Toolkit (AWT): Sun's initial effort to create a set of cross-platformGUIclasses. (JDK 1.0 -1.1)

JOptionPane: Option Dialog(2)

15

Page 16: IT2033: Object-Oriented Programming (OOP) · Java GUIHistory • Abstract Windowing Toolkit (AWT): Sun's initial effort to create a set of cross-platformGUIclasses. (JDK 1.0 -1.1)

EVENTLISTENERS

Page 17: IT2033: Object-Oriented Programming (OOP) · Java GUIHistory • Abstract Windowing Toolkit (AWT): Sun's initial effort to create a set of cross-platformGUIclasses. (JDK 1.0 -1.1)

Graphical events

• event: An object that represents a user's interaction with aGUI

component; can be "handled" to create interactivecomponents.

• listener: An object that waits for events and responds tothem.

– To handle an event, attach a listener to acomponent.

– The listener willbe notified when the event occurs (e.g. button click).

17

Page 18: IT2033: Object-Oriented Programming (OOP) · Java GUIHistory • Abstract Windowing Toolkit (AWT): Sun's initial effort to create a set of cross-platformGUIclasses. (JDK 1.0 -1.1)

Event-driven programming

• event-driven programming: A style of coding where a program's overall

flow of execution is dictated byevents.

– Rather than a central "main" method that drivesexecution,

the program loads and waits for user inputevents.

– As each event occurs, the program runs particular code to respond.

– The overall flow of what code is executed is determined by the series of eventsthat

occur, not a pre-determined order.

18

Page 19: IT2033: Object-Oriented Programming (OOP) · Java GUIHistory • Abstract Windowing Toolkit (AWT): Sun's initial effort to create a set of cross-platformGUIclasses. (JDK 1.0 -1.1)

Eventhierarchy

• EventObject

– AWTEvent (AWT)

• ActionEvent

• TextEvent

• ComponentEvent

– FocusEvent

– WindowEvent

– InputEvent

• KeyEvent

• MouseEvent

• EventListener

– AWTEventListener

– ActionListener

– TextListener

– ComponentListener

– FocusListener

– WindowListener

– KeyListener

– MouseListener

19

import java.awt.event.*;

Page 20: IT2033: Object-Oriented Programming (OOP) · Java GUIHistory • Abstract Windowing Toolkit (AWT): Sun's initial effort to create a set of cross-platformGUIclasses. (JDK 1.0 -1.1)

Action events

• action event: An action that has occurred on a GUIcomponent.

– The most common, general event type in Swing. Causedby:

• button or menuclicks,

• check box checking / unchecking,

• pressing Enter in a text field, ...

• Represented by a class named ActionEvent

• Handled by objects that implement interfaceActionListener

20

Page 21: IT2033: Object-Oriented Programming (OOP) · Java GUIHistory • Abstract Windowing Toolkit (AWT): Sun's initial effort to create a set of cross-platformGUIclasses. (JDK 1.0 -1.1)

Implementing aListener

public class name implements ActionListener {

public void actionPerformed(ActionEvent event) {

code to handle the event;

}

}

• JButton and other graphical components have this method:– public void addActionListener(ActionListener

al)

Attaches the given listener tobe notified of clicks and events that occur on thiscomponent.

21

Page 22: IT2033: Object-Oriented Programming (OOP) · Java GUIHistory • Abstract Windowing Toolkit (AWT): Sun's initial effort to create a set of cross-platformGUIclasses. (JDK 1.0 -1.1)

Action Listener: JButton

22

Page 23: IT2033: Object-Oriented Programming (OOP) · Java GUIHistory • Abstract Windowing Toolkit (AWT): Sun's initial effort to create a set of cross-platformGUIclasses. (JDK 1.0 -1.1)

Action Listener: JButton(2)

• What will happen if user input non numeric value

• Use exception handling, we will discuss this one later sample code ishas

follows:

23

Page 24: IT2033: Object-Oriented Programming (OOP) · Java GUIHistory • Abstract Windowing Toolkit (AWT): Sun's initial effort to create a set of cross-platformGUIclasses. (JDK 1.0 -1.1)

Action Listener: JButton(3)

• Use exception handling, we will discuss this one later sample code ishas

follows:

24

Page 25: IT2033: Object-Oriented Programming (OOP) · Java GUIHistory • Abstract Windowing Toolkit (AWT): Sun's initial effort to create a set of cross-platformGUIclasses. (JDK 1.0 -1.1)

•Remember to seta

Button group.

Action Listener: JCheckBox

25

Page 26: IT2033: Object-Oriented Programming (OOP) · Java GUIHistory • Abstract Windowing Toolkit (AWT): Sun's initial effort to create a set of cross-platformGUIclasses. (JDK 1.0 -1.1)

Action Listener: JCheckBox

26

Page 27: IT2033: Object-Oriented Programming (OOP) · Java GUIHistory • Abstract Windowing Toolkit (AWT): Sun's initial effort to create a set of cross-platformGUIclasses. (JDK 1.0 -1.1)

Action Listener: JCheckBox

27

Page 28: IT2033: Object-Oriented Programming (OOP) · Java GUIHistory • Abstract Windowing Toolkit (AWT): Sun's initial effort to create a set of cross-platformGUIclasses. (JDK 1.0 -1.1)

•Remember to seta

Button group.

Coding is similar to the checkboxes

Action Listener: JRadioButton

28

Page 29: IT2033: Object-Oriented Programming (OOP) · Java GUIHistory • Abstract Windowing Toolkit (AWT): Sun's initial effort to create a set of cross-platformGUIclasses. (JDK 1.0 -1.1)

• Use to select option

from alist

Action Listener: JComboBox andJList

Combo box

29

List

Page 30: IT2033: Object-Oriented Programming (OOP) · Java GUIHistory • Abstract Windowing Toolkit (AWT): Sun's initial effort to create a set of cross-platformGUIclasses. (JDK 1.0 -1.1)

Action Listener: JComboBox and JList: SampleCode

30

Page 31: IT2033: Object-Oriented Programming (OOP) · Java GUIHistory • Abstract Windowing Toolkit (AWT): Sun's initial effort to create a set of cross-platformGUIclasses. (JDK 1.0 -1.1)

31

Action Listener: JMenu

Page 32: IT2033: Object-Oriented Programming (OOP) · Java GUIHistory • Abstract Windowing Toolkit (AWT): Sun's initial effort to create a set of cross-platformGUIclasses. (JDK 1.0 -1.1)

THANKYOU!