자바 스윙 은 로 컬 글꼴 이 아 닌 글꼴 을 사용 합 니 다.

2514 단어
package reyo;

import java.awt.Container;
import java.awt.Font;
import java.awt.FontFormatException;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.UIManager;

public class ReYoFont extends JFrame {

	private static final long serialVersionUID = 8844899652191928500L;
	private Font definedFont = null;

	public ReYoFont() {
		super("ReYoFontTest");
		this.init();
	}

	private void init() {
		//       
		Container container = getContentPane();
		setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));
		for (float align = 0.0f; align <= 1.0f; align += 0.25f) {
			JButton button = new JButton("X align = " + align);
			button.setFont(getDefinedFont());
			button.setAlignmentX(align);
			container.add(button);
			container.add(Box.createVerticalGlue());
		}
		//       
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(300, 100, 600, 400);
		setVisible(true);
	}

	public Font getDefinedFont() {
		if (definedFont == null) {
			InputStream is = null;
			BufferedInputStream bis = null;
			try {
				is = ReYoFont.class.getResourceAsStream("/reyo.ttf");
				bis = new BufferedInputStream(is);
				// createFont                    Font。<br>
				//   Font    1,    PLAIN,           InputStream
				definedFont = Font.createFont(Font.TRUETYPE_FONT, bis);
				//     Font        ,           Font  。
				definedFont = definedFont.deriveFont(30);
			} catch (FontFormatException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					if (null != bis) {
						bis.close();
					}
					if (null != is) {
						is.close();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return definedFont;
	}

	public static void main(String args[]) {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e) {
		}
		new ReYoFont();
	}
}

좋은 웹페이지 즐겨찾기