Look and Feel


The three most popular types of GUI look and feel are Metal, Common Desktop Environment (CDE)/Motif, and Windows . Swing enables you to choose one for your application so that it looks the same on all platforms. For example, if you like the Unix look, you can use CDE/Motif even when the user is running your application on a Windows box. By default, Java uses Metal as the cross-platform look and feel. The following code shows you how to list the available themes:

 UIManager.LookAndFeelInfo[] theme =             UIManager.getInstalledLookAndFeels(); for (int i=0; i<theme.length; i++) {       System.out.println(theme[i].getName());       System.out.println("--" + theme[i].getClassName()); } //RETURNS: //Metal //--javax.swing.plaf.metal.MetalLookAndFeel //CDE/Motif //--com.sun.java.swing.plaf.motif.MotifLookAndFeel //Windows //--com.sun.java.swing.plaf.windows.WindowsLookAndFeel 

You can use the code in Listing 14.1 to actually change your application's look and feel from the command line.

Listing 14.1 How to Set an Application's Look and Feel
 //some people prefer to list all components //others prefer plain javax.swing.* import javax.swing.JLabel; import javax.swing.JButton; import javax.swing.JTextField; import javax.swing.JPanel; import javax.swing.JFrame; import javax.swing.UIManager; import java.awt.BorderLayout; public class LookFeelApplication {     public LookFeelApplication()     {         final JLabel label = new JLabel("Click Here:");         JButton button = new JButton("OK");         JTextField  text = new JTextField("Type Here");         JPanel pane = new JPanel();         pane.add(label);         pane.add(button);         pane.add(text);         JFrame frame = new JFrame("Look-Feel");         frame.getContentPane().add(pane, BorderLayout.CENTER);         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         frame.pack();         frame.setVisible(true);     }     public static void main(String[] args)     {         String metal = "javax.swing.plaf.metal."                      + "MetalLookAndFeel";         String motif = "com.sun.java.swing.plaf."                      + "motif.MotifLookAndFeel";         String windows = "com.sun.java.swing.plaf."                      + "windows.WindowsLookAndFeel";         try         {             if( args[0].equals("motif") )             {                 UIManager.setLookAndFeel(motif);             }else if( args[0].equals("metal") )             {                 UIManager.setLookAndFeel(metal);             }else if( args[0].equals("windows") )             {                 UIManager.setLookAndFeel(windows);             }else             {                 UIManager                   .setLookAndFeel(                   UIManager                   .getCrossPlatformLookAndFeelClassName());             }         } catch (Exception e) {}         //Create the top-level container and add contents to it.         LookFeelApplication app = new LookFeelApplication();     } } 

Listing 14.1 generates a simple window containing a Label, a Button, and a TextField in the three available themes. From the command line, the user can type java LookFeelApplication metal to see the window shown in Figure 14.1. The other themes are shown in Figures 14.2 and 14.3.

Figure 14.1. The Metal look and feel.

graphics/14fig01.jpg

Figure 14.2. The Windows look and feel.

graphics/14fig02.jpg

Figure 14.3. The Motif look and feel.

graphics/14fig03.jpg

For the assignment, you can use the Metal default, which means you don't have to bother with setting the look and feel. However, if you choose to override Metal with the Motif or Windows theme, be consistent if you use more than one window.



JavaT 2 Developer Exam CramT 2 (Exam CX-310-252A and CX-310-027)
JavaT 2 Developer Exam CramT 2 (Exam CX-310-252A and CX-310-027)
ISBN: N/A
EAN: N/A
Year: 2003
Pages: 187

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