如何在两个平台上运行java程序?

nig*_*ird 1 java swing

我在eclipse和平台上开发了软件是ubuntu(linux).我在程序中使用了古吉拉特字体并完成了它并且它在ubuntu中运行得非常好但是在使用JRE在Windows 7中运行时它无法正常工作.文本将无法正确显示,因为它在ubuntu中使用.我该怎么办?

在此输入图像描述

我也尝试过使用java -Dfileencoding = utf-8但它无法正常工作.当我在Windows中打开从Ubuntu中挑选的java sorce文件时,文本正常工作并显示.所以请告诉我正确的工作方式.

Rob*_*dob 6

如果要在应用程序中使用自定义字体(在所有操作系统上都不可用),则需要在.jar文件中包含字体文件(otf,ttf等),然后可以使用应用程序中的字体通过此处描述的方法:

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")));
Run Code Online (Sandbox Code Playgroud)

如果您不确定如何从.jar中提取文件,这里是我之前在SO上分享的方法;

/**
*  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;
    }
}
Run Code Online (Sandbox Code Playgroud)

  • +1,类似问题/答案,代码示例:http://stackoverflow.com/questions/5652344/how-can-i-use-a-custom-font-in-java (4认同)