Java 皮肤详解

前言:由于我们很多时候对系统的默认皮肤有点不满意那么我们就需要使用我们自定义或者第三方所设计的皮肤了,在这里对这方面做一个整理。如果有不合理的地方或者不足的地方请留言!!

官网文档:Modifying the Look and Feel

一、简单应用

首先我们不说原理,简单介绍一下应用!!!

查看已有观感器:

UIManager.LookAndFeelInfo[] info = UIManager.getInstalledLookAndFeels();  
for (LookAndFeelInfo lookAndFeelInfo : info) {  
    System.out.println("name:" + lookAndFeelInfo.getName());  
    System.out.println("class:" + lookAndFeelInfo.getClassName());  
}  

我的电脑查询出来的结果是:

name:Metal
class:javax.swing.plaf.metal.MetalLookAndFeel
name:Nimbus
class:javax.swing.plaf.nimbus.NimbusLookAndFeel
name:CDE/Motif
class:com.sun.java.swing.plaf.motif.MotifLookAndFeel
name:Windows
class:com.sun.java.swing.plaf.windows.WindowsLookAndFeel
name:Windows Classic
class:com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel

更改默认观感器设置:

  位于 ${java.home}/lib/swing.properties 中的文件swing.properties 存在且包含键 swing.defaultlaf,则将其值用作默认外观类名称。在这个文件中,将属性swing.defaultlaf设置为所希望的观感器类名。
注意:Metal观感器位于javax.swing包中。其他的观感器包位于com.sun.java包中。采用这种方式开启观感器时必须重新启动程序。Swing程序只在启动时读取该配置文件。

动态更改观感器:

方法很简单:调用UIManager.setLookAndFeel方法,并提供所想要的观感器类名,然后调用静态方法SwingUtilities.updateComponentTreeUI来刷新全部的组件集。这里需要向这个方法提供一个组件,并由此找到其他的所有组件。

基础知识:

javax.swing.JComponent 该类是除顶层容器外所有 Swing 组件的基类。要使用继承自 JComponent 的组件,必须将该组件置于一个根为顶层 Swing 容器的包含层次结构(containment hierarchy)中。顶层 Swing 容器(如 JFrame、JDialog 和 JApplet)是专门为其他 Swing 组件提供绘制自身场所的组件。
javax.swing.plaf.ComponentUI  用于 Swing 可插入外观架构中所有 UI 委托对象的基本类。Swing 组件的 UI 委托对象负责实现依赖于外观的那些组件的某些方面。为了委托那些可能随所安装的外观而改变的操作(绘制、布局计算等等),JComponent 类要调用来自此类的方法。客户端程序不应在此类上直接调用这些方法。

L&F(look and feel):在本篇文档中解释为观感。

图形用户界面(Graphical User Interface,简称 GUI,又称图形用户接口)

二、细节剖析

  The architecture of Swing is designed so that you may change the "look and feel" (L&F) of your application's GUI (see A Swing Architecture Overview). "Look" refers to the appearance of GUI widgets (more formally, JComponents) and "feel" refers to the way the widgets behave.

  Swing的体系结构已经被设计好,因此我们可以在应用程序序的GUI上改变"look and feel" (L&F)。“Look”取决于GUI小部件的外观(更正式的说是JComponents),“feel”取决于这些小部件的行为方式。

  Swing's architecture enables multiple L&Fs by separating every component into two distinct classes: aJComponent subclass and a correspondingComponentUI subclass. For example, everyJList instance has a concrete implementation ofListUI (ListUI extends ComponentUI). TheComponentUI subclass is referred to by various names in Swing's documentation—"the UI," "component UI," "UI delegate," and "look and feel delegate" are all used to identify theComponentUI subclass.

  Swing的体系结构可以由多个观感,其实现是通过将每一个component分割成两个不同的类:JComponent 的子类和与其相对应的ComponentUI子类。例如,每一个JList实例都有一个具体的实现ListUI (ListUI继承自ComponentUI)。ComponentUI的子类通常在Swing文档中的命名格式为--"the UI," "component UI," "UI delegate," and "look and feel delegate"。

  Most developers never need to interact with the UI delegate directly. For the most part, the UI delegate is used internally by theJComponentsubclass for crucial functionality, with cover methods provided by theJComponent subclass for all access to the UI delegate. For example, all painting inJComponent subclasses is delegated to the UI delegate. By delegating painting,the 'look' can vary depending upon the L&F.

  大多数开发者不需要直接与UI代理进行交互。大部分时候,UI代理在关键部位被JComponent  的子类所调用,JComponent的子类提供了访问UI代理的覆写的方法。例如,所有JComponent子类关于绘制的操作是调用UI代理来完成的。通过代理来绘制,这样“look”取决于不同的L&F。

  It is the responsibility of each L&F to provide a concrete implementation for each of theComponentUI subclassesdefined by Swing. For example, the Java Look and Feel creates an instance of MetalTabbedPaneUI to provide the L&F for JTabbedPane.The actual creation of the UI delegate is handled by Swing for you—for the most part you never need to interact directly with the UI delegate.

  每一个L&F都有责任为每一个ComponentUI的子类提供一个具体的实现。例如,Java 观感器为JTabbedPane的观感创建一个MetalTabbedPaneUI实例。UI代理的实际为你管理Swing组件--大部分我们唔需要与UI代理进行交互。

