Look and Feel

"Look and feel" is a way to make your Java application or applet look like the operating system on which it is being executed, without changing any code. As you can guess, because Java works on most platforms, this is a really useful tool, and it's easy to implement.

As we mentioned earlier in this chapter, it is only possible to use look and feel with Swing. The reason we cannot use it with AWT is that AWT uses heavyweight components, whereas Swing components are actually drawn purely in Java. So it is then possible to change the look (and feel) of the components.

Let's now look at a simple application, which will look and feel like the operating system on which it is being executed.

Code Listing 8-7: Look and feel example

start example
import javax.swing.*;   public class LookandFeel extends JFrame {     public LookandFeel()     {         super("Look and Feel Example");         setSize(400, 300);         setDefaultCloseOperation(EXIT_ON_CLOSE);         setVisible(true);     }       public static void main(String args[])     {         try         {             UIManager.setLookAndFeel(UIManager.getSystemLookAnd                 FeelClassName());         }         catch (Exception e)         {             System.out.println(e);         }              LookandFeel theApp = new LookandFeel();     } }
end example

When we run the example, you will see the following window appear on the screen (see Figure 8-5).

click to expand
Figure 8-5: Java look and feel

This example was run on the Windows platform and therefore uses the Windows-style look and feel. If you compare it to previous windows that we created earlier in this chapter, you can see that the gray color of the windows differs. There are more noticeable differences than this of course, which you will take note of when using different GUI components with different look and feels. We will look at GUI components in more detail in Chapter 13, "Introduction to GUI."

To change the look and feel, we can simply call the static setLookAndFeel method of the UIManager class. In our example, we pass in a call to the getSystemLookAndFeelClassName static method of the UIManager class. This can be seen in the following code segment:

try {     UIManager.setLookAndFeel(UIManager.getSystemLookAnd         FeelClassName()); } catch (Exception e) {     System.out.println(e); }

This checks to see which operating system you are running the application on and then selects the appropriate look and feel for it.

Alternatively, we can also specify the look and feel directly, but we are not always guaranteed that it will work, as not all implementations of Java have all the look and feels available. Here is a list of the available look and feels we can use:

Look and Feel Class Name

Description

UIManager.getCrossPlatformLookAndFeelClassName()

This one is always available, as it is the default.

UIManager.getSystemLookAndFeelClassName()

This is the one used in the previous example and will make the application/ applet look and feel like the operating system on which it is running.

"javax.swing.plaf.metal.MetalLookAndFeel"

If available, this will give the application/ applet a metallic look.

"com.sun.java.swing.plaf.windows. WindowsLookAndFeel"

If available, this will make the application/applet look like it is running on the Windows platform.

"com.sun.java.swing.plaf.motif.MotifLookAndFeel"

If available, this will make the application/applet look like CDE/Motif.

"javax.swing.plaf.mac.MacLookAndFeel"

If available, this will make the application/applet look like it is running on the Mac OS.



Java 1.4 Game Programming
Java 1.4 Game Programming (Wordware Game and Graphics Library)
ISBN: 1556229631
EAN: 2147483647
Year: 2003
Pages: 237

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net