1. java中如何修改JDialog、JLable等漢字的顯示字體
boundary.setFont(new Font("TimesRoman", Font.PLAIN, 13");
如果手頭有ttf字體文件還可以通過載入字體文件自定義字體,如下:
Font font = Font.createFont(Font.TRUETYPE_FONT, new File(
"src/Font/minidy.ttf"));
AWT定義的五種邏輯字體:
* SansSerif
* Serif
* Monospaced
* Dialog
* DialogInput
這些字體被映射到客戶機上的實際字體如在Windows下,SansSerif被映射到Arial。
Font helvb14 = new Font("Helvetica", Font.BOLD, 14);
第二個參數是字體風格
Font.PLAIN
Font.BOLD
Font.ITALIC
Font.BOLD+Font.ITALIC
提供字體名的位置也可以給出邏輯字體的名稱,如
Font sansbold14 = new Font("SansSerif", Font.BOLD, 14);
boundary.setFont(sansbold14);
如果還不懂網路hi我就好!
2. java中如何讀取ttf字體文件,生成Font數組
1.讀取
import java.awt.Font;
import java.io.File;
import java.io.FileInputStream;
public class Loadfont
{
public static Font loadFont(String fontFileName, float fontSize) //第一個參數是外部字體名,第二個是字體大小
{
try
{
File file = new File(fontFileName);
FileInputStream aixing = new FileInputStream(file);
Font dynamicFont = Font.createFont(Font.TRUETYPE_FONT, aixing);
Font dynamicFontPt = dynamicFont.deriveFont(fontSize);
aixing.close();
return dynamicFontPt;
}
catch(Exception e)//異常處理
{
e.printStackTrace();
return new java.awt.Font("宋體", Font.PLAIN, 14);
}
}
public static java.awt.Font Font(){
String root=System.getProperty("user.dir");//項目根目錄路徑
Font font = Loadfont.loadFont(root+"/data/PRISTINA.ttf", 18f);//調用
return font;//返回字體
}
public static java.awt.Font Font2(){
String root=System.getProperty("user.dir");//項目根目錄路徑
Font font = Loadfont.loadFont(root+"/data/XXXX.ttf", 18f);
return font;//返回字體
}
}
2.轉換:
public static String ascii2native(String ascii) {
int n = ascii.length() / 6;
StringBuilder sb = new StringBuilder(n);
for (int i = 0, j = 2; i < n; i++, j += 6) {
String code = ascii.substring(j, j + 4);
char ch = (char) Integer.parseInt(code, 16);
sb.append(ch);
}
return sb.toString();
}
3. java字體設置
寫圖形控制項,如果不指定字體,將會用JRE在操作系統環境下的默認字體。
有源碼的情況下,如果去更改JRE的字體,顯然不夠直接也會破壞統一的環境。
通常是在源碼中從顯示控制項比如Label或JLabel的setFont指定一個統一的字體,比如"宋體"
然後把宋體的字體文件simsun.ttf作為資源文件打在jar中.,或者放在外面作為運行時調用。
這樣能統一在各平台的字體效果。
默認情況JRE只提供拉丁字母集的跨平台字體,沒有包括中文。
4. java相關,關於使用itext導出pdf後,中文字體設置的問題。
可以直接調用字體 寫一個
BaseFont bf=BaseFont.createFont("字體",BaseFont.CP1250, BaseFont.EMBEDDED);
字體就設置,你的那個字體路徑
5. java 如何讀取otf字體急求高手解答。。
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
public static void main(String[] args) throws Exception {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
document.open();
BaseFont bf = BaseFont.createFont("esl_gothic_shavian.otf", "Cp1252", BaseFont.EMBEDDED);
System.err.println(bf.getClass().getName());
Font font = new Font(bf, 12);
document.add(new Paragraph("abced"));
document.add(new Paragraph("this is a test", font));
document.close();
}
}
這段程序貌似是把pdf設置成otf字體輸出,改一下就好!
這程序絕對經得起考研!從國外教程網站找到d!
把esl_gothic_shavian.otf換成你的字體
6. 如何獲取android系統的字體名稱
在java環境中有一個專門的獲取ttf文件的頭信息的Font類
Font f = Font.createFont(Font.TRUETYPE_FONT, new FileInputStream("seguisym.ttf"));
String name = f.getName();
System.out.println(name);
但是在android環境下,我們無法直接用到該類去解析TTF文件,下面我將附上代碼來解析ttf文件
TTFParser.Java
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
/**
* TTF Font file parser
* <p>
* sample:
* <code><pre>
* File fs = new File("C:\\Windows\\Fonts");
* File[] files = fs.listFiles(new FilenameFilter() {
* public boolean accept(File dir, String name) {
* if (name.endsWith("ttf")){ return true;}
* return false;
* }
* });
* for (File file : files) {
* TTFParser parser = new TTFParser();
* parser.parse(file.getAbsolutePath());
* System.out.println("font name: " + parser.getFontName());
* }
* </pre></code>
* <p/>
* Copyright: Copyright (c) 12-8-6 下午3:51
* <p/>
* Version: 1.0
* <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 (fontProperties.containsKey(FULL_FONT_NAME)) {
return fontProperties.get(FULL_FONT_NAME);
} else if (fontProperties.containsKey(FAMILY_NAME)) {
return fontProperties.get(FAMILY_NAME);
} else {
return null;
}
}
/**
* 獲取ttf屬性
* @param nameID 屬性標記,見靜態變數
* @return 屬性值
*/
public String getFontPropertie(int nameID) {
if (fontProperties.containsKey(nameID)) {
return fontProperties.get(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 {
fontProperties.clear();
RandomAccessFile f = null;
try {
f = new RandomAccessFile(fileName, "r");
parseInner(f);
} finally {
try {
f.close();
}catch (Exception e) {
//ignore;
}
}
}
private void parseInner(RandomAccessFile randomAccessFile) throws IOException {
int majorVersion = randomAccessFile.readShort();
int minorVersion = randomAccessFile.readShort();
int numOfTables = randomAccessFile.readShort();
if (majorVersion != 1 || minorVersion != 0) { return; }
//jump to TableDirectory struct
randomAccessFile.seek(12);
boolean found = false;
byte[] buff = new byte[4];
TbleDirectory tableDirectory = new TableDirectory();
for (int i = 0; i < numOfTables; i++) {
randomAccessFile.read(buff);
tableDirectory.name = new String(buff);
tableDirectory.checkSum = randomAccessFile.readInt();
tableDirectory.offset = randomAccessFile.readInt();
tableDirectory.length = randomAccessFile.readInt();
if ("name".equalsIgnoreCase(tableDirectory.name)) {
found = true;
break;
}else if (tableDirectory.name == null || tableDirectory.name.length() == 0) {
break;
}
}
// not found table of name
if (!found) { return; }
randomAccessFile.seek(tableDirectory.offset);
NameTableHeader nameTableHeader = new NameTableHeader();
nameTableHeader.fSelector = randomAccessFile.readShort();
nameTableHeader.nRCount = randomAccessFile.readShort();
nameTableHeader.storageOffset = randomAccessFile.readShort();
NameRecord nameRecord = new NameRecord();
for (int i = 0; i < nameTableHeader.nRCount; i++) {
nameRecord.platformID = randomAccessFile.readShort();
nameRecord.encodingID = randomAccessFile.readShort();
nameRecord.languageID = randomAccessFile.readShort();
nameRecord.nameID = randomAccessFile.readShort();
nameRecord.stringLength = randomAccessFile.readShort();
nameRecord.stringOffset = randomAccessFile.readShort();
long pos = randomAccessFile.getFilePointer();
byte[] bf = new byte[nameRecord.stringLength];
long vpos = tableDirectory.offset + nameRecord.stringOffset + nameTableHeader.storageOffset;
randomAccessFile.seek(vpos);
randomAccessFile.read(bf);
String temp = new String(bf, Charset.forName("utf-16"));
fontProperties.put(nameRecord.nameID, temp);
randomAccessFile.seek(pos);
}
}
@Override
public String toString() {
return fontProperties.toString();
}
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
}
}