O'Reilly logo

Java Examples in a Nutshell, 3rd Edition by David Flanagan

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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.

The ScribbleApp application

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required