⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lookandfeelmanager.java

📁 实现了一个由时间服务器
💻 JAVA
字号:
package client.tools;

import java.awt.Component;
import java.io.PrintStream;
import java.util.HashMap;

import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.plaf.FontUIResource;

/**
 * 用来管理LookAndFeel的类
 * 
 * @author elegate
 */
public class LookAndFeelManager
{
    /**
         * @author elegate
         */
    public enum LookAndFeelType
    {
	/**
         * Windows look and feel
         */
	WINDOWS,
	/**
         * Motif look and feel
         */
	CDE_MOTIF,
	/**
         * Metal look and feel,that is java's default look and feel
         */
	METAL;

	/**
         * @param type
         *                LookAndFeelType
         * @return A string describe the type
         * @see LookAndFeelType
         */
	public static String toString(LookAndFeelType type)
	{
	    if (type == LookAndFeelType.WINDOWS)
	    {
		return "Windows";
	    }
	    else if (type == LookAndFeelType.CDE_MOTIF)
	    {
		return "CDE/Motif";
	    }
	    else
		return "Metal";
	}
    }

    /**
         * 保存系统支持的LookAndFeel信息
         */
    private static HashMap<String, UIManager.LookAndFeelInfo> LookAndFeelMap = new HashMap<String, UIManager.LookAndFeelInfo>();

    static
    {
	UIManager.LookAndFeelInfo[] lookAndFeelInfos = UIManager
		.getInstalledLookAndFeels();
	for (int i = 0; i < lookAndFeelInfos.length; i++)
	{
	    LookAndFeelMap.put(lookAndFeelInfos[i].getName(),
		    lookAndFeelInfos[i]);
	}

    }

    /**
         * private 构造函数,用户不能实例化此类的对象
         */
    private LookAndFeelManager()
    {

    }

    public static void changeToSystemLookAndFeel(Component c)
    {
	try
	{
	    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
	    if (c != null)
		SwingUtilities.updateComponentTreeUI(c);
	}
	catch (Exception e)
	{
	    e.printStackTrace();
	}
    }

    /**
         * 改变给定组件的LookAndFeel
         * 
         * @param type
         *                LookAndFeel的种类
         * @param c
         *                要改变感观的组件
         */
    public static void changeLookAndFeel(LookAndFeelType type, Component c)
    {
	try
	{
	    UIManager.LookAndFeelInfo info = LookAndFeelMap.get(LookAndFeelType
		    .toString(type));
	    UIManager.setLookAndFeel(info.getClassName());
	    if (c != null)
		SwingUtilities.updateComponentTreeUI(c);
	}
	catch (Exception e)
	{
	    e.printStackTrace();
	}
    }

    /**
         * 改变给定组件的感观
         * 
         * @param lookAndFeelName
         *                感官的名称
         * @param c
         *                要改变感观的组件
         */
    public static void changeLookAndFeel(String lookAndFeelName, Component c)
    {
	try
	{
	    UIManager.LookAndFeelInfo info = LookAndFeelMap
		    .get(lookAndFeelName);
	    UIManager.setLookAndFeel(info.getClassName());
	    if (c != null)
		SwingUtilities.updateComponentTreeUI(c);
	}
	catch (Exception e)
	{
	    e.printStackTrace();
	}
    }

    /**
         * 显示所有感官的名称
         * 
         * @param out
         *                输出流,指定输出
         */
    public static void listAllInstalledLookAndFeelNames(PrintStream out)
    {
	for (String e : LookAndFeelMap.keySet())
	{
	    out.println(e + "->" + LookAndFeelMap.get(e));
	}
	out.println(UIManager.getSystemLookAndFeelClassName());
    }

    /**
         * 设置界面上所有元素使用的字体
         * 
         * @param font
         *                FontUIResource的对象
         * @see FontUIResource
         */

    public static void setUIFont(FontUIResource font)
    {
	java.util.Enumeration keys = UIManager.getDefaults().keys();
	while (keys.hasMoreElements())
	{
	    Object key = keys.nextElement();
	    Object value = UIManager.get(key);
	    if (value instanceof javax.swing.plaf.FontUIResource)
	    {
		// System.out.println("key="+key+",value="+value);
		UIManager.put(key, font);
	    }
	}
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -