获取字体,大小,粗体,等等 [英] Getting fonts, sizes, bold,...etc

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

问题描述

我在查找Windows字体或预定义字体和大小时遇到​​问题。所以对于我的java程序,我有一个带有字体,大小和颜色的 JComboBox 。问题是我需要预先输入字体,大小和颜色。我将如何能够获得预定义的字体,颜色和大小?到目前为止,这是我对这种字体的,但它不正确的方式。

  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 = new Font(Arial,setAttribute,setSize);
Writer.setFont(font);
Writer.setForeground(setColor);


解决方案

  GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); 
String [] fonts = ge.getAvailableFontFamilyNames();

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



EG



  import java.awt。*; 
import javax.swing。*;
$ b class ShowFonts {

public static void main(String [] args){
SwingUtilities.invokeLater(new Runnable(){
public void run (){
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 {
$ b $ 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((String)value,Font.PLAIN,20);
label.setFont(font);
退货标签;




JavaDoc



用于 GraphicsEnvironment.getAvailableFontFamilyNames() 的状态部分..


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

/javase/8/docs/api/java/awt/GraphicsEnvironment.html#getAllFonts--rel =noreferrer> 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.*;

class ShowFonts {

    public static void main(String[] args) {
        SwingUtilities.invokeLater( new Runnable() {
            public void run() {
                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((String)value, Font.PLAIN, 20);
        label.setFont(font);
        return label;
    }
}

JavaDoc

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

Returns an array containing the names of all font families in this GraphicsEnvironment localized for the default locale, as returned by Locale.getDefault()..

See also:

getAllFonts()..

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

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