Java Reference
In-Depth Information
Chapter 9
Fun wi th Swing
9.1
Introduction to Swing
.................................................................. 193
9.2
Creating Windows
...................................................................... 194
9.3
Panels and Drawing
.................................................................... 198
9.4
Summary ................................................................................ 209
9.5
Syntax
.................................................................................. 209
9.6
Important Points
....................................................................... 211
................................................................................
9.7
Exercises
211
9.8
Lab ...................................................................................... 212
If you have ever used a graphical Java application, you may have noticed that Java has its
own way of displaying windows, menus, buttons, and toolbars. In fact, Java has its own
library for displaying graphical components called Swing . Swing allows Java to provide the
same look and feel under different operating systems. In this chapter we will explore basic
Swing primitives, including drawing formatted text, shapes, and images inside a window.
9.1 Introduction to Swing
A graphical user interface (GUI) allows a program to provide a graphical interface to the
user. Unlike a text interface that involves writing text to the console and reading text from
the keyboard, a GUI allows the user to see graphics and interact with the system not only
using the keyboard, but also using the mouse and/or other pointing devices. The advantage
of using a GUI is that it is more intuitive and easier to learn. A GUI program is usually
written on top of a graphical operating system, such as Microsoft Windows, X Windows, or
Apple Mac OS X. Windows are typically the basic component of current graphical operating
systems, where every application displays one or more windows. All components are usually
displayed inside the windows.
In Java, there are two distinct ways to display a window and the graphical components
inside it: using Swing and using the Abstract Window Toolkit (AWT) . While AWT uses the
primitives (that is, the application programming interface (API)) of the operating system,
Swing relies on the Java native implementation to draw the components inside the windows.
Menus, scroll bars, and text fields will look and behave differently under different operat-
ing systems under AWT. This implies that an application needs to be tested under different
operating systems before it can be released, which complicates the application development
process. In contrast, a Swing application will look exactly the same under any operating
system. The downside is that Swing is a little slower because the windows are painted by
the Java virtual machine rather than by the operating system. However, with today's fast
computers, the difference in speed is negligible.
This chapter teaches us how to use Swing to write GUI programs. There is a perception
among some programmers that writing GUI code is not hard work. In fact, you do not
even need to write actual code. You can just use the mouse to drag and drop buttons,
193
 
Search WWH ::




Custom Search