Available Look and Feels

JRE为我们提供了如下的观感

  1. CrossPlatformLookAndFeel—也叫作“Metal”,这种观感在任何平台下都是一样的。这部分在JavaAPI的javax.swing.plaf.metal中,而且也是默认的观感器。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. 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.
  3. Synth—通过XML文件创建自己的观感设置。the basis for creating your own look and feel with an XML file.
  4. Multiplexing—一种通过UI方法代理实现同一时间实现多个观感的方法。 。a way to have the UI methods delegate to a number of different look and feel implementations at the same time.

  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.

  对于Linux和Solaris系统,如果GTK+ 2.2或者之后版本已经安装那么系统的观感是"GTK+",否则是"Motif"。对于Windows,系统的观感是"Windows"。GTK+, Motif, and Windows观感有Sun公司提供,被加载在Java SDK与JRE中,虽然他们不是JavaAPI的一部分。

  Apple provides its own JVM which includes their proprietary L&F.苹果提供它自己的包含其特有观感的JVM。

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

总的来说,当你使用SystemLookAndFeel(观感器),下面这些你将会看到:

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*

Supplied by the system vendor.

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:

你不会在API中看到系统的观感。GTK+, Motif, and Windows需要哦的包在JavaSDK中:

com.sun.java.swing.plaf.gtk.GTKLookAndFeel
com.sun.java.swing.plaf.motif.MotifLookAndFeel
com.sun.java.swing.plaf.windows.WindowsLookAndFeel

Note that the path includes java, and not javax.注意路径是在java下而不是javax。

Note: 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.
注意:GTK+观感只运行在UNIX或者Linux系统下,并且要求已经安装了GTK2.2或者之后的版本;Windows观感只运行在Windows系统下;想Java(Metal)观感,Motif观感可以运行在任何平台。
  All of Sun's L&Fs have a great deal of commonality. This commonality is defined in the Basic look and feel in the API (javax.swing.plaf.basic). The Motif and Windows L&Fs are each built by extending the UI delegate classes in javax.swing.plaf.basic (a custom L&F can be built by doing the same thing). The "Basic" L&F is not used without being extended.

  所有Sun提供的观感都有很大的共性。这个共性被定义在基本的观感API中(javax.swing.plaf.basic)。Motif与Windows观感都建立在(javax.swing.plaf.basic)UI代理的拓展上(自定义的皮肤也可以做同样的事情)。基础的皮肤如果没有被拓展就无法使用。

In the API you will see four L&F packages(在API中你将看到四个观感包):

  • javax.swing.plaf.basic—basic UI Delegates to be extended when creating a custom L&F.创建自定义观感时的可拓展的基本UI代理,
  • 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.这个Java观感也被称为跨平台的观感(“Metal”就是对这个观感的命名),这个观感默认的主题是“Ocean”,因此这个观感通常也被成为Ocean观感。
  • 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 thatprovides audio cues on top of the Windows look and feel.This is a way of creating a handicapped-accessible look and feel.一个可以复用的观感,允许UI方法通过代理在同一时间实现多个观感。可以用来增强特定观感的行为,例如在Windows观感上提供一个提供音频观感。只是一种创建handicapped-accessible观感的方式。
  • javax.swing.plaf.synth—an easily configured L&F using XML files (discussed in the next section of this lesson)。一个简单使用XML文件配置的观感(在下一个部分讲解)

  You aren't limited to the L&Fs supplied with the 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:

  你没必要只限于Java平台提供的观感。你可以使用项目中的任意在项目Class路径下的观感。第三方的观感经常会以jar包的形式提供给我们,我们载运行的时候将他们加入到我们项目的Class路径下。如下:

java -classpath .;C:\java\lafdir\customlaf.jar YourSwingApplication

  Once an external L&F is in your program's class path, your program can use it just like any of the L&Fs shipped with the Java platform.
  一旦外部的观感在程序的Class路径下,你的程序可以像使用Java平台提供的观感一样进行使用了。

Programatically Setting the Look and Feel

Note: 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&F regardless of what L&F you've requested.This can happen inadvertently when a static field references a Swing class, which causes the L&F to be loaded. If no L&F has yet been specified, 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.

