set Font For AWT Component - Java 2D Graphics

Java examples for 2D Graphics:Font

Description

set Font For AWT Component

Demo Code


//package com.java2s;
import java.awt.Component;
import java.awt.Container;

import java.awt.Font;

import java.util.Locale;

public class Main {
    public static void setFontForComponent(Component Comp, Font font) {
        if (font == null) {
            try {
                if (Locale.getDefault().toString().toLowerCase()
                        .contains("zh")) {
                    font = new Font("Microsoft YaHei", Font.PLAIN, 12);
                }//from   w  ww .j  a va2s  . co m
            } catch (Throwable e) {
            }
        }
        if (font == null) {
            return;
        }
        if (Comp != null) {
            try {
                Comp.setFont(font);
            } catch (Exception e) {
                return;
            }
        }
        if (Comp instanceof Container) {
            Component[] components = ((Container) Comp).getComponents();
            for (int i = 0; i < components.length; i++) {
                Component child = components[i];
                if (child != null) {
                    setFontForComponent(child, font);
                }
            }
        }
        return;
    }
}

Related Tutorials