【问题标题】:java.lang.NoSuchMethodError: main Exception in thread "main" [duplicate]java.lang.NoSuchMethodError:线程“main”中的主要异常[重复]
【发布时间】:2011-02-20 18:42:41
【问题描述】:

我不明白为什么这个消息会来-------->

java.lang.NoSuchMethodError: main
Exception in thread "main" .

我知道它期待 main() 方法,但我正在构建一个不包含 main 的小程序 方法而不是包含 init() 方法。那我该怎么办?我的代码如下 --->

import java.applet.*;
import java.awt.*;

public class Ballbewegung1 extends Applet implements Runnable
{
// Initialisierung der Variablen

    int x_pos = 10;     // x - Position des Balles
    int y_pos = 100;    // y - Position des Balles
    int radius = 20;    // Radius des Balles

    public void init()
    {
        setBackground (Color.blue);
    }

    public void start ()
    {
        // Schaffen eines neuen Threads, in dem das Spiel lไuft
        Thread th = new Thread (this);
        // Starten des Threads
        th.start ();
    }

    public void stop()
    {

    }

    public void destroy()
    {

    }

    public void run ()
    {
        // Erniedrigen der ThreadPriority um zeichnen zu erleichtern
        Thread.currentThread().setPriority(Thread.MIN_PRIORITY);

        // Solange true ist lไuft der Thread weiter
        while (true)
        {
            // Verไndern der x- Koordinate
            x_pos ++;

            // Neuzeichnen des Applets
            repaint();

            try
            {
                // Stoppen des Threads fr in Klammern angegebene Millisekunden
                Thread.sleep (20);
            }
            catch (InterruptedException ex)
            {
                // do nothing
            }

            // Zurcksetzen der ThreadPriority auf Maximalwert
            Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
        }
    }

    public void paint (Graphics g)
    {
        g.setColor  (Color.red);
        g.fillOval (x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);
    }
}

而且我不知道如何使用代码标签。所以请有人回答。

【问题讨论】:

  • 我得到了解决方案。步骤如下>1.编译java文件。虽然它没有运行,但它会创建一个.class文件。然后它需要嵌入到一个html文件中。在html文件中: 然后运行 ​​html 文件浏览器。
  • 此社区 Wiki 问题列出了此常见问题的可能原因:stackoverflow.com/questions/5407250/…

标签: java


【解决方案1】:

小程序需要一个容器才能运行。将其嵌入 html 页面并使用 appletviewer 或 Web 浏览器运行它

【讨论】:

    【解决方案2】:

    或者,如果您使用 Eclipse,则需要将其作为小程序运行:右键单击类 -> 运行方式 -> Java Applet(我不确定其他 IDE,但我猜它们具有类似的运行选项) .

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-28
      • 2011-06-17
      • 1970-01-01
      • 2011-01-14
      • 2019-06-18
      • 2018-08-02
      • 1970-01-01
      相关资源
      最近更新 更多