Introduction to GUI
So far, we have covered most of the basic constructs of Java and introduced the important concept of Object-Oriented Programming (OOP). As discussed, OOP permits higher level of abstraction than the traditional procedural-oriented languages (such as C and Pascal). OOP lets you think in the problem space rather than the computer's bits and bytes. You can create high-level abstract data types calledclasses to mimic real-life things and represent entities in the problem space. These classes are self-contained and are reusable.
In this article, I shall show you how you can reuse the graphics classes provided in JDK for constructing your own Graphical User Interface (GUI) applications. Writing your own graphics classes (re-inventing the wheels) will take you many years! These graphics classes, developed by expert programmers, are highly complex and involve many advanced Java concepts. However, re-using them are not so difficult, if you follow the API documentation, samples and templates provided.
I shall also describe an important concept called nested class (or inner class) in this article.
There are two sets of Java APIs for graphics programming: AWT (Abstract Windowing Toolkit) and Swing.
- AWT API was introduced in JDK 1.0. Most of the AWT components have become obsolete and should be replaced by newer Swing components.
- Swing API, a much more comprehensive set of graphics libraries that enhances the AWT, was introduced as part of Java Foundation Classes (JFC) after the release of JDK 1.1. JFC, which consists of Swing, Java2D, Accessibility API, Internationalization, and Pluggable Look-and-Feel Support, was an add-on to JDK 1.1 but has been integrated into core Java since JDK 1.2.
Other than AWT/Swing Graphics APIs provided in JDK, others have also provided Graphics APIs that work with Java, such as Eclipse's Standard Widget Toolkit (SWT), Google Web Toolkit (GWT), 3D Graphics API such as Java bindings for OpenGL (JOGL) and Java3D.
Programming GUI with AWT
Java Graphics APIs - AWT and Swing - provide a huge set of reusable GUI components, such as button, text field, label, choice, panel and frame for building GUI applications. You can simply reuse these classes rather than re-invent the wheels. I shall start with the AWT classes before moving into Swing to give you a complete picture. I have to stress that many AWT component classes are now obsolete. They are used only in exceptional circumstances when the JRE supports only JDK 1.1.
2.1 AWT Packages
AWT is huge! It consists of 12 packages (Swing is even bigger, with 18 packages as of JDK 1.7!). Fortunately, only 2 packages -
java.awt and java.awt.event - are commonly-used.- The
java.awtpackage contains the core AWT graphics classes:- GUI Component classes (such as
Button,TextField, andLabel), - GUI Container classes (such as
Frame,Panel,DialogandScrollPane), - Layout managers (such as
FlowLayout,BorderLayoutandGridLayout), - Custom graphics classes (such as
Graphics,ColorandFont).
- GUI Component classes (such as
- The
java.awt.eventpackage supports event handling:- Event classes (such as
ActionEvent,MouseEvent,KeyEventandWindowEvent), - Event Listener Interfaces (such as
ActionListener,MouseListener,KeyListenerandWindowListener), - Event Listener Adapter classes (such as
MouseAdapter,KeyAdapter, andWindowAdapter).
- Event classes (such as
AWT provides a platform-independent and device-independent interface to develop graphic programs that runs on all platforms, such as Windows, Mac, and Linux.
No comments:
Post a Comment