Java Reference
In-Depth Information
Here are some common
LookAndFeel
subclasses:
•
javax.swing.plaf.metal.MetalLookAndFeel
•
com.sun.java.swing.plaf.windows.WindowsLookAndFeel
•
com.sun.java.swing.plaf.motif.MotifLookAndFeel
•
com.sun.java.swing.plaf.gtk.GTKLookAndFeel
•
com.apple.mrj.swing.MacLookAndFeel
■
Caution
The only look and feel that is guaranteed to exist on all platforms is the one that is in the stan-
dard Java packages:
javax.swing.plaf.metal.MetalLookAndFeel
. All other look-and-feel packages are
in nonstandard packages—the
com.sun
or
com.apple
(or other vendor) packages. The vendor packages
probably won't exist in JVMs produced by other vendors, and may not even exist in all JVMs produced by a
particular vendor. For example, the
WindowsLookAndFeel
is only available on Microsoft Windows platforms,
and the
GTKLookAndFeel
is only available on platforms supporting GTK (typically Unix and Unix-like systems).
Since we have been using the Ocean theme from the Metal look and feel for the previous
examples, Listing 8-4 will show an example of setting the look and feel to the Microsoft Win-
dows look and feel, or to a look and feel specified on the command line. An example of how
this will look is shown in Figure 8-11. Other look-and-feel examples are shown in Figures 8-12
through 8-15.
Listing 8-4.
Setting the Microsoft Windows Look and Feel
import java.awt.*;
import javax.swing.*;
public class MyFrame extends JFrame {
public static void main(String[] args) throws Exception {
new MyFrame(args).setVisible(true);
}
public MyFrame(String[] args) throws Exception {
String lookAndFeelName = (args.length > 0)
? args[0]
: "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
UIManager.setLookAndFeel(lookAndFeelName);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Panel topPanel = new Panel(new FlowLayout(FlowLayout.LEFT));
topPanel.add(new JTextField(15));
add(topPanel, BorderLayout.NORTH);