2013-08-05 26 views
1

我在eclipse中開發過軟件,平臺是ubuntu(linux)。我在程序中使用了gujarati字體並完成了它,它在ubuntu中運行得很好,但是在使用JRE運行在Windows 7中時,它無法正常工作。文本將不會像ubuntu中使用的那樣正常顯示。我做什麼?如何在兩個平臺上運行java程序?

enter image description here

我已經用java -Dfileencoding = UTF-8也試過,但它不會工作正常。當我打開從Windows中的Ubuntu中挑選的java sorce文件時,文本正常工作並顯示。 所以請告訴我正確地做這件事的方法。

+0

爲什麼不使用正常字體?這不是一個標準的Windows字體,所以它當然不會正確渲染。 –

+0

@JesusRamos古吉拉特語是一種語言。你不知道他使用哪種字體。 – Sebastian

+0

@Sebastian那麼這個語言必須支持字體,因爲它看起來像是需要特殊字符。 –

回答

0

您需要將字體安裝到Windows中 - 或更改應用程序中的字體。您可以通過將其包含在安裝過程中來完成此操作。

+0

從unicode安裝古吉拉特字體然後它也不工作.... –

6

如果您想在您的應用程序中使用自定義字體(在所有操作系統上都不可用),您需要在.jar文件中包含字體文件(otf,ttf等)然後可以通過該方法使用的字體在應用程序中描述如下:從(Here thanks to commenter

http://download.oracle.com/javase/6/docs/api/java/awt/Font.html#createFont%28int,%20java.io.File%29

示例代碼;

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); 
ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, new File("A.ttf"))); 

如果不確定如何從一個.jar提取您的文件,這裏是我在這之前共享的方法;

/** 
* This method is responsible for extracting resource files from within the .jar to the temporary directory. 
* @param filePath The filepath relative to the 'Resources/' directory within the .jar from which to extract the file. 
* @return A file object to the extracted file 
**/ 
public File extract(String filePath) 
{ 
    try 
    { 
     File f = File.createTempFile(filePath, null); 
     FileOutputStream resourceOS = new FileOutputStream(f); 
     byte[] byteArray = new byte[1024]; 
     int i; 
     InputStream classIS = getClass().getClassLoader().getResourceAsStream("Resources/"+filePath); 
//While the input stream has bytes 
     while ((i = classIS.read(byteArray)) > 0) 
     { 
//Write the bytes to the output stream 
      resourceOS.write(byteArray, 0, i); 
     } 
//Close streams to prevent errors 
     classIS.close(); 
     resourceOS.close(); 
     return f; 
    } 
    catch (Exception e) 
    { 
     System.out.println("An error has occurred while extracting a resource. This may mean the program is missing functionality, please contact the developer.\nError Description:\n"+e.getMessage()); 
     return null; 
    } 
} 
+4

+1,這裏的simllar問題/答案,代碼示例:http://stackoverflow.com/questions/5652344/how-can-i-use-a -custom-font-in-java –

+0

包括在我的答案中,謝謝。 – Robadob

相關問題