📄 qqutils.java
字号:
package com.zlf.qqclient.utils;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.Properties;
import java.util.TimeZone;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.plaf.FontUIResource;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
/**
* 类 <code>QQUtils</code> 包含一些常的方法,如:设置程序的外观,设置程序的字体等.
*/
public class QQUtils {
/**
* 常量WINDOWS用于指定Look&Feel为window.
*/
public final static String WINDOWS = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
/**
* 常量MOTIF用于指定Look&Feel为MOTIF.
*/
public final static String MOTIF = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
/**
* 常量METAL用于指定Look&Feel为METAL.
*/
public final static String METAL = "javax.swing.plaf.metal.MetalLookAndFeel";
/**
* 常量BORLAND用于指定Look&Feel为BORLAND.
*/
public final static String BORLAND = "com.borland.plaf.borland.BorlandLookAndFeel";
/**
* 常量LIROSOFT用于指定Look&Feel为LIROSOFT.
*/
public final static String LIROSOFT = "com.birosoft.liquid.LiquidLookAndFeel";
/**
* 常量GTK用于指定Look&Feel为GTKLookAndFeel.
*/
public final static String GTK = "com.sun.java.swing.plaf.gtk.GTKLookAndFeel";
/**
* 用于存储客户端的设置信息.
*/
public static Properties properties = null;
/**
* 仅可内部构造.
*/
private QQUtils() {
}
// 1.=================== 操作设置信息的相关方法.start ============================
/**
* 获取配置信息.使用静态块加载默认配置信息.
*/
static {
properties = new Properties();
try {
properties.load(new FileInputStream("./config/clientConfig.properties"));
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "获取配置信息出错!");
System.exit(1);// 非正常退出程序.
}
}
/**
* 加载QQ对应设置信息.
*
* @param dir
* QQ号码.
*/
public static void setProperties(String dir) {
File qqFile = new File("./log/" + dir);
if (qqFile.exists()) {// 如果对应的文件夹存在,则表示用户登录过,加载.
try {
properties.clear();
properties.load(new FileInputStream("./log/" + dir
+ "/selfset.properties"));
} catch (IOException e) {
}
} else {// 如果用户第一次登录,产生相应的文件夹.
qqFile.mkdir();
saveProperties(dir);
}
}
public static void saveProperties(String dir) {
try {
properties.store(new FileOutputStream("./log/" + dir+"/selfset.properties"), null);// selfset.properties
} catch (Exception ex) {
}
}
/**
* 保存设置.
*
*
* QQ号码.
*/
public static void saveProperties() {
try {
properties.store(new FileOutputStream("./config/clientConfig.properties"), null);// selfset.properties
} catch (Exception ex) {
}
}
/**
* 通过设置信息,产生Attr属性对象.
*
* @return 属性对象.
*/
public static MutableAttributeSet getFontAttr() {
MutableAttributeSet attr = new SimpleAttributeSet();
StyleConstants.setFontFamily(attr, properties.getProperty("DefaultFont"));// 字体名称.
StyleConstants.setFontSize(attr, Integer.parseInt(properties
.getProperty("DefaultFontSize")));// 字体大小.
if (properties.getProperty("bold").equals("yes"))
StyleConstants.setBold(attr, true);// 粗体.
if (properties.getProperty("Italic").equals("yes"))
StyleConstants.setItalic(attr, true);// 斜体.
if (properties.getProperty("UnderLine").equals("yes"))
StyleConstants.setUnderline(attr, true);// 下划线.
String[] foreground = QQUtils.properties.getProperty("foreground")
.split(",");
int red = Integer.parseInt(foreground[0]);
int green = Integer.parseInt(foreground[1]);
int blue = Integer.parseInt(foreground[2]);
Color foreColor = new Color(red, green, blue);
StyleConstants.setForeground(attr, foreColor);// 设置颜色.
return attr;
}
public static void initLookAndFeel() {
setLAF(null, null);// 读取配置文件中的主题.
setSelfFont(null);// 读取配置文件中的字体.
}
/**
* 设置主题.如传空则读配置文件中的配置.
*
* @param defaultLF
* 外观名.
* @param c
* 作用组件.
*/
public static void setLAF(String defaultLF, Component c) {
if (defaultLF == null || defaultLF.equals("")) {
defaultLF = properties.getProperty("DefaultLF");// 主题名.
}
if (defaultLF != null) {
if (defaultLF.equalsIgnoreCase("Borland")) {
setUI(QQUtils.BORLAND, c);
} else if (defaultLF.equalsIgnoreCase("Window")) {
setUI(QQUtils.WINDOWS, c);
} else if (defaultLF.equalsIgnoreCase("Motif")) {
setUI(QQUtils.MOTIF, c);
} else if (defaultLF.equalsIgnoreCase("Metal")) {
setUI(QQUtils.METAL, c);
} else if (defaultLF.equalsIgnoreCase("Lirosoft")) {
setUI(QQUtils.LIROSOFT, c);
} else if (defaultLF.equalsIgnoreCase("Gtk")) {
setUI(QQUtils.GTK, c);
} else if (defaultLF.equalsIgnoreCase("Nimrod")) {
/*
* try { UIManager .setLookAndFeel(new
* com.nilo.plaf.nimrod.NimRODLookAndFeel()); if (c != null) {
* SwingUtilities.updateComponentTreeUI(c); } } catch
* (UnsupportedLookAndFeelException e) {
* Log.getInstance(QQUtils.class).log(e); }
*/
}
}
}
/**
* 用默认 METAL 设置全局显示外观.
*/
public static void setUI() {
setUI(QQUtils.METAL, null);
}
/**
* 用指定Look&Feel 设置全局显示外观.
*
* @param lf
* 外观名称.
*/
public static void setUI(String lf) {
setUI(lf, null);
}
/**
* 用默认 METAL 设置指定对象显示外观.
*
* @param c
* 作用组件.
*/
public static void setUI(Component c) {
setUI(QQUtils.METAL, c);
}
/**
* 用指定Look&Feel 设置指定对象显示外观.
*
* @param lf
* 外观名称.
* @param c
* 作用组件.
*/
public static void setUI(String lf, Component c) {
try {
UIManager.setLookAndFeel(lf);
// 运行时动态改变的话要加下面的语句;
if (c != null) {
SwingUtilities.updateComponentTreeUI(c);
}
} catch (Exception e) {
}
}
/**
* 设置字体.如传空则读配置文件中的配置.
*
* @param defaultFont
* 字体名.
*/
public static void setSelfFont(String defaultFont) {
if (defaultFont == null || defaultFont.equals("")) {
defaultFont = properties.getProperty("DefaultFont");// 字体
}
if (defaultFont != null) {
setGlobalFont(defaultFont);
} else {
setGlobalFont("宋体");
}
}
/**
* 设置特定字体.
*
* @param defaultFont
* 字体名.
* @param target
* 作用对象.
*/
public static void setGlobalFont(String defaultFont, String target) {
Font fontRes = new Font(defaultFont, Font.PLAIN, 12);
UIManager.put(target, new FontUIResource(fontRes));
}
/**
* 用默认 宋体,Font.PLAIN,12 设置全局字体.
*/
public static void setGlobalFont() {
setGlobalFont("字体");
}
/**
* 用指定字体,Font.PLAIN,12 设置全局字体.
*
* @param fontName
* 字体名称.
*/
public static void setGlobalFont(String fontName) {
Font fontRes = new Font(fontName, Font.PLAIN, 12);
for (Enumeration keys = UIManager.getDefaults().keys(); keys
.hasMoreElements();) {
Object key = keys.nextElement();
Object value = UIManager.get(key);
if (value instanceof FontUIResource) {
// FontUIResource fuir = (FontUIResource) value;
// Font fontRes = new Font(font, fuir.getStyle(),
// fuir.getSize());
UIManager.put(key, new FontUIResource(fontRes));
}
}
}
/**
* 返回当前日期
* @return 当前日期
*/
public static String getCurDate(){
SimpleDateFormat sdf = new SimpleDateFormat(
"yyyy-MM-dd hh:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
return sdf.format(new Date());
}
// ========================== 字体设置的相关方法.end ============================
// 4.========================== 获取资源的相关方法.start ============================
/**
* 返回images中的图片.
*
* @param icon
* 图片在image下的完整路径.
*
* @return images下图标.
*/
public static Icon getImgIcon(String icon) {
Icon myIcon = null;
myIcon = new ImageIcon("./image/" + icon);
return myIcon;
}
/**
* 返回images/total中的图片.
*
* @param icon
* 图片在image/total下的完整路径.
*
* @return images/total下图标.
*/
public static Icon getTotalIcon(String icon) {
return getImgIcon("total/" + icon);
}
/**
* 返回images/newface中的图片.
*
* @param icon
* 图片在images/newface下的完整路径.
*
* @return images/newface下图标.
*/
public static Icon getNewfaceIcon(String icon) {
return getImgIcon("newface/" + icon);
}
/**
* 返回images/face中的图片.
*
* @param icon
* 图片在images/face下的完整路径.
*
* @return images/face下图标.
*/
public static Icon getFaceIcon(String icon) {
return getImgIcon("face/" + icon);
}
/**
* 返回images/font中的图片.
*
* @param icon
* 图片在images/font下的完整路径.
*
* @return icon 图标.
*/
public static Icon getFontIcon(String icon) {
return getImgIcon("total/font/" + icon);
}
/**
* 返回窗体图标.
*
* @param frame
* 窗体.
*
* @param icon
* 图标.
*
* @return imgIcon 图片.
*/
public static Image getIconImage(JFrame frame, String icon) {
Image imgIcon = null;
Toolkit tookit = null;
tookit = frame.getToolkit();
// 获取图片.
imgIcon = tookit.getImage("./image/" + icon);
return imgIcon;
}
// ========================== 获取资源的相关方法.end ============================
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -