【问题标题】:How do i get rid of this message Error: Main method not found in class我如何摆脱此消息错误:在类中找不到主要方法
【发布时间】:2014-10-25 17:31:14
【问题描述】:

我最近一直在开发这款 Java 游戏。我试图添加带有音频剪辑的音乐,但它不起作用,所以我取出了我的主代码并删除了主代码,因为我不再需要它了。所有代码与我实现音乐之前的代码相同,但现在弹出这条消息,不允许我运行我的游戏:

错误:在类中找不到主要方法 com.illuminationsco.GoNerdGo.Entities.Obstacles,请定义 主要方法为:public static void main(String[] args)

当我尝试放回 main 时,它根本不运行!

如果有人可以帮助我,那就太好了!

这是我的代码:

Bully bully = new Bully();
Nerd nerd = new Nerd();

static BufferedImage[] sprites;

public static float x, y, velX = 5.5f, velY = 5.5f;
public static int whatObstacle = 5;

public static int basketBall = 6;
public static int lava = 2;
public static int trash = 1;

public static boolean trashOpen = false;

public Rectangle getBounds() {
    return null;
}

public Obstacles() {
    int width = 100;
    int height = 100;
    int columns = 2;
    int rows = 2;

    BufferedImage spriteSheet = null;
    try {
        spriteSheet = ImageIO.read(new File(
                "res/Images/ObstacleSpriteSheet.png"));
    } catch (IOException e) {
        e.printStackTrace();
    }
    sprites = new BufferedImage[rows * columns];

    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {
            sprites[(i * columns) + j] = spriteSheet.getSubimage(i * width,
                    j * height, width, height);
        }
    }
}

public void tick() {
    if (whatObstacle >= basketBall && Game.gos == null) {
        x += velX;
        y += velY;
    }

    if (x <= 0 || x >= 800 - 35) {
        velX *= -1;
    }
    if (y <= 425 || y >= 600 - 35) {
        velY *= -1;
    }
}

public void render(Graphics g) {
    if (whatObstacle >= basketBall) {

        // Displaying basketball

        g.drawImage(sprites[3], (int) x, (int) y, null);

        Rectangle basketballRect = new Rectangle((int) x, (int) y, 35, 35);

        if (nerd.getFeetBounds().intersects(basketballRect)) {
            Game.setOverGameState(Game.GameOverlayState.BullyWins);
        }
        if (bully.getFeetBounds().intersects(basketballRect)) {
            Game.setOverGameState(Game.GameOverlayState.NerdWins);
        }

        // Displaying lava pool
    } else if (whatObstacle >= lava) {

        g.drawImage(sprites[2], (int) x, (int) y, null);

        Rectangle lavaRect = new Rectangle((int) x + 5, (int) y + 7, 80, 19);

        if (nerd.getFeetBounds().intersects(lavaRect)) {
            Game.setOverGameState(Game.GameOverlayState.BullyWins);
        }
        if (bully.getFeetBounds().intersects(lavaRect)) {
            Game.setOverGameState(Game.GameOverlayState.NerdWins);
        }

        // Displaying trash can

    } else if (whatObstacle == trash) {

        if (trashOpen) {
            g.drawImage(sprites[1], (int) x, (int) y, null);
        } else {
            g.drawImage(sprites[0], (int) x, (int) y, null);
        }

        Rectangle trashRect = new Rectangle((int) x, (int) y, 60, 97);

        if (bully.getBounds().intersects(trashRect)) {
            Game.setOverGameState(Game.GameOverlayState.BullyWins);
            Obstacles.trashOpen = true;
        }
    }
}

【问题讨论】:

  • 简单,把main方法放回去就好了。
  • 您是否阅读了您发布的错误消息?它已经告诉你该怎么做。 ;)
  • 我试过了,但它根本没有运行

标签: java main


【解决方案1】:

在你的类中插入一个主方法 - public static void main(String[] args)! 当您运行 Java 程序时,输出只是此方法中的输出。所以你总是需要它!

【讨论】:

  • 我已经有一个主类来处理我不需要放在主方法中的所有内容
【解决方案2】:

来自Setting an Application's Entry Point

如果您将应用程序捆绑在 JAR 文件中,则需要某种方式来指示 JAR 文件中的哪个类是您的应用程序的入口点。您可以在清单中使用 Main-Class 标头提供此信息,该标头具有一般形式:

Main-Class: classname

classname 是作为应用程序入口点的类的名称。

回想一下,入口点是一个具有签名public static void main(String[] args) 的方法的类。

【讨论】:

    【解决方案3】:

    老兄, 如果您正在扩展 Applet 类,那么 声明类后在 cmets 中添加以下代码:

    /*
    <applet code = "class-name" width = 300 height = 200></applet>
    */
    

    然后使用

    运行它
    appletviewer file-name.java
    

    如果是正常扩展 Frame 则:

    添加这样的方法:

    public static void main(String argsp[])
    {
    new Constructor();
    }
    

    它会起作用的。

    【讨论】:

      猜你喜欢
      • 2019-05-07
      • 2016-07-25
      • 2021-11-30
      • 1970-01-01
      • 1970-01-01
      • 2020-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多