Java自带了字体包,因此在Java中可以很方便地查询、获取、使用字体。字体包位于Java的awt包中,awt包中的Font类是实现Java字体功能的关键类,通过它可以创建、合成、渲染字体。如果在使用Java时发现没有字体包,可能是以下几个原因:1安装有问题:在安装Java时有选择要不要安装字体文件的选项。如果没有安装字体文件,就会看到类似于“javaawtFont - Font not found”这样的异常,需要重新安装Java并选择安装字体文件。2 *** 作系统问题:如果 *** 作系统的字体文件不完整或出现了问题,也有可能导致Java没有字体包。可以尝试更新或修复 *** 作系统的字体文件来解决问题。3缺少字体文件:有时某些字体文件在Java中是没有默认包含的,需要手动添加进去。这时可以在Java中使用FontcreateFont()方法加载字体文件,并通过GraphicsEnvironmentgetLocalGraphicsEnvironment()registerFont()方法向Java注册字体。总之,Java自带了字体包,如果在使用中遇到没有字体包的情况,多数是由于安装或 *** 作系统问题,需要进行一些调整修复。
首先获取存放字体的地址,然后获取地址下的字体文件的名字,然后将字体文字放进String数组里,对数组进行循环获取名字首字母并判断是否是b,(String里有个方法是获取首字母的,可以百度。),然后输出文件名就行了。
String typeface = "宋体";
if(egetSource == b1) {
typeface = "黑体";
}else if(egetSource == b2) {
typeface = "楷体";
} else {
typeface = "宋体";
}
tasetFont(new Font(typeface, FontPLAIN, 20));
这样试试看,如果还是不行的话就按下面看看系统自带字体是否有黑体和楷体。
//获取系统中可用的字体的名字GraphicsEnvironment e = GraphicsEnvironmentgetLocalGraphicsEnvironment();
String[] fontName = egetAvailableFontFamilyNames();
for(int i = 0; i<fontNamelength ; i++) {
Systemoutprintln(fontName[i]);
}
是 的,据我了解,java *** 作用的字体是系统字体。Java是一门面向对象的编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。Java语言作为静态面向对象编程语言的代表,极好地实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程。
class Test
{
public static void main(String[] args)
{
Properties pps=SystemgetProperties();
ppslist(Systemout);
Systemoutprintln("--------------------以上为JVM的所有属性值-------------");
Systemoutprint("系统默认的字符集为:");
String name=SystemgetProperty("sunjnuencoding");
Systemoutprint(name);
}
}
import javaappletApplet;
import javaawtFont;
import javaawtGraphics;
public class BeColor extends Applet{
public void paint(Graphics g){
gsetFont(new Font("黑体",FontBOLD,20));
gdrawString("I LOVE BEIJING", 0, 20);
gsetFont(new Font("SansSerif", FontITALIC, 20));
gdrawString("I LOVE BEIJING", 0, 50);
gsetFont(new Font("Monotype Corsiva", FontCENTER_BASELINE, 20));
gdrawString("I LOVE BEIJING", 0, 80);
}
}
在java环境中有一个专门的获取ttf文件的头信息的Font类
Font f = FontcreateFont(FontTRUETYPE_FONT, new FileInputStream("seguisymttf"));
String name = fgetName();
Systemoutprintln(name);
但是在android环境下,我们无法直接用到该类去解析TTF文件,下面我将附上代码来解析ttf文件
TTFParserJava
import javaioIOException;
import javaioRandomAccessFile;
import javaniocharsetCharset;
import javautilHashMap;
import javautilMap;
/
TTF Font file parser
<p>
sample:
<code><pre>
File fs = new File("C:\\Windows\\Fonts");
File[] files = fslistFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
if (nameendsWith("ttf")){ return true;}
return false;
}
});
for (File file : files) {
TTFParser parser = new TTFParser();
parserparse(filegetAbsolutePath());
Systemoutprintln("font name: " + parsergetFontName());
}
</pre></code>
<p/>
Copyright: Copyright (c) 12-8-6 下午3:51
<p/>
Version: 10
<p/>
/
public class TTFParser {
public static int COPYRIGHT = 0;
public static int FAMILY_NAME = 1;
public static int FONT_SUBFAMILY_NAME = 2;
public static int UNIQUE_FONT_IDENTIFIER = 3;
public static int FULL_FONT_NAME = 4;
public static int VERSION = 5;
public static int POSTSCRIPT_NAME = 6;
public static int TRADEMARK = 7;
public static int MANUFACTURER = 8;
public static int DESIGNER = 9;
public static int DESCRIPTION = 10;
public static int URL_VENDOR = 11;
public static int URL_DESIGNER = 12;
public static int LICENSE_DESCRIPTION = 13;
public static int LICENSE_INFO_URL = 14;
private Map<Integer, String> fontProperties = new HashMap<Integer, String>();
/
获取ttf font name
@return
/
public String getFontName() {
if (fontPropertiescontainsKey(FULL_FONT_NAME)) {
return fontPropertiesget(FULL_FONT_NAME);
} else if (fontPropertiescontainsKey(FAMILY_NAME)) {
return fontPropertiesget(FAMILY_NAME);
} else {
return null;
}
}
/
获取ttf属性
@param nameID 属性标记,见静态变量
@return 属性值
/
public String getFontPropertie(int nameID) {
if (fontPropertiescontainsKey(nameID)) {
return fontPropertiesget(nameID);
} else { return null; }
}
/
获取ttf属性集合
@return 属性集合(MAP)
/
public Map<Integer, String> getFontProperties() { return fontProperties; }
/
执行解析
@param fileName ttf文件名
@throws IOException
/
public void parse(String fileName) throws IOException {
fontPropertiesclear();
RandomAccessFile f = null;
try {
f = new RandomAccessFile(fileName, "r");
parseInner(f);
} finally {
try {
fclose();
}catch (Exception e) {
//ignore;
}
}
}
private void parseInner(RandomAccessFile randomAccessFile) throws IOException {
int majorVersion = randomAccessFilereadShort();
int minorVersion = randomAccessFilereadShort();
int numOfTables = randomAccessFilereadShort();
if (majorVersion != 1 || minorVersion != 0) { return; }
//jump to TableDirectory struct
randomAccessFileseek(12);
boolean found = false;
byte[] buff = new byte[4];
TbleDirectory tableDirectory = new TableDirectory();
for (int i = 0; i < numOfTables; i++) {
randomAccessFileread(buff);
tableDirectoryname = new String(buff);
tableDirectorycheckSum = randomAccessFilereadInt();
tableDirectoryoffset = randomAccessFilereadInt();
tableDirectorylength = randomAccessFilereadInt();
if ("name"equalsIgnoreCase(tableDirectoryname)) {
found = true;
break;
}else if (tableDirectoryname == null || tableDirectorynamelength() == 0) {
break;
}
}
// not found table of name
if (!found) { return; }
randomAccessFileseek(tableDirectoryoffset);
NameTableHeader nameTableHeader = new NameTableHeader();
nameTableHeaderfSelector = randomAccessFilereadShort();
nameTableHeadernRCount = randomAccessFilereadShort();
nameTableHeaderstorageOffset = randomAccessFilereadShort();
NameRecord nameRecord = new NameRecord();
for (int i = 0; i < nameTableHeadernRCount; i++) {
nameRecordplatformID = randomAccessFilereadShort();
nameRecordencodingID = randomAccessFilereadShort();
nameRecordlanguageID = randomAccessFilereadShort();
nameRecordnameID = randomAccessFilereadShort();
nameRecordstringLength = randomAccessFilereadShort();
nameRecordstringOffset = randomAccessFilereadShort();
long pos = randomAccessFilegetFilePointer();
byte[] bf = new byte[nameRecordstringLength];
long vpos = tableDirectoryoffset + nameRecordstringOffset + nameTableHeaderstorageOffset;
randomAccessFileseek(vpos);
randomAccessFileread(bf);
String temp = new String(bf, CharsetforName("utf-16"));
fontPropertiesput(nameRecordnameID, temp);
randomAccessFileseek(pos);
}
}
@Override
public String toString() {
return fontPropertiestoString();
}
private static class TableDirectory {
String name; //table name
int checkSum; //Check sum
int offset; //Offset from beginning of file
int length; //length of the table in bytes
}
private static class NameTableHeader {
int fSelector; //format selector Always 0
int nRCount; //Name Records count
int storageOffset; //Offset for strings storage,
}
private static class NameRecord {
int platformID;
int encodingID;
int languageID;
int nameID;
int stringLength;
int stringOffset; //from start of storage area
}
}
如果你有eclipse编辑工具,再获取Font对象后,在对象后打点,之后所有的方法工具帮你显示出来,没有工具的话,建议使用jdk帮助文档 ctrl键加F键 , 搜索Font关键字,找到相关类,底下有所有方法!你说的这个类对象没有这个方法
以上就是关于java没有字体包全部的内容,包括:java没有字体包、怎样获取输出系统中以b开头的字体(font)求JAVA大神!!!!急!!!、java中关于setFont()方法的问题等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)