现在的位置: 首页 > 综合 > 正文

[Java Swing]如何设置Look & Feel

2012年05月18日 ⁄ 综合 ⁄ 共 3009字 ⁄ 字号 评论关闭

Sun's JRE provides the following L&Fs:

  1. CrossPlatformLookAndFeel—this is the "Java L&F" (also called "Metal") that looks the same on all platforms. It is part of the Java API (javax.swing.plaf.metal) and is the default that will be used if you do nothing in your code to set a different L&F.
  2.  

  3. SystemLookAndFeel—here, the application uses the L&F that is native to the system it is running on. The System L&F is determined at runtime, where the application asks the system to return the name of the appropriate L&F.
  4.  

  5. Synth—the basis for creating your own look and feel with an XML file.
  6.  

  7. Multiplexing— a way to have the UI methods delegate to a number of different look and feel implementations at the same time.

 

In summary, when you use the SystemLookAndFeel, this is what you will see:

Platform Look and Feel
Solaris, Linux with GTK+ 2.2 or later GTK+
Other Solaris, Linux Motif
IBM UNIX IBM*
HP UX HP*
Classic Windows Windows
Windows XP Windows XP
Windows Vista Windows Vista
Macintosh Macintosh*

 

 

In the API you will see four L&F packages:

  • javax.swing.plaf.basic—basic UI Delegates to be extended when creating a custom L&F
  •  

  • javax.swing.plaf.metal—the Java L&F, also known as the CrossPlatform L&F ("Metal" was the Sun project name for this L&F) The current default "theme" (discussed below) for this L&F is "Ocean, so this is often referred to as the Ocean L&F.
  •  

  • javax.swing.plaf.multi—a multiplexing look and feel that allows the UI methods to delegate to a number of look and feel implementations at the same time. It can be used to augment the behavior of a particular look and feel, for example with a L&F that provides audio cues on top of the Windows look and feel. This is a way of creating a handicapped-accessible look and feel.
  •  

  • javax.swing.plaf.synth—an easily configured L&F using XML files (discussed in the next section of this lesson)

 

 

To programatically specify a L&F, use the UIManager.setLookAndFeel() method with the fully qualified name of the appropriate subclass of LookAndFeel as its argument. For example, the bold code in the following snippet makes the program use the cross-platform Java L&F:

public static void main(String[] args) {
    try {
	    // Set cross-platform Java L&F (also called "Metal")
        UIManager.setLookAndFeel(
            UIManager.getCrossPlatformLookAndFeelClassName());
    } 
    catch (UnsupportedLookAndFeelException e) {
       // handle exception
    }
    catch (ClassNotFoundException e) {
       // handle exception
    }
    catch (InstantiationException e) {
       // handle exception
    }
    catch (IllegalAccessException e) {
       // handle exception
    }
	
    new SwingApplication(); //Create and show the GUI.
}

Alternatively, this code makes the program use the System L&F:

public static void main(String[] args) {
    try {
	    // Set System L&F
        UIManager.setLookAndFeel(
            UIManager.getSystemLookAndFeelClassName());
    } 
    catch (UnsupportedLookAndFeelException e) {
       // handle exception
    }
    catch (ClassNotFoundException e) {
       // handle exception
    }
    catch (InstantiationException e) {
       // handle exception
    }
    catch (IllegalAccessException e) {
       // handle exception
    }

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

You can also use the actual class name of a Look and Feel as the argument to UIManager.setLookAndFeel(). For example,

// Set cross-platform Java L&F (also called "Metal")
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");

or

// Set Motif L&F on any platform
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");



原文来自:http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 

抱歉!评论已关闭.