[Java Swing]如何设置Look & Feel

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:

PlatformLook and Feel
Solaris, Linux with GTK+ 2.2 or laterGTK+
Other Solaris, LinuxMotif
IBM UNIXIBM*
HP UXHP*
Classic WindowsWindows
Windows XPWindows XP
Windows VistaWindows Vista
MacintoshMacintosh*

 

 

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 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值