A Complete GUI
We’ve looked separately at components, containers, layout management, and event handling, so now it is time to tie these pieces together and add the additional details required to create a complete graphical user interface. Example 11-15 lists ScribbleApp.java, a simple paint-style application, pictured in Figure 11-13.
Figure 11-13. The ScribbleApp application
This application relies on the scribbling capabilities
of the ScribblePane
class of Example 11-13. It places a
ScribblePane
instance within a
JFrame
container to create the main
application window and then adds a JMenuBar
and two JToolBar
components to allow the user to
control the application. ScribbleApp
uses a JColorChooser
to let the user select a
drawing color and a JOptionPane
to
display a confirmation dialog when the user asks to quit. You should
pay particular attention to how these five Swing components are used;
most full-featured applications use them in similar ways. Note that
Example 11-15 is a complete
application; it is designed to be run standalone, not to be viewed
using the ShowBean
program.
However, the ScribbleApp
class is
designed as a subclass of JFrame
,
so that other applications can instantiate ScribbleApp
windows of their own if they so
choose.
This example also introduces the javax.swing.Action
interface, which is a
subinterface of java.awt.event.ActionListener ...
Get Java Examples in a Nutshell, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.