站内搜索: 请输入搜索关键词
当前页面: 在线文档首页 > Java Tutorial 5.0 英文版

How to Set the Look and Feel - Java Tutorial 5.0 英文版

The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Creating a GUI with JFC/Swing
Lesson: Using Other Swing Features

How to Set the Look and Feel

If you don't care which look and feel your program uses, you can skip this section entirely. For example, most of the programs in this trail don't specify the look and feel, so that you can easily run the programs with your preferred look and feel.

When a program does not set its look and feel, the Swing UI manager must figure out which look and feel to use. It first checks whether the user has specified a preferred look and feel. If so, it attempts to use that. If not, or if the user's choice isn't valid, then the UI manager chooses the Java look and feel.


Version Note: Release 1.4.2 introduces two look and feels: GTK+ and Microsoft Windows XP.

You aren't limited to the look and feels supplied with the Java platform. You can use any look and feel that's in your program's class path. External look and feels are usually provided in one or more JAR files that you add to your program's class path at runtime. For example:

java -classpath .;C:\java\lnfdir\newlnf.jar SwingApplication

Once an external look and feel is in your program's class path, your program can use it just like any of the look and feels shipped with the Java platform.

Programmatically Setting the Look and Feel

To programmatically specify a look and feel, use the UIManager.setLookAndFeel method. For example, the bold code in the following snippet makes the program use the Java look and feel:
public static void main(String[] args) {
    try {
        UIManager.setLookAndFeel(
            UIManager.getCrossPlatformLookAndFeelClassName());
    } catch (Exception e) { }

    new SwingApplication(); //Create and show the GUI.
}


Note: If you are going to set the look and feel, you should do it as the very first step in your application. Otherwise you run the risk of initializing the Java look and feel regardless of what look and feel you've requested. This can happen inadvertently when a static field references a Swing class, which causes the look and feel to be loaded. If no look and feel has yet been specified, the default Java look and feel is loaded.

The argument to setLookAndFeel is the fully qualified name of the appropriate subclass of LookAndFeel. To specify the Java look and feel, we used the getCrossPlatformLookAndFeelClassName method. If you want to specify the native look and feel for whatever platform the user runs the program on, use getSystemLookAndFeelClassName instead. To specify a particular UI, you can use the actual class name. For example, if you design a program to look best with the GTK+ look and feel, you can use this code to set the look and feel:

UIManager.setLookAndFeel(
	    "com.sun.java.swing.plaf.gtk.GTKLookAndFeel");

Note: The GTK+ look and feel was released in all versions of the 1.4.2 SDK. However, it was not included in the 1.4.2 JRE release for Microsoft Windows. If you are running the 1.4.2 JRE for Windows and wish to use the GTK+ look and feel, you need to download the 1.4.2 SDK. We expect GTK+ to be shipped for all platforms in future JRE releases.

Here are some of the arguments you can use for setLookAndFeel:

UIManager.getCrossPlatformLookAndFeelClassName()
Returns the look and feel that works on all platforms — the Java look and feel.

UIManager.getSystemLookAndFeelClassName()
Specifies the look and feel for the current platform. On Microsoft Windows platforms, this specifies the Windows look and feel. On Mac OS platforms, this specifies the Mac OS look and feel. On other Unix platforms, such as Solaris or Linux, this returns the CDE/Motif look and feel.

"com.sun.java.swing.plaf.gtk.GTKLookAndFeel"
Specifies the GTK+ look and feel. Introduced in release 1.4.2. You can specify the particular theme either using a resource file (outside of the tutorial) or the gtkthemefile command-line parameter. Here is an example:
java -Dswing.gtkthemefile=customTheme/gtkrc Application

"javax.swing.plaf.metal.MetalLookAndFeel"
Specifies the Java look and feel. (The codename for this look and feel was Metal.)

"com.sun.java.swing.plaf.windows.WindowsLookAndFeel"
Specifies the Windows look and feel. Currently, you can use this look and feel only on Microsoft Windows systems.

Version Note: As of release 1.4.2, WindowsLookAndFeel has been updated to mimic the Windows XP look and feel when running on the Windows XP platform.

"com.sun.java.swing.plaf.motif.MotifLookAndFeel"
Specifies the CDE/Motif look and feel. This look and feel can be used on any platform.

You aren't limited to the preceding arguments. You can specify the name for any look and feel that is in your program's class path.

Specifying the Look and Feel: Command Line

You can specify the look and feel at the command line by using the -D flag to set the swing.defaultlaf property. For example:
java -Dswing.defaultlaf=com.sun.java.swing.plaf.gtk.GTKLookAndFeel MyApp

java -Dswing.defaultlaf=com.sun.java.swing.plaf.windows.WindowsLookAndFeel MyApp

Specifying the Look and Feel: swing.properties

Yet another way to specify the current look and feel is to use the swing.properties file to set the swing.defaultlaf property. This file is located in the lib directory of the Java release. For example, if you're using the Java interpreter in javaHomeDirectory\bin, then the swing.properties file (if it exists) is in javaHomeDirectory\lib. Here is an example of the contents of a swing.properties file:
# Swing properties

swing.defaultlaf=com.sun.java.swing.plaf.windows.WindowsLookAndFeel

How the UI Manager Chooses the Look and Feel

Here are the look-and-feel determination steps that occur when the UI manager first initializes itself:
  1. If the program sets the look and feel before any components are created, the UI manager tries to create an instance of the specified look-and-feel class. If successful, all components use that look and feel.

  2. If the program hasn't successfully specified a look and feel, then the UI manager uses the look and feel specified by the swing.defaultlaf property. If the property is specified in both the swing.properties file and on the command line, the command-line definition takes precedence.

  3. If none of these steps has resulted in a valid look and feel, the program uses the Java look and feel.

Changing the Look and Feel After Startup

You can change the look and feel with setLookAndFeel even after the program's GUI is visible. To make existing components reflect the new look and feel, invoke the SwingUtilities updateComponentTreeUI method once per top-level container. Then you might wish to resize each top-level container to reflect the new sizes of its contained components. For example:
UIManager.setLookAndFeel(lnfName);
SwingUtilities.updateComponentTreeUI(frame);
frame.pack();

Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.