Java JTextField使用自定义TrueType字体时会裁剪文本。如何使其正常显示文本?
所以,问题肯定出在字体上。问题是如何使文本字段完全显示文本。例如:Java JTextField使用自定义TrueType字体时会裁剪文本。如何使其正常显示文本?,java,swing,fonts,jtextfield,truetype,Java,Swing,Fonts,Jtextfield,Truetype,所以,问题肯定出在字体上。问题是如何使文本字段完全显示文本。例如: 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.ReadableByte
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();
}
}
字体为数字7:
问题解决
解决方案是将字体或其他类似位置转换为PFM,并按如下方式使用:
Font myFont = Font.createFont(Font.TYPE1_FONT, new File("res/my_font.pfm"));
我怀疑问题在于字体而不是Java
我在一个页面上声明有时会修复无效字体,但找不到一个简单地报告有效性的页面。试着先把它运行一遍。@AndrewVershinin编辑上面的文本,并将其作为一个问题。如果网络上有可用的字体&字体不太大,请发布一个指向它的热链接。我怀疑问题在于字体而不是Java。@AndrewThompson字体是@AndrewThompson,我的第一个直觉是字体没有提供适当的信息,使其呈现为Yes。听起来很合理。感谢您发布链接。+1,有趣的链接,但是
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 )