[Java Swing]如何設定Look & Feel

來源:互聯網
上載者:User

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 platformUIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");



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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.