it2033: object-oriented programming (oop) · java guihistory • abstract windowing toolkit (awt):...

Post on 18-Mar-2020

9 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

IT2033: Object-Oriented Programming

(OOP)

GRAPHICAL USER INTERFACES

(GUIS) WITH SWING/AWT

Chapter 6

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

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

Components

5

Components(2)

6

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.*;

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

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

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

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

JOptionPane: Message Dialog

• JOptionPane provides support for laying out standarddialogs,

– providing icons,

– specifying the dialog title andtext,

– customizing the button text.

12

JOptionPane: Message Dialog (2)

13

JOptionPane: Option Dialog

14

JOptionPane: Option Dialog(2)

15

EVENTLISTENERS

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

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

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.*;

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

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

Action Listener: JButton

22

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

Action Listener: JButton(3)

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

follows:

24

•Remember to seta

Button group.

Action Listener: JCheckBox

25

Action Listener: JCheckBox

26

Action Listener: JCheckBox

27

•Remember to seta

Button group.

Coding is similar to the checkboxes

Action Listener: JRadioButton

28

• Use to select option

from alist

Action Listener: JComboBox andJList

Combo box

29

List

Action Listener: JComboBox and JList: SampleCode

30

31

Action Listener: JMenu

THANKYOU!

top related