注意:如果你要设置观感,你应该把它作为你的应用程序的第一步。否则你初始化观感是有风险的。这可能会发生不经意间的情况下,当一个静态字段引用一个Swing类这会影响观感的加载。如果没有观感被指定,JRE默认的观感就会被加载。JRE默认的观感是Java观感,对于苹果是Apple观感等等。

  The L&F that Swing components use is specified by way of the UIManager class in the javax.swing package. Whenever a Swing component is created,the component asks the UI manager for the UI delegate that implements the component's L&F.For example,each JLabel 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.

  Swing组件所使用的观感是由javax.swing包下的 UIManager 类指定的。无论Swing组件什么时候被创建,组件会请求实现观感的UI代理来进行UI管理。例如,每一个JLabel构造器为UI代理对象在UI manager中查找恰当的label。然后会使用UI代理对象来实现所有组件的绘制和事件的绑定。

  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:

  通过编程来指定观感,我们需要调用UIManager.setLookAndFeel()方法并提供所需要观感的类名。例如,在下面代码片段中使用了跨平台的Java观感:

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

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 (ClassNotFoundException | InstantiationException
			| IllegalAccessException | UnsupportedLookAndFeelException e) {
		// handle exception
	}
}

You can also use the actual class name of a Look and Feel as the argument to UIManager.setLookAndFeel(). For example,
你也可以将观感的实际类名作为UIManager.setLookAndFeel()方法的参数传进去。例如:

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

或者:

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

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.
你不必局限于前面的论点。你可以指定任意在你程序Class路径下的观感名称。

Specifying the Look and Feel: Command Line

You can specify the L&F at the command line by using the -D flag to set the swing.defaultlaf property. For example:

你可以在命令行下使用 -D 参数指定swing.defaultlaf的默认观感配置。例如:

java -Dswing.defaultlaf=com.sun.java.swing.plaf.gtk.GTKLookAndFeel MyApp

java -Dswing.defaultlaf=com.sun.java.swing.plaf.windows.WindowsLookAndFeel MyApp

Specifying the Look and Feel: swing.properties File

  Yet another way to specify the current L&F is to use the swing.properties file to set the swing.defaultlaf property. This file, which you may need to create, is located in the lib directory of Sun's Java release (other vendors of Java may use a different location). For example, if you're using the Java interpreter in javaHomeDirectory\bin, then the swing.properties file (if it exists) is in javaHomeDirectory\lib. Here is an example of the contents of a swing.properties file:

  另一种指定当前观感的方式是使用swing.properties文件并设置swing.defaultlaf值。这个文件需要我们自己创建,并且这个文件位于JRE路径下的lib文件夹下。例如,如果你使用Java解释器在javaHomeDirectory\bin,那么swing.properties文件也应该在javaHomeDirectory\lib这个文件夹中。下面是一个swing.properties文件内容的一个例子:

# Swing properties
swing.defaultlaf=com.sun.java.swing.plaf.windows.WindowsLookAndFeel

How the UI Manager Chooses the Look and Feel

Here are the look-and-feel determination steps that occur when the UI manager needs to set a L&F:

当一个UI manager需要设置观感时的步骤:

  • 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.设置观感之前,UI manager尝试创建一个指定的观感器实例,如果成功,所有的组件将使用这个观感。
  • If the program hasn't successfully specified a L&F, then the UI manager uses the L&F specified by the swing.defaultlaf property. If the property is specified in both the swing.properties file and on the command line, the command-line definition takes precedence.如果程序没有成功指定观感,UI manager将使用swing.defaultlaf值所指定的观感。如果命令行与配置文件swing.properties中都指定了swing.defaultlaf的值,命令行优先。
  • 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.如果上述都没有指定合适的观感,JRE将会使用Java观感。其它供应商,例如苹果,将会使用其默认的观感。

Changing the Look and Feel After Startup

  You can change the L&F with setLookAndFeel even after the program's GUI is visible. To make existing components reflect the new L&F, invoke the SwingUtilities updateComponentTreeUI method once per top-level container. Then you might wish to resize each top-level container to reflect the new sizes of its contained components. For example:

  我们可以使用setLookAndFeel方法来改变观感,即使GUI程序已经运行。让已经显示地组件应用新的观感,需要在每个顶级容器中借助SwingUtilities.updateComponentTreeUI方法。然后,您可能希望调整每一个顶级容器,以反映其所包含的组件的新大小。例如:

UIManager.setLookAndFeel(lnfName);
SwingUtilities.updateComponentTreeUI(frame);
frame.pack();

