导入字体到GUI [英] Importing Font to GUI

查看:156
本文介绍了导入字体到GUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想改变我的GUI字体除了基本的5秋千似乎来。如何导入字体和我的code实际使用它们?

I am trying to change the font for my GUI besides the basic 5 that swing seems to come with. How to import fonts and actually use them in my code?

推荐答案

有通常超过5默认提供的,但它们从系统中更改系统。这个回答将检查两个现有的字体,以及如何加载和放大器;注册新字体。

There are usually more than 5 available by default, but they change from system to system. This answer examines both the existing fonts, as well as how to load & register new fonts.

它使用可用的简明Airacobra字型从下载免费字体(所获得热链接URL)。一种字体在您的应用程序的罐子。也通过URL访问。

It uses the 'Airacobra Condensed' font available from Download Free Fonts (obtained by hot-link URL). A font that is in the Jar of your app. is also accessible by URL.

import java.awt.*;
import javax.swing.*;
import java.net.URL;

class LoadFont {
    public static void main(String[] args) throws Exception {
        // This font is < 35Kb.
        URL fontUrl = new URL("http://www.webpagepublicity.com/" +
            "free-fonts/a/Airacobra%20Condensed.ttf");
        Font font = Font.createFont(Font.TRUETYPE_FONT, fontUrl.openStream());
        GraphicsEnvironment ge = 
            GraphicsEnvironment.getLocalGraphicsEnvironment();
        ge.registerFont(font);
        JList fonts = new JList( ge.getAvailableFontFamilyNames() );
        JOptionPane.showMessageDialog(null, new JScrollPane(fonts));
    }
}


OK,这很有趣,但是这是什么字体实际上是什么样子?


OK, that was fun, but what does this font actually look like?

import java.awt.*;
import javax.swing.*;
import java.net.URL;

class DisplayFont {
    public static void main(String[] args) throws Exception {
        URL fontUrl = new URL("http://www.webpagepublicity.com/" +
            "free-fonts/a/Airacobra%20Condensed.ttf");
        Font font = Font.createFont(Font.TRUETYPE_FONT, fontUrl.openStream());
        font = font.deriveFont(Font.PLAIN,20);
        GraphicsEnvironment ge =
            GraphicsEnvironment.getLocalGraphicsEnvironment();
        ge.registerFont(font);

        JLabel l = new JLabel(
            "The quick brown fox jumped over the lazy dog. 0123456789");
        l.setFont(font);
        JOptionPane.showMessageDialog(null, l);
    }
}

这篇关于导入字体到GUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