zoukankan      html  css  js  c++  java
  • Swing获取字符串的宽度和高度

    import java.awt.Color;
    import java.awt.Font;
    import java.awt.font.FontRenderContext;
    import java.awt.geom.AffineTransform;
    
    public class SwingUtil {
        private static AffineTransform atf = new AffineTransform();
    
        private static FontRenderContext frc = new FontRenderContext(atf, true,
                true);
    
        public static int getStringHeight(String str, Font font) {
            if (str == null || str.isEmpty() || font == null) {
                return 0;
            }
            return (int) font.getStringBounds(str, frc).getWidth();
    
        }
    
        public static int getStringWidth(String str, Font font) {
            if (str == null || str.isEmpty() || font == null) {
                return 0;
            }
            return (int) font.getStringBounds(str, frc).getWidth();
        }
    
        /**
         * 将形如“#FFFFFF”的颜色转换成Color
         * 
         * @param hex
         * @return
         */
        public static Color getColorFromHex(String hex) {
            if (hex == null || hex.length() != 7) {
                try {
                    throw new Exception("不能转换这种类型的颜色");
                } catch (Exception e) {
                    e.printStackTrace();
                    return null;
                }
            }
            int r = Integer.valueOf(hex.substring(1, 3), 16);
            int g = Integer.valueOf(hex.substring(3, 5), 16);
            int b = Integer.valueOf(hex.substring(5), 16);
            return new Color(r, g, b);
        }
    
    }
  • 相关阅读:
    MVC,KVO,KVC的简单认识
    Objective-C之集合对象
    Objective-C之词典对象
    Objective-C之数组对象
    Objective-C关键字static
    IOS做天气预报
    同步和异步GET,POST请求
    iOS开发常用的开源库和示例
    KVC KVO KVB
    iOS中的 沙盒文件夹 (数据的写入和读取,归档和反归档)
  • 原文地址:https://www.cnblogs.com/happyPawpaw/p/3513918.html
Copyright © 2011-2022 走看看