【问题标题】:JTextField crops text when using custom TrueType font. How to make it display text normally?JTextField 在使用自定义 TrueType 字体时裁剪文本。如何让它正常显示文字?
【发布时间】:2013-12-17 02:57:55
【问题描述】:

所以,问题肯定出在字体上。问题是如何使文本字段完全显示文本。示例:

import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;

class Example extends JFrame{
    public Example(){
        setLayout(new BorderLayout());
        Font myFont = null;
        try {
            URL link = new URL("http://rghost.ru/download/50564305/e6efddd74f598b86f7ac704cab72e430a490bc7f/digital-7.ttf");
            ReadableByteChannel rbc = Channels.newChannel(link.openStream());
            File font_file = new File("font.ttf");
            if(!font_file.exists())
                font_file.createNewFile();

            FileOutputStream fos = new FileOutputStream(font_file);
            fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
            myFont = Font.createFont(Font.TRUETYPE_FONT, font_file);
        } catch (FontFormatException e) {
            e.printStackTrace(); 
        } catch (IOException e) {
            e.printStackTrace();
        }
        JTextField myField = new JTextField("sample text");
        myField.setFont(myFont.deriveFont(32.0f));
        add(myField, BorderLayout.NORTH);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(300, 400);
        setVisible(true);
    }

    public static void main(String[] args){
        new Example();
    }
}

字体为Digital 7:dafont.com
问题解决了 解决方案是将字体here(或其他类似地方)转换为PFM并像这样使用它:

Font myFont = Font.createFont(Font.TYPE1_FONT, new File("res/my_font.pfm"));

【问题讨论】:

  • @AndrewVershinin 编辑上面的文字并提出问题。
  • 如果字体在网络上可用并且不是太大,请发布一个SSCCE 以热链接到它。我怀疑问题出在Font 而不是Java。
  • @AndrewThompson 字体为link
  • @AndrewThompson 来了。
  • 我的第一个直觉是字体没有提供适当的信息以使其正确呈现

标签: java swing fonts jtextfield truetype


【解决方案1】:

我怀疑问题出在Font 而不是Java。

foundhttp://onlinefontconverter.com/font?id=p1 的一个页面声称(有时)修复了无效字体,但找不到一个只会报告有效性的页面。尝试先运行它。

【讨论】:

【解决方案2】:
myField.setPreferredSize(new Dimension(40,40)); // where the first argument is the width of the component,
                                                //  and 2nd is the height(that's what you need )

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-06
  • 1970-01-01
  • 2011-01-04
相关资源
最近更新 更多