Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal [email protected].

20
Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal [email protected]

Transcript of Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal [email protected].

Page 1: Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal emreilal@iyte.edu.tr.

Object Oriented Programming

Ders 11: Interfaces

Mustafa Emre İ[email protected]

Page 2: Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal emreilal@iyte.edu.tr.

Recap

• Assignment 9

• The Collections Framework

Page 3: Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal emreilal@iyte.edu.tr.

Today

• Assignment 10

• Interfaces – Swing classes

• Thinking in Java – Chapter 14

Page 4: Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal emreilal@iyte.edu.tr.

User Interface

• Graphical User Interface [GUI] • "gooey"• Every operating system’s (platform) windowing

mechanisms differ vastly• For each platform the GUI code needs to be

redesigned• Java interface runs on the Java platform and is

developed only once

Page 5: Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal emreilal@iyte.edu.tr.

java.awt.* and javax.swing.*

• Java’s GUI classes are in these two packages• AWT (Abstract Windowing Toolkit) – old; were

not totally platform independent– Button– TextField

• Swing (part of JFC) – recommended; platform independent, provides more options– JButton– JTextField

Page 6: Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal emreilal@iyte.edu.tr.

Main windows

• 3 often used windows (top-level containers)

JFrame

JDialog

JApplet

Page 7: Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal emreilal@iyte.edu.tr.

Containers

JPanel

JScrollPane

JTabbedPane

JToolBar

JSplitPane

Page 8: Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal emreilal@iyte.edu.tr.

Controls (widgets)

JMenu

JSpinnerJSlider

JTextField

JComboBox

JButtonJList

JRadioButtonJCheckBox

Page 9: Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal emreilal@iyte.edu.tr.

Information

JProgressBar

JLabelJToolTip

Page 10: Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal emreilal@iyte.edu.tr.

Complex Classes

JFileChooser

JTree

JTable

JColorChooser

Page 11: Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal emreilal@iyte.edu.tr.

Windows

• Components are placed in containers which are in turn placed inside other containers to form a tree-like hierarchy

Page 12: Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal emreilal@iyte.edu.tr.

Layout Manager

• When components are placed into a containers, where they appear is controlled by rules that are handled by LayoutManager s

• Each container will have a LayoutManager. • Various LayoutManagers:

– FlowLayout– GridLayout– BorderLayout– etc.

Page 13: Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal emreilal@iyte.edu.tr.

Example Layouts

Page 14: Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal emreilal@iyte.edu.tr.

Event

• User interacts with our application through the operating system interface

• Keyboard and mouse is tracked by the OS. Movement of the mouse, click on a button, pressing on a key are all “events”

• Applications are notified by the OS about events that occur over their windows. Making sense out of events (user requests) and responding to them are the applications’ responsibility.

Page 15: Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal emreilal@iyte.edu.tr.

Event• The events that are sent by the OS to the Java apps are

represented by either an “awt event” or a “swing event” and are associated with the component they occur on.

AWTEvent

ActionEvent ComponentEvent WindowEvent

MouseEvent MouseMotionEvent FocusEvent

KeyEvent ContainerEvent HierarchyEvent

InputMethodEvent ItemEvent vs

Swing Event

ListSelectionListener MenuListener HyperlinkListener

TreeSelectionListener DocumentEvent vs

Page 16: Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal emreilal@iyte.edu.tr.

EventListener• When an event takes place, starting with the

component it takes place on, all the elements in the GUI hierarchy above the source component gets notified

• These components in turn notify all the EventListeners that have registered with them for the type of event that has occured

• EventListeners need to be written by you and have to implement the proper EventListener interface.

Page 17: Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal emreilal@iyte.edu.tr.

Example – EventListener

public class MyClass implements ActionListener {

...

public void actionPerformed(ActionEvent e) {

...

}

}

MyClass mc = new MyClass();

someComponent.addActionListener(mc);

Page 18: Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal emreilal@iyte.edu.tr.

Assignment 12a

• Write an application that reads a text file and displays the number of vowels and consonents on a GUI.

TextFile.txt

Consonents

Vowels

Read

785

5543

Reading complete

Page 19: Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal emreilal@iyte.edu.tr.

Assignment 12b

• Develop a GUI for the application you have written for assignment 11 :– Whenever the “Create Shapes” button is pressed, create

10 random shapes and display them on the canvas. (pay attention to shape sizes)

Create Shapes

Page 20: Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal emreilal@iyte.edu.tr.

Next Week

• Final exam?

• Review• Projects...