📄 plaf.html
字号:
<blockquote>You can change the L&F with <code>setLookAndFeel</code>even after the program's GUI is visible.To make existing components reflect the new L&F,invoke the<code>SwingUtilities</code> <code>updateComponentTreeUI</code> methodonce per top-level container.Then you might wish to resize each top-level containerto reflect the new sizes of its contained components.For example:<blockquote><pre>UIManager.setLookAndFeel(lnfName);SwingUtilities.updateComponentTreeUI(frame);frame.pack();</pre></blockquote></blockquote><h3><a name="example">An Example</a></h3><blockquote>In the following example, <code>LookAndFeelDemo.java</code>, you can experiment with different Look and Feels. The program creates a simple GUI with a button and a label. Every time you click the button, the label increments.<p>You can change the L&F by changing the <code>LOOKANDFEEL</code> constant on line 18. The comments on the preceding lines tell you what values are acceptable:<blockquote><pre> // Specify the look and feel to use by defining the LOOKANDFEEL constant // Valid values are: null (use the default), "Metal", "System", "Motif", // and "GTK" final static String LOOKANDFEEL = "Motif";</pre></blockquote>Here the constant is set to "Motif", which is a L&F that will run on any platform (as will the default, "Metal"). "GTK+" will not run on Windows, and "Windows" will run only on Windows. If you choose a L&F that will not run, you will get the Java, or Metal, L&F.<p>In the section of the code where the L&F is actually set, you will see several different ways to set it, as discussed above:<p><blockquote><pre>if (LOOKANDFEEL.equals("Metal")) { lookAndFeel = UIManager.getCrossPlatformLookAndFeelClassName(); // an alternative way to set the Metal L&F is to replace the // previous line with: // lookAndFeel = "javax.swing.plaf.metal.MetalLookAndFeel";</pre></blockquote><p>You can verify that both arguments work by commenting/un-commenting the two alternatives.<p>Here is a listing of the<a class="SourceLink" target="_blank" href="examples/LookAndFeelDemo.java"><code>LookAndFeelDemo</code></a> source file:<blockquote><pre>/* * LookAndFeelDemo.java is a Java SE 6 example that requires * one other file (TestTheme.java). */import javax.swing.*; import java.awt.*;import java.awt.event.*;import javax.swing.plaf.metal.*;public class LookAndFeelDemo implements ActionListener { private static String labelPrefix = "Number of button clicks: "; private int numClicks = 0; final JLabel label = new JLabel(labelPrefix + "0 "); // Specify the look and feel to use by defining the LOOKANDFEEL constant // Valid values are: null (use the default), "Metal", "System", "Motif", // and "GTK" final static String LOOKANDFEEL = "Metal"; // If you choose the Metal L&F, you can also choose a theme. // Specify the theme to use by defining the THEME constant // Valid values are: "DefaultMetal", "Ocean", and "Test" final static String THEME = "Test"; public Component createComponents() { JButton button = new JButton("I'm a Swing button!"); button.setMnemonic(KeyEvent.VK_I); button.addActionListener(this); label.setLabelFor(button); /* * An easy way to put space between a top-level container * and its contents is to put the contents in a JPanel * that has an "empty" border. */ JPanel pane = new JPanel(new GridLayout(0, 1)); pane.add(button); pane.add(label); pane.setBorder(BorderFactory.createEmptyBorder( 30, //top 30, //left 10, //bottom 30) //right ); return pane; } public void actionPerformed(ActionEvent e) { numClicks++; label.setText(labelPrefix + numClicks); } private static void initLookAndFeel() { String lookAndFeel = null; if (LOOKANDFEEL != null) { if (LOOKANDFEEL.equals("Metal")) { lookAndFeel = UIManager.getCrossPlatformLookAndFeelClassName(); // an alternative way to set the Metal L&F is to replace the // previous line with: // lookAndFeel = "javax.swing.plaf.metal.MetalLookAndFeel"; } else if (LOOKANDFEEL.equals("System")) { lookAndFeel = UIManager.getSystemLookAndFeelClassName(); } else if (LOOKANDFEEL.equals("Motif")) { lookAndFeel = "com.sun.java.swing.plaf.motif.MotifLookAndFeel"; } else if (LOOKANDFEEL.equals("GTK")) { lookAndFeel = "com.sun.java.swing.plaf.gtk.GTKLookAndFeel"; } else { System.err.println("Unexpected value of LOOKANDFEEL specified: " + LOOKANDFEEL); lookAndFeel = UIManager.getCrossPlatformLookAndFeelClassName(); } try { UIManager.setLookAndFeel(lookAndFeel); // If L&F = "Metal", set the theme if (LOOKANDFEEL.equals("Metal")) { if (THEME.equals("DefaultMetal")) MetalLookAndFeel.setCurrentTheme(new DefaultMetalTheme()); else if (THEME.equals("Ocean")) MetalLookAndFeel.setCurrentTheme(new OceanTheme()); else MetalLookAndFeel.setCurrentTheme(new TestTheme()); UIManager.setLookAndFeel(new MetalLookAndFeel()); } } catch (ClassNotFoundException e) { System.err.println("Couldn't find class for specified look and feel:" + lookAndFeel); System.err.println("Did you include the L&F library in the class path?"); System.err.println("Using the default look and feel."); } catch (UnsupportedLookAndFeelException e) { System.err.println("Can't use the specified look and feel (" + lookAndFeel + ") on this platform."); System.err.println("Using the default look and feel."); } catch (Exception e) { System.err.println("Couldn't get specified look and feel (" + lookAndFeel + "), for some reason."); System.err.println("Using the default look and feel."); e.printStackTrace(); } } } /** * Create the GUI and show it. For thread safety, * this method should be invoked from the * event dispatch thread. */ private static void createAndShowGUI() { //Set the look and feel. initLookAndFeel(); //Make sure we have nice window decorations. JFrame.setDefaultLookAndFeelDecorated(true); //Create and set up the window. JFrame frame = new JFrame("SwingApplication"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); LookAndFeelDemo app = new LookAndFeelDemo(); Component contents = app.createComponents(); frame.getContentPane().add(contents, BorderLayout.CENTER); //Display the window. frame.pack(); frame.setVisible(true); } public static void main(String[] args) { //Schedule a job for the event dispatch thread: //creating and showing this application's GUI. javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); }}</pre></blockquote></blockquote><h3><a name="themes">Themes</a></h3><blockquote>Themes were introduced as a way of easily changing the colors and fonts of the cross-platform Java (Metal) Look and Feel. In the sample program, <code>LookAndfeelDemo.java</code>, listed above, you can change the theme of the Metal L&F by setting the <code>THEME</code> constant on line 23 to one of three values:<ul><li><code>DefaultMetal</code></li><li><code>Ocean</code></li><li><code>Test</code></li></ul><code>Ocean</code>, which is a bit softer than the pure Metal look, has been the default theme for the Metal (Java) L&F since Java SE 5.Despite its name, <code>DefaultMetal</code> is not the default theme for Metal (it was before Java SE 5, which explains it's name). The <code>Test</code> theme is a theme defined in <code>TestTheme.java</code>, which you must compile with <code>LookAndfeelDemo.java</code>. As it is written, <code>TestTheme.java</code> sets the three primary colors (with somewhat bizarre results). You can modify <code>TestTheme.java</code> to test any colors you like.<p>The section of code where the theme is set is found beginning on line 92 of <code>LookAndfeelDemo.java</code>. Note that you must be using the Metal L&F to set a theme.<blockquote><pre> if (LOOKANDFEEL.equals("Metal")) { if (THEME.equals("DefaultMetal")) MetalLookAndFeel.setCurrentTheme(new DefaultMetalTheme()); else if (THEME.equals("Ocean")) MetalLookAndFeel.setCurrentTheme(new OceanTheme()); else MetalLookAndFeel.setCurrentTheme(new TestTheme()); UIManager.setLookAndFeel(new MetalLookAndFeel()); } </pre></blockquote></blockquote><h3><a name="swingset2">The SwingSet2 Demonstration Program</a></h3><blockquote>In your JDK 6 installation, there is a <code>demo\jfc</code> folder that contains a demonstration program called <code>SwingSet2</code>. This program has a graphically rich GUI and allows you to change the Look and Feel from the menu. Further, if you are using the Java (Metal) Look and Feel, you can choose a variety of different themes. The files for the various themes (for example, <code>RubyTheme.java</code>) are found in the <code>SwingSet2\src</code> folder.<p>This is the "Ocean" theme, which is the default for the cross-platform Java (Metal) Look and Feel:<p><p><center><IMG SRC="../../figures/uiswing/lookandfeel/OceanLAF.gif" WIDTH="665" HEIGHT="607" ALIGN="BOTTOM" ALT=""></center></p><p><p>This is the "Steel" theme, which was the original theme for the cross-platform Java (Metal) Look and Feel:<p><p><center><IMG SRC="../../figures/uiswing/lookandfeel/MetalLAF.gif" WIDTH="665" HEIGHT="607" ALIGN="BOTTOM" ALT=""></center></p><p>To run the <code>SwingSet2</code> demo program on a system that has the JDK installed, use this command:<blockquote><pre>java -jar SwingSet2.jar</pre></blockquote>This will give you the default theme of Ocean. <p> To get the metal L&F, run this:<blockquote><pre>java -Dswing.metalTheme=steel -jar SwingSet2.jar</pre></blockquote><p>It is also possible to run the SwingSet2 demo program with Java Web Start:<blockquote><pre><a class="OutsideLink" target="_blank" href="http://java.sun.com/products/jfc/jws/SwingSet2.jnlp">http://java.sun.com/products/jfc/jws/SwingSet2.jnlp</a></pre></blockquote> </blockquote> <div class=NavBit> <a target=_top href=index.html>« Previous</a> • <a target=_top href=../TOC.html>Trail</a> • <a target=_top href=../dnd/index.html>Next »</a> </div> </div> <div id=Footer><div id=TagNotes> Problems with the examples? Try <a target="_blank" href=../../information/run-examples.html>Compiling and Running the Examples: FAQs</a>. <br> Complaints? Compliments? Suggestions? <a target="_blank" href="http://developer.sun.com/contact/tutorial_feedback.jsp">Give us your feedback</a>.<br><br> <a target="_blank" href="../../information/copyright.html">Copyright</a> 1995-2006 Sun Microsystems, Inc. All rights reserved. <span id=Download></span></div> </div> <div class=PrintHeaders> <b>Previous page:</b> Modifying the Look and Feel <br><b>Next page:</b> Drag and Drop and Data Transfer </div> </body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -