@Override 注解的问题

发布于 2024-10-08 00:46:16 字数 1056 浏览 2 评论 0原文

我正在制作一个类似于 Banko 小程序的 Java 应用程序。当我点击“public void init()”方法时,我进展顺利。当我完成后,除此之外的所有内容都已编译完毕。它告诉我添加 @Override 注释。我尝试过,但每当我这样做时,无论我把它放在哪里,编译器都会失败并出现以下错误:

找不到符号

符号:类覆盖

位置:类aBomb.Bomb

我不知道什么可能会阻止应用程序正确执行。 :| 如果您在我下面编写的代码中发现您认为应该更改的内容,请告诉我。我对 Java 还比较陌生:( 代码:

public void init() {
    BorderLayout border = new BorderLayout();
    setLayout(border);

    JPanel top = new JPanel();
    JLabel moneyLabel = new JLabel("Money : $");
    moneyField = new JTextField("", 8);
    moneyField.setEditable(false);
    JLabel foundLabel = new JLabel("Found: ");
    foundField = new JTextField("", 8);
    foundField.setEditable(false);

    restart = new JButton("Restart");
    restart.addActionListener(this);
    top.add(moneyLabel);
    top.add(moneyField);
    top.add(foundLabel);
    top.add(foundField);
    top.add(restart);
    add(top, BorderLayout.NORTH);

    board = new Board(this, ROW_COUNT, COLUMN_COUNT, BOMB_COUNT);
    add(board, BorderLayout.CENTER);
    setup();
    setVisible(true);
}

I am making a Java application similar to that of the Banko applet. I was coming along just fine when I hit the "public void init()" method. When I was finished, everything compiled except for that. It told me to add an @Override annotation. I tried that, but whenever I do, regardless of where I put it, the compiler fails with the following error:

cannot find symbol

symbol: class Overrides

location: class aBomb.Bomb

I have no idea what could be preventing the application from executing properly. :|
If you find something in the code I have written below that you think should be changed, please tell me. I'm relatively new to Java :(
Code:

public void init() {
    BorderLayout border = new BorderLayout();
    setLayout(border);

    JPanel top = new JPanel();
    JLabel moneyLabel = new JLabel("Money : $");
    moneyField = new JTextField("", 8);
    moneyField.setEditable(false);
    JLabel foundLabel = new JLabel("Found: ");
    foundField = new JTextField("", 8);
    foundField.setEditable(false);

    restart = new JButton("Restart");
    restart.addActionListener(this);
    top.add(moneyLabel);
    top.add(moneyField);
    top.add(foundLabel);
    top.add(foundField);
    top.add(restart);
    add(top, BorderLayout.NORTH);

    board = new Board(this, ROW_COUNT, COLUMN_COUNT, BOMB_COUNT);
    add(board, BorderLayout.CENTER);
    setup();
    setVisible(true);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

高速公鹿 2024-10-15 00:46:16

首先,如果您至少包含类定义(“公共类...”部分),那将真正有帮助。

我猜您有一个名为 aBomb 的类,它扩展了来自 Applet:

public class aBomb extends Applet {
//...
    // Here's the init method; the @Override goes
    // immediately before the declaration.
    @Override
    public void init() {
//...
};

错误消息看起来好像您将 @Override 拼写为 @Overrides

First of all, it would really help if you'd included at least the class definition (the "public class..." part.)

I'm guessing that you have a class named aBomb which extends from Applet:

public class aBomb extends Applet {
//...
    // Here's the init method; the @Override goes
    // immediately before the declaration.
    @Override
    public void init() {
//...
};

The error message looks as if you misspelled @Override as @Overrides.

云之铃。 2024-10-15 00:46:16

您尝试使用的注释类是java.lang.Override。这是默认导入的。

检查以下内容:

  • 注释是 @Override,但错误消息表明您将其拼写为 @Overrides。检查源代码是否存在拼写/输入错误。

  • 您正在使用 Java 5.0 或更高版本。

  • 您尚未使用 -source-target 编译开关来针对旧版本的 Java 进行编译。

  • 您没有使用 -bootclasspath (或其他内容)来针对非标准类库进行编译。

The annotation class you are trying to use is java.lang.Override. This is imported by default.

Check the following:

  • The annotation is @Override but the error message says that you spelled it as @Overrides. Check your source code for a spelling / typing error.

  • You are using Java 5.0 or later.

  • You have not used the -source or -target compilation switches to compile for an older version of Java.

  • You are not using -bootclasspath (or whatever) to compile against a non-standard class library.

雅心素梦 2024-10-15 00:46:16

该注释称为 @Override,而不是 @Overrides。它位于重写方法之前,例如:

@Override
public void init() {
   ...

The annotation is called @Override, not @Overrides. It goes in front of the overriding method, like:

@Override
public void init() {
   ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文