为什么会出现缺少主方法错误?

发布于 2024-10-20 10:01:47 字数 4221 浏览 1 评论 0原文

所以这个源代码是从这个网站免费获得的。 http://math.hws.edu/javamath/basic_applets/SliderGraph.html它似乎有点旧,但我不知道这是否是问题。我真的很想编写自己的小程序,但这正是我所需要的,而且我的时间很短。代码编译正常,没有错误,但在线程 main java.lang.NoSuchMethodError: main 中出现异常 为什么会发生这种情况?我应该在哪里添加 main 方法?

import java.awt.*;
import edu.hws.jcm.data.*;
import edu.hws.jcm.draw.*;
import edu.hws.jcm.awt.*;

public class SliderGraph extends java.applet.Applet {



   private DisplayCanvas canvas;

   public void stop() {
      canvas.releaseResources();
   }

   JCMPanel makeSliderPanel(VariableSlider v) {
         // A small utility routing that makes a JCMPanel that contains
         // a VariableSlider and a DisplayLabel that shows the value
         // of the variable associated with that slider.
      JCMPanel p = new JCMPanel();
      p.add(v, BorderLayout.CENTER);
      p.add( new DisplayLabel(v.getName() + " = #", new Value[] { v } ), BorderLayout.EAST);
      return p;
   }


   public void init() {

      Parser parser = new Parser();
      Variable x = new Variable("x");
      parser.add(x);

        // Create the three VariableSliders.  In this case, the sliders have
        //   names.  There is also a Variable associated with each slider,
        //   which has the same name.  This variable is added to the parser
        //   which is passed as the fourth parameter to the constructor, making
        //   it possible to use "a", "b", and "c" in expressions parsed by the
        //   parser.  Adjusting the value on a slider changes the value of the
        //   associated variable, and therefore changes the value of any
        //   expression that refers to that variable.  The second and third
        //   parameters to the constructor give the minimum and maximum Values
        //   on the slider.  Passing "null,null" uses the defaults, namely
        //   new Constant(-5) and new Constant(5).
      VariableSlider a = new VariableSlider("a",null,null,parser);
      VariableSlider b = new VariableSlider("b",null,null,parser);
      VariableSlider c = new VariableSlider("c",null,null,parser);

      canvas = new DisplayCanvas();
      canvas.setHandleMouseZooms(true);
      canvas.add(new Panner());

      LimitControlPanel limits =
           new LimitControlPanel( LimitControlPanel.SET_LIMITS | LimitControlPanel.RESTORE
                                    | LimitControlPanel.EQUALIZE,  false);
      limits.addCoords(canvas);

      ExpressionInput input = new ExpressionInput("a*x^2 + b*x + c", parser);
      Graph1D graph = new Graph1D(input.getFunction(x));

      ComputeButton button = new ComputeButton("Graph it!");

      canvas.add(new Axes());
      canvas.add(graph);
      canvas.add(new DrawBorder(Color.darkGray, 2));

      JCMPanel main = new JCMPanel();  // Build interface out of JCMPanels!
      main.setInsetGap(3);
      main.add(canvas, BorderLayout.CENTER);
      main.add(limits, BorderLayout.EAST);
      JCMPanel bot = new JCMPanel(5,1);
      main.add(bot, BorderLayout.SOUTH);

      bot.add(new Label("Enter a function f(x), which can use the constants a, b, and c:"));
      JCMPanel inputPanel = new JCMPanel();
      bot.add(inputPanel);
      inputPanel.add(input, BorderLayout.CENTER);
      inputPanel.add(button, BorderLayout.EAST);

      bot.add( makeSliderPanel(a) );  // Create and add the sliders.
      bot.add( makeSliderPanel(b) );
      bot.add( makeSliderPanel(c) );

      Controller controller = main.getController();  // Set up error reporting.
      controller.setErrorReporter(canvas);
      limits.setErrorReporter(canvas);

      main.gatherInputs();  // Set up main panel to respond to changes in input objects.
                            // This works since the interface is built of JCMPanels.  For
                            // the same reason, I don't have to add the objects the the
                            // controller.

      button.setOnUserAction(controller);  // Set controller to respond to button.

      setBackground(Color.lightGray);
      setLayout(new BorderLayout());
      add(main,BorderLayout.CENTER);

   } // end init()

} // end class SliderGraph

So this source code comes free from this website. http://math.hws.edu/javamath/basic_applets/SliderGraph.html It seems to be somewhat old but I don't know if that is the issue. I would really like to write my own applet but this is exactly what I need and I am very short on time. The code compiles fine with no errors but comes up Exception in thread main java.lang.NoSuchMethodError: main Why is this happening? Where should I add the main method?

import java.awt.*;
import edu.hws.jcm.data.*;
import edu.hws.jcm.draw.*;
import edu.hws.jcm.awt.*;

public class SliderGraph extends java.applet.Applet {



   private DisplayCanvas canvas;

   public void stop() {
      canvas.releaseResources();
   }

   JCMPanel makeSliderPanel(VariableSlider v) {
         // A small utility routing that makes a JCMPanel that contains
         // a VariableSlider and a DisplayLabel that shows the value
         // of the variable associated with that slider.
      JCMPanel p = new JCMPanel();
      p.add(v, BorderLayout.CENTER);
      p.add( new DisplayLabel(v.getName() + " = #", new Value[] { v } ), BorderLayout.EAST);
      return p;
   }


