【问题标题】:Syntax errors, but i see no errors语法错误,但我没有看到任何错误
【发布时间】:2014-08-23 02:01:06
【问题描述】:

在 Eclipse 中它给了我一堆语法错误,但我看到代码非常好???第 14、23、27 行有语法错误,但就像我说的,它们对我来说似乎很好? :(

package me.slappy826.Everlost.ttf;

import java.awt.Font;
import java.io.InputStream;

import org.lwjgl.opengl.GL11;
import org.newdawn.slick.*;

public class SlickTTF
{
    InputStream fontFile = org.newdawn.slick.util.ResourceLoader.getResourceAsStream("assets/everlost/Ubuntu-LI.ttf");
    private static TrueTypeFont font;

    private Font awtFont = null;

    try 
    {
        awtFont = Font.createFont(Font.TRUETYPE_FONT, fontFile);
        awtFont = awtFont.deriveFont(20f);
        font = new TrueTypeFont(awtFont, true);
    } catch(Exception e) {}


    public static void drawString(String s, int i, int j, Color color)
    {
        font.drawString(i, j, s, color);
    }
}

【问题讨论】:

  • 第 14、23 和 27 行在哪里?
  • 第 14 行是私有字体 awtFont = null; |第 23 行是 public static void drawString(String s, int i, int j, Color color) |第 27 行是最后一行。
  • 您应该在方法/构造函数内部或静态块内部声明您的 try/catch 块,如 here 解释的那样。
  • 报告错误时,始终将 EXACT 和 COMPLETE 错误消息复制/粘贴到您的问题中。

标签: java syntax


【解决方案1】:

您不能在类级别范围内声明 try-catch,只需将其放在方法中即可。下面是一个示例,其中包含一个可能的构造函数来初始化您的成员:

public SlickTTF(){
    try{
        awtFont = Font.createFont(Font.TRUETYPE_FONT, fontFile);
        awtFont = awtFont.deriveFont(20f);
        font = new TrueTypeFont(awtFont, true);
    }catch(Exception e){}
}

现在您可以将 SlickTTF 对象创建为SlickTTF example = new SlickTTF(),它的类属性将正确初始化

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-19
    相关资源
    最近更新 更多