java - Java 中的字体问题

标签 java fonts truetype

这是我的问题。

我正在将 ttf 字体加载到 Java 中,我知道它正在读取它,因为它不会给我错误,但它不会真正读取该字体,因为它只是给我一条线。

这是我的 Screen 类使用该字体的代码:

public class BLANKMainMenuScreen extends JPanel {

private JButton playButton;

Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
int screenWidth = d.width;
int screenHeight = d.height;

public ImageIcon greenButton = new ImageIcon("resources/menubuttons/GreenButton.png");
public ImageIcon redButton = new ImageIcon("resources/menubuttons/RedButton.png");

public BLANKMainMenuScreen() throws FontFormatException, IOException {
    setLayout(new GroupLayout(this));
    playButton = new JButton("Play!");
    playButton.setIcon(greenButton);
    playButton.setBorderPainted(false);
    playButton.setContentAreaFilled(false);
    playButton.setFocusPainted(false);
    playButton.setActionCommand("/mainMenuPlayButton");
    playButton.setFont(Font.createFont(Font.TRUETYPE_FONT, new File("resources/font/cubic.ttf")));
    playButton.setBounds(screenWidth / 2 - 100, screenHeight / 3 - 50, 200, 100);
    playButton.setRolloverEnabled(true);
    playButton.setRolloverIcon(redButton);
    playButton.setHorizontalTextPosition(SwingConstants.CENTER);
    add(playButton);
}

public void addActionListener(JButton b, ActionListener listener) {
    b.addActionListener(listener);
}

public void removeActionListener(JButton b, ActionListener listener) {
    b.removeActionListener(listener);
}

@Override
public Dimension getPreferredSize() {
    return Toolkit.getDefaultToolkit().getScreenSize();
}

我不知道为什么它只会向我显示一行内容,但确实如此。

最佳答案

Font#createFont返回 1pt 的字体...您应该将结果分配给 Font 变量并使用 Font#deriveFont指定尺寸和样式

关于java - Java 中的字体问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25983787/

相关文章:

html - 在域之间进行自定义@font-face 缓存吗?

java - JTextField 在使用自定义 TrueType 字体时裁剪文本。如何让它正常显示文字?

html - 如何在我的应用程序中离线使用下载的 Google 字体?

java - 按位取反给出意想不到的结果

java - FirebaseRecyclerAdapter 的 FirebaseDatabase 查询 : How to query some data basing on some values from other childs.

java - 如何使用 JSQL Parser 从查询中检索表名?

c++ - 如何定义 tesseract 用于识别(而不是训练)的字体类型?

java - 设置数据源时出错

css - 圆圈上的 SVG 文本 - 更改文本方向

跨平台安全使用的 SVG 字体