An Example

  In the following example, LookAndFeelDemo.java, 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.

  在下面的示例中(LookAndFeelDemo.java),你可以实验不同的观感,程序创建了一个简单的只有按钮和标签的GUI。每次你点击按钮,标签就会增加。

  You can change the L&F by changing the LOOKANDFEEL constant on line 18. The comments on the preceding lines tell you what values are acceptable:

  你可以改变观感通过改变第18行LOOKANDFEEL常量的值。前面的行的注释告诉你什么值是可以接受的:

// 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";

  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.
  这里常量设置为“MOtif”这是一个可以运行在任何平台的观感(就像默认的“Metal”)。"GTK+"无法运行在Windows系统上,“Windows”只能运行在Windows系统上。如果你选择的观感无法再当前系统运行,你将会得到Java观感或者Metal观感。

  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:
  在下面观感设置的代码中,你会看到几种不同的方法来设置它,正如上面所讨论的:

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";

You can verify that both arguments work by commenting/un-commenting the two alternatives.

你可以通过评论/不评论这两个备选方案来验证这两个论点的运行。

Here is a listing of the LookAndFeelDemo source file(下面是LookAndFeelDemo源文件):

package lookandfeel;

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);

        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();
            }
        }
    }

    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();
            }
        });
    }
}

Themes

  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,LookAndfeelDemo.java, listed above, you can change the theme of the Metal L&F by setting theTHEME constant on line 23 to one of three values:
  主题是一种可以简单地改变跨平台的观感的颜色和字体的一种方式。在(LookAndfeelDemo.java)这个上面展示的简单程序中,你可以通过改变第23行THEME这个常量的值(下面三种之一)来改变Metal观感的主题:

  • DefaultMetal
  • Ocean
  • Test

  Ocean, 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, DefaultMetal is not the default theme for Metal (it was before Java SE 5, which explains its name). The Test theme is a theme defined in TestTheme.java, which you must compile with LookAndfeelDemo.java.As it is written, TestTheme.java sets the three primary colors (with somewhat bizarre results). You can modify TestTheme.java to test any colors you like.
  Ocean(这是一个有点比纯金属外观)从Java SE 5 开始就是Metal(Java)观感默认的主题。尽管名字中含有Default,但是DefaultMetal不是Metal默认的主题(这个名字是指其在Java SE 5之前定义的)。Test主题定义在了TestTheme.java中,这个文件必须与LookAndfeelDemo.java文件一同编译。因为它是书面的,testtheme.java集三基色(有点奇怪的结果)。你可以修改testtheme.java测试任何你喜欢的颜色。

package lookandfeel;
 
import javax.swing.plaf.*;
import javax.swing.plaf.metal.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
 
/**
 * This class describes a theme using "primary" colors.
 * You can change the colors to anything else you want.
 *
 * 1.9 07/26/04
 */
public class TestTheme extends DefaultMetalTheme {
 
    public String getName() { return "Toms"; }
 
    private final ColorUIResource primary1 = new ColorUIResource(255, 255, 0);
    private final ColorUIResource primary2 = new ColorUIResource(0, 255, 255);
    private final ColorUIResource primary3 = new ColorUIResource(255, 0, 255);
 
    protected ColorUIResource getPrimary1() { return primary1; }
    protected ColorUIResource getPrimary2() { return primary2; }
    protected ColorUIResource getPrimary3() { return primary3; }
 
}

  The section of code where the theme is set is found beginning on line 92 of LookAndfeelDemo.java. Note that you must be using the Metal L&F to set a theme.

  设置主题代码所在的地方开始于LookAndfeelDemo.java第92行。注意:你必须使用Metal观感来设置主题。

The SwingSet2 Demonstration Program

  When you download the JDK and JavaFX Demos and Samples bundle and open it up, there is a demo\jfc folder that contains a demonstration program called SwingSet2. 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, RubyTheme.java) are found in the SwingSet2\src folder.

  当你下载JDK and JavaFX Demos and Samples bundle并打开它,里面会有一个demo\jfc文件夹并且包含一个演示程序名字为SwingSet2。这个程序包含了丰富的GUI界面并允许你通过菜单来更改程序的Look and Feel。此外,如果您使用的是java(金属)的外观和感觉,你可以选择各种不同的主题。所有主题文件都可以在SwingSet2\src中找到(例如RubyTheme.java)。

This is the "Ocean" theme, which is the default for the cross-platform Java (Metal) Look and Feel:

This is the "Steel" theme, which was the original theme for the cross-platform Java (Metal) Look and Feel:

To run the SwingSet2 demo program on a system that has the JDK installed, use this command:

在已经安装JDK的系统的命令行中运行SwingSet2示例程序:

java -jar SwingSet2.jar

This will give you the default theme of Ocean.这将会默认使用Ocean主题。
To get the metal L&F, run this:

java -Dswing.metalTheme=steel -jar SwingSet2.jar

SwingSet2示例程序的CSDN下载地址:

赞赏

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值