Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal
Recap Assignment 9 The Collections Framework
Today Assignment 10 Interfaces – Swing classes Thinking in Java – Chapter 14
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
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
Main windows 3 often used windows (top-level containers) JFrame JDialog JApplet
Containers JPanel JScrollPane JTabbedPane JToolBar JSplitPane
Controls (widgets) JMenu JSpinner JSlider JTextField JComboBox JButton JList JRadioButton JCheckBox
Information JProgressBar JLabel JToolTip
Complex Classes JFileChooser JTree JTable JColorChooser
Windows Components are placed in containers which are in turn placed inside other containers to form a tree- like hierarchy
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.
Example Layouts
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.
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 ActionEventComponentEventWindowEvent MouseEventMouseMotionEventFocusEvent KeyEventContainerEventHierarchyEvent InputMethodEventItemEventvs Swing Event ListSelectionListenerMenuListenerHyperlinkListener TreeSelectionListenerDocumentEventvs
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.
Example – EventListener public class MyClass implements ActionListener {... public void actionPerformed(ActionEvent e) {... } MyClass mc = new MyClass(); someComponent.addActionListener(mc);
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 Reading complete
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
Next Week Final exam? Review Projects...