📄 plaf.html
字号:
<li> <a href="#themes">Themes</a><li> <a href="#swingset2">The SwingSet2 Demonstration Program</a></ul><h3 fmstyle=C-Head><a name="available">Available Look and Feels</a></h3>Sun's JRE provides the following L&Fs:<ol><li><code>CrossPlatformLookAndFeel</code>—this is the "Java L&F" (also called "Metal") that looks the same on all platforms. It is part of the Java API (<code>javax.swing.plaf.metal</code>) and is the default that will be used if you do nothing in your code to set a different L&F.</li><p><li><code>SystemLookAndFeel</code>—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. </li><li>Synth—the basis for creating your own look and feel with an XML file.</li><li>Multiplexing— a way to have the UI methods delegate to a number of different look and feels at the same time. </li></ol><p>For Linux and Solaris, the System L&Fs are "GTK+" if GTK+ 2.2 or later is installed, "Motif" otherwise. For Windows, the System L&F is "Windows," which mimics the L&F of the particular Windows OS that is running—classic Windows, XP, or Vista. The GTK+, Motif, and Windows L&Fs are provided by Sun and shipped with the Java SDK and JRE, although they are not part of the Java API. <p>Apple provides its own JVM which includes their proprietary L&F.<p>In summary, when you use the <code>SystemLookAndFeel</code>, this is what you will see:<P><table border="2" cellpadding="2" cellspacing="2"><title>System Look and Feel</title><tr><th> <b>Platform</b></th><th> <b>Look and Feel</b></th></tr><tr><td> Solaris, Linux with GTK+ 2.2 or later</td><td>GTK+</td></tr><tr><td> Other Solaris, Linux </td><td>Motif</td></tr><tr><td>IBM UNIX</td><td>IBM*</td></tr><tr><td>HP UX</td><td>HP*</td></tr><tr><td>Classic Windows</td><td>Windows</td></tr><tr><td>Windows XP</td><td>Windows XP</td></tr><tr><td>Windows Vista</td><td>Windows Vista</td></tr><tr><td>Macintosh</td><td>Macintosh*</td></table>* Supplied by the system vendor.<p>You don't see the System L&F in the API. The GTK+, Motif, and Windows packages that it requires are shipped with the Java SDK as:<p><blockquote><pre>com.sun.java.swing.plaf.gtk.GTKLookAndFeelcom.sun.java.swing.plaf.motif.MotifLookAndFeelcom.sun.java.swing.plaf.windows.WindowsLookAndFeel</pre></blockquote>Note that the path includes <code>java</code>, and not <code>javax</code>.<p><blockquote><hr><strong>Note:</strong> The GTK+ L&F will only run on UNIX or Linux systems with GTK+ 2.2 or later installed, while the Windows L&F runs only on Windows systems. Like the Java (Metal) L&F, the Motif L&F will run on any platform.<hr></blockquote><p>All of Sun's L&Fs have a great deal of commonality. This commonality is defined in the <code>Basic </code> look and feel in the API (<code>javax.swing.plaf.basic</code>). The Motif and Windows L&Fs are each built by extending the UI delegate classes in <code>javax.swing.plaf.basic</code> (a custom L&F can be built by doing the same thing). The "Basic" L&F is not used without being extended.<p>In the API you will see four L&F packages:<ul><li><code>javax.swing.plaf.basic</code>—basic UI Delegates to be extended when creating a custom L&F</li><p><li><code>javax.swing.plaf.metal</code>—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.</li><p><li><code>javax.swing.plaf.multi</code>—a multiplexing L&F that allows the UI methods to delegate to anumber of look and feels at the same time. It can be used to augment the behavior of a particular L&F, for example with a L&F that provides audio cues on top of the Windows L&F. This is a way of creating a handicapped-accessible L&F.</li><p><li><code>javax.swing.plaf.synth</code>—an easily configured L&F using XML files (discussed in the next section of this lesson)</li></ul><P><!-- Synth works by using XML files to create UI Delegates. Instead of writing code, you create an XML file that follows the Synth DVD. Synth then creates the UI Delegate classes for each JComponent based on your XML element for that widget.<p> -->You aren't limited to the L&Fs supplied withthe Java platform. You can use any L&F that is in your program's class path. External L&Fs are usually provided in one or more JAR files that you add to your program's class path at runtime. For example:<blockquote><pre>java -classpath .;C:\java\lafdir\customlaf.jar YourSwingApplication</pre></blockquote><p>Once an external L&F is in your program's class path,your program can use it just like any of the L&Fsshipped with the Java platform.<p></blockquote><h3><a name="programmatic">Programatically Setting the Look and Feel</a></h3><blockquote><blockquote><hr><strong>Note:</strong> If you are going to set the L&F,you should do it as the very first step in your application.Otherwise you run the risk of initializing the Java L&Fregardless of what L&F you'verequested. This can happen inadvertently when a static fieldreferences a Swing class, which causes the L&Fto be loaded. If no L&F has yet beenspecified, the default L&F for the JRE is loaded. For Sun's JRE the default is the Java L&F, for Apple's JRE the Apple L&F, and so forth.<hr></blockquote><p>The L&F that Swing components use is specified by way of the <code>UIManager</code> class in the <code>javax.swing</code> package. Whenever a Swing component is created,the component asks the UI manager for theUI delegate that implements the component's L&F. For example, each <code>JLabel</code> constructor queries the UI manager for the UI delegate object appropriate for the label. It then uses that UI delegate object to implement all of its drawing and event handling.<p>To programatically specify a L&F,use the <code>UIManager.setLookAndFeel()</code> method with the fully qualified name of the appropriate subclass of <code>LookAndFeel</code> as its argument.For example, the bold code in the following snippetmakes the program use the cross-platform Java L&F:<blockquote><pre>public static void main(String[] args) { try { // Set cross-platform Java L&F (also called "Metal") <b>UIManager.setLookAndFeel( UIManager.getCrossPlatformLookAndFeelClassName());</b> } 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.}</pre></blockquote><p>Alternatively, this code makes the program use the System L&F:<blockquote><pre>public static void main(String[] args) { try { // Set System L&F <b>UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName());</b> } 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.}</pre></blockquote><p>You can also use the actual class name of a Look and Feel as the argument to <code>UIManager.setLookAndFeel()</code>. For example,<blockquote><pre>// Set cross-platform Java L&F (also called "Metal")UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");</pre></blockquote>or<blockquote><pre>// Set Motif L&F on any platformUIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");</pre></blockquote><p>You aren't limited to the preceding arguments.You can specify the name for any L&F that is in your program's class path.</blockquote><h3><a name="commandLine">Specifying the Look and Feel: Command Line</a></h3><blockquote>You can specify the L&F at the command line byusing the <code>-D</code> flag to set the<code>swing.defaultlaf</code> property. For example:<blockquote><pre>java -Dswing.defaultlaf=com.sun.java.swing.plaf.gtk.GTKLookAndFeel MyAppjava -Dswing.defaultlaf=com.sun.java.swing.plaf.windows.WindowsLookAndFeel MyApp</pre></blockquote></blockquote><h3><a name="properties">Specifying the Look and Feel: swing.properties File</a></h3><blockquote>Yet another way to specify the current L&F is to use the<code>swing.properties</code> file to set the <code>swing.defaultlaf</code>property. This file, which you may need to create, is locatedin the <code>lib</code> directory of Sun's Java release (other vendors of Java may use a different location).For example, if you're using the Java interpreterin <code><em>javaHomeDirectory</em>\bin</code>,then the <code>swing.properties</code> file (if it exists)is in <code><em>javaHomeDirectory</em>\lib</code>.Here is an example of the contents of a <a href="examples/swing.properties"><code>swing.properties</code></a>file:<!--<blockquote><pre># Swing propertiesswing.defaultlaf=com.sun.java.swing.plaf.windows.WindowsLookAndFeel</pre></blockquote>--><blockquote><pre># Swing propertiesswing.defaultlaf=com.sun.java.swing.plaf.windows.WindowsLookAndFeel</pre></blockquote></blockquote><h3><a name="steps">How the UI Manager Chooses the Look and Feel </a></h3><blockquote>Here are the look-and-feel determination steps that occur when the UI manager needs to set a L&F:<ol><li> If the program sets the L&F before a look and feel is needed, the UI manager tries to create an instance of the specified look-and-feel class. If successful, all components use that L&F.<p><li> If the program hasn't successfully specified a L&F, then the UI manager uses the L&F specified by the <code>swing.defaultlaf</code> property. If the property is specified in both the <code>swing.properties</code> file <em>and</em> on the command line, the command-line definition takes precedence.<p><li> If none of these steps has resulted in a valid L&F, Sun's JRE uses the Java L&F. Other vendors, such as Apple, will use their default L&F.</ol></blockquote><h3><a name="dynamic">Changing the Look and Feel After Startup</a></h3>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -