Java Reference
In-Depth Information
5.
Try resizing the window. What works and what does not? What could look better? Keep these
aspects in mind for later.
6.
Note that Java, by default, will pick a “look and feel” to match a cross‐platform theme. You might
have noted that the button Java shows looks nothing like a normal Windows button. If you want
to select another Swing look, try adding the following code at the top of the main method:
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException
| IllegalAccessException | UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
(Eclipse will suggest which classes to import.) When you run the program again, you will see the win-
dow in Figure 11-2, which looks a lot more similar to your normal system environment.
figure 11-2  
If you've followed along with the Try It Out (or just looked at the pictures), you might note that
Swing will handle the look and feel of the buttons you've added to your window, but the window
itself—its title bar; minimize, maximize, and close buttons; and border—still look as if they were
drawn by the operating system. So what gives?
The explanation behind this is that there are four Swing components that are actually not lightweight
and thus still drawn and displayed on-screen by calling an underlying operating system function. These
are JFrame , JDialog , JWindow , and JApplet , which are all the container components that have some
kind of border that's displayed in a window. Instead of Swing drawing these windows by itself (and
thus also determining the look and feel of title bars and borders), it was decided to continue offloading
this to the operating system, and that is why the actual window retains its look in this example.
Note Actually, it is possible to define so‐called “undecorated” windows in
Swing, if you do want to draw your own custom controls for taking care of win-
dow management or apply custom borders. It is also possible to change the
opacity (transparency) and shape of windows, so it is possible to make windows
that look like whatever you want, although it requires some advanced usage of
Swing and takes much more code than showing a standard looking window.
On another note, you might expect there to be a wealth of Swing look and feel to choose from, but
in actuality, the number of high‐quality “themes” out there is very limited. The main reason for this
is that creating a complete look-and-feel package is very complex (especially when all components
Search WWH ::




Custom Search