本文介绍了获取字体、大小、粗体等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法找到有关访问 Windows 字体或预定义字体和大小的内容.所以对于我的 java 程序,我有一个带有字体、大小和颜色的 JComboBox.问题是我需要预先输入字体、大小和颜色.我怎样才能获得预定义的字体、颜色和大小?到目前为止,这就是我对这种字体的看法,但它的方式不正确.

 if (font.equals("Arial")) {如果(大小.等于(8")){设置大小 = 8;} else if (size.equals("10")) {设置大小 = 10;} else if (size.equals("12")) {设置大小 = 12;}如果(颜色.等于(黑色")){setColor = Color.BLACK;} else if (color.equals("Blue")) {setColor = Color.BLUE;} else if (color.equals("Red")) {setColor = Color.red;}Font font = new Font("Arial", setAttribute, setSize);Writer.setFont(字体);Writer.setForeground(setColor);
解决方案
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();String[] fonts = ge.getAvailableFontFamilyNames();

可以在运行时设置大小和样式.

例如

import java.awt.*;导入 javax.swing.*;公共类 ShowFonts {公共静态无效主(字符串 [] args){SwingUtilities.invokeLater(() -> {GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();String[] fonts = ge.getAvailableFontFamilyNames();JComboBox fontChooser = new JComboBox(fonts);fontChooser.setRenderer(new FontCellRenderer());JOptionPane.showMessageDialog(null, fontChooser);});}}类 FontCellRenderer 扩展 DefaultListCellRenderer {公共组件 getListCellRendererComponent(JList 列表,对象值,整数索引,布尔值被选中,布尔 cellHasFocus) {JLabel 标签 = (JLabel)super.getListCellRendererComponent(列表、值、索引、isSelected、cellHasFocus);Font font = new Font(value.toString(), Font.PLAIN, 20);label.setFont(字体);退货标签;}}

JavaDoc

GraphicsEnvironment.getAvailableFontFamilyNames() 部分说明..

返回一个数组,其中包含此 GraphicsEnvironment 中针对默认语言环境本地化的所有字体系列的名称,如 Locale.getDefault()..

另见:

getAllFonts()...

I'm having trouble finding stuff on accessing Windows fonts or predefined fonts, and sizes. So for my java program I have a JComboBox with fonts, sizes, and colors. The problem is that I need to pre-Enter the fonts, sizes and colors. How would I be able to get the predefined fonts, colors, and sizes? So far this is what I have for this font but its not in the correct way.

               if (font.equals("Arial")) {

                if (size.equals("8")) {
                    setSize = 8;
                } else if (size.equals("10")) {
                    setSize = 10;
                } else if (size.equals("12")) {
                    setSize = 12;
                }

                if (color.equals("Black")) {
                    setColor = Color.BLACK;
                } else if (color.equals("Blue")) {
                    setColor = Color.BLUE;
                } else if (color.equals("Red")) {
                    setColor = Color.red;
                }

                Font font = new Font("Arial", setAttribute, setSize);
                Writer.setFont(font);
                Writer.setForeground(setColor);
解决方案
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fonts = ge.getAvailableFontFamilyNames();

The sizes and styles can be set at run-time.

E.G.

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

public class ShowFonts {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
            String[] fonts = ge.getAvailableFontFamilyNames();
            JComboBox fontChooser = new JComboBox(fonts);
            fontChooser.setRenderer(new FontCellRenderer());
            JOptionPane.showMessageDialog(null, fontChooser);
        });
    }
}

class FontCellRenderer extends DefaultListCellRenderer {

    public Component getListCellRendererComponent(
            JList list,
            Object value,
            int index,
            boolean isSelected,
            boolean cellHasFocus) {
        JLabel label = (JLabel)super.getListCellRendererComponent(
                list,value,index,isSelected,cellHasFocus);
        Font font = new Font(value.toString(), Font.PLAIN, 20);
        label.setFont(font);
        return label;
    }
}

JavaDoc

The JDoc for GraphicsEnvironment.getAvailableFontFamilyNames() state in part..

这篇关于获取字体、大小、粗体等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-28 14:30
查看更多