   public void init() {

      Parser parser = new Parser();
      Variable x = new Variable("x");
      parser.add(x);

        // Create the three VariableSliders.  In this case, the sliders have
        //   names.  There is also a Variable associated with each slider,
        //   which has the same name.  This variable is added to the parser
        //   which is passed as the fourth parameter to the constructor, making
        //   it possible to use "a", "b", and "c" in expressions parsed by the
        //   parser.  Adjusting the value on a slider changes the value of the
        //   associated variable, and therefore changes the value of any
        //   expression that refers to that variable.  The second and third
        //   parameters to the constructor give the minimum and maximum Values
        //   on the slider.  Passing "null,null" uses the defaults, namely
        //   new Constant(-5) and new Constant(5).
      VariableSlider a = new VariableSlider("a",null,null,parser);
      VariableSlider b = new VariableSlider("b",null,null,parser);
      VariableSlider c = new VariableSlider("c",null,null,parser);

      canvas = new DisplayCanvas();
      canvas.setHandleMouseZooms(true);
      canvas.add(new Panner());

      LimitControlPanel limits =
           new LimitControlPanel( LimitControlPanel.SET_LIMITS | LimitControlPanel.RESTORE
                                    | LimitControlPanel.EQUALIZE,  false);
      limits.addCoords(canvas);

      ExpressionInput input = new ExpressionInput("a*x^2 + b*x + c", parser);
      Graph1D graph = new Graph1D(input.getFunction(x));

      ComputeButton button = new ComputeButton("Graph it!");

      canvas.add(new Axes());
      canvas.add(graph);
      canvas.add(new DrawBorder(Color.darkGray, 2));

      JCMPanel main = new JCMPanel();  // Build interface out of JCMPanels!
      main.setInsetGap(3);
      main.add(canvas, BorderLayout.CENTER);
      main.add(limits, BorderLayout.EAST);
      JCMPanel bot = new JCMPanel(5,1);
      main.add(bot, BorderLayout.SOUTH);

      bot.add(new Label("Enter a function f(x), which can use the constants a, b, and c:"));
      JCMPanel inputPanel = new JCMPanel();
      bot.add(inputPanel);
      inputPanel.add(input, BorderLayout.CENTER);
      inputPanel.add(button, BorderLayout.EAST);

      bot.add( makeSliderPanel(a) );  // Create and add the sliders.
      bot.add( makeSliderPanel(b) );
      bot.add( makeSliderPanel(c) );

      Controller controller = main.getController();  // Set up error reporting.
      controller.setErrorReporter(canvas);
      limits.setErrorReporter(canvas);

      main.gatherInputs();  // Set up main panel to respond to changes in input objects.
                            // This works since the interface is built of JCMPanels.  For
                            // the same reason, I don't have to add the objects the the
                            // controller.

      button.setOnUserAction(controller);  // Set controller to respond to button.

      setBackground(Color.lightGray);
      setLayout(new BorderLayout());
      add(main,BorderLayout.CENTER);

   } // end init()

} // end class SliderGraph

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

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

发布评论

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

评论(3

甲如呢乙后呢 2024-10-27 10:01:48

它是一个小程序,它不需要 main 方法,因为它有 init()。将其编译为小程序,而不是程序(如果您使用的是 eclipse,那就是)。

编辑:在命令行上,您可以在 .class 文件上使用“appletviewer”命令。

It's an applet, it shouldn't need a main method because it has init(). Compile it as an applet, not a program (if you're using eclipse, that is).

EDIT: on the command line, you'd use the "appletviewer" command on the .class file.

飘过的浮云 2024-10-27 10:01:48

除了其他答案说作为小程序运行之外。示例代码中的主要方法关闭得太晚了,所有其他方法都包含在示例中的主要方法中,说实话,我看不出它如何在当前状态下编译。删除 main 方法和结尾 init() 注释后的右大括号。

In addition to the other answers saying run as an applet. The main method in your sample code is closed too late, all other methods are enclosed by the main method in your sample, to be honest I can't see how it could compile in it's current state. Remove the main method and the closeing curly brace after the end init() comment.

太傻旳人生 2024-10-27 10:01:47

您可能会遇到此问题,因为您正在尝试将 Applet 作为应用程序运行。小程序不包含主要方法,而应用程序则包含。如果您想将Applet作为应用程序运行,只需添加一个main方法,该方法执行以下操作

  1. 创建一个窗口 (JFrame) 来容纳小程序。
  2. 让窗口的关闭框停止小程序。
  3. 创建一个新的小程序对象,并将其添加到窗口中。
  4. 通过调用 init() 和 start() 来启动小程序。
  5. 完成布局。
  6. 使窗口(其中包含小程序)可见。

You are likely encountering this issue because you are trying to run the Applet as an Application. Applets do not contain main methods, Applications do. If you want to run the Applet as an Application, just add a main method that does the following:

  1. Create a window (JFrame) to hold the applet.
  2. Make the window's close box stop the applet.
  3. Create a new applet object, and add it to the window.
  4. Start the applet by calling init(), then start().
  5. Finalize the layout.
  6. Make the window (with the applet in it) visible.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文