2011-03-03 169 views
0

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

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 
+2

总是引用*问题中的代码*。人们不应该遵循一些随机链接来回答你,并且StackOverflow应该是独立的(外部链接可以被删除,移动等)。 – 2011-03-03 14:57:25

+0

是的,我忘记复制后立即意识到。 – Kosig 2011-03-03 15:00:39

+1

您确定引用的代码是正确的?这个主要的方法在这里看起来很奇怪... – reef 2011-03-03 15:01:33

回答

2

您可能会遇到此问题,因为您试图将Applet作为应用程序运行。小程序不包含主要方法,应用程序。如果你想运行小程序作为一个应用程序,只需添加一个main方法does the following

  1. 创建一个窗口(JFrame的)持有的小程序。
  2. 使窗口的关闭框停止小程序。
  3. 创建一个新的applet对象,并将其添加到窗口中。
  4. 通过调用init(),然后启动(),启动applet。
  5. 完成布局。
  6. 使窗口(其中的小程序)可见。
+0

好吧我正在考虑你的建议。虽然我有一个问题。它似乎并不认可SliderGraph()作为JApplet。编译时会出现以下错误。 'SliderGraph.java:找到不兼容的类型:SliderGrpah required:javax.swing.JApplet'它指向'JApplet theApplet = new SliderGraph()'这一行中的new一词;' – Kosig 2011-03-03 15:36:02

+0

SliderGraph extends ** java.applet.Applet **! = SliderGraph扩展** javax.swing.JApplet **坦率地说,小程序*不适用于新手! – 2011-03-03 18:07:40

+0

谢谢弗兰克,但是如果不用一些代码练习,人不会成为“新手”? – Kosig 2011-03-04 13:46:54

0

这是一个小程序,它不应该需要一个主要的方法,因为它有init()。将它编译为一个applet,而不是一个程序(如果你使用的是eclipse,那就是)。

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

+0

's /编译/运行/' – 2011-03-03 15:03:22

0

除了其他答案说作为一个小程序运行。你的示例代码中的主要方法被关闭得太晚,所有其他方法都被样本中的主要方法所包围,老实说,我不明白它如何编译它的当前状态。在init()注释结束后删除main方法和大括号。