Java GUI Programming Essentials
Learn about Java GUI programming with Swing classes, building user interfaces, event handling, and layout managers. Develop interactive applications using Java's GUI components efficiently.
Java GUI Programming Essentials
E N D
Presentation Transcript
Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal emreilal@iyte.edu.tr
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
JFrame JDialog JApplet Main windows • 3 often used windows (top-level containers)
Containers JScrollPane JPanel JToolBar JTabbedPane JSplitPane
Controls (widgets) JSpinner JSlider JTextField JMenu JComboBox JList JButton JRadioButton JCheckBox
Information JToolTip JLabel JProgressBar
Complex Classes JColorChooser JFileChooser JTree JTable
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.
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.
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 Read 785 Vowels 5543 Consonents 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...