无法启动我的applet程序!

时间:2011-04-04 06:24:39

标签: java applet

我在java中做了一些applet程序,点击按钮后会改变文本的颜色,编码如下:

 import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    public class colorpalette extends Applet implements ActionListener 
    {
        TextArea text;
        Button b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12;
        Panel p;
        public void init() 
       { 
         text = new TextArea(5, 10);
            b1 = new Button("lightgrey");
            b2 = new Button("grey");
            b3 = new Button("darkgrey");
            b4 = new Button("black");
            b5 = new Button("red");
            b6 = new Button("pink");
            b7 = new Button("orange");
            b8 = new Button("yellow");
            b9 = new Button("green");
            b10 = new Button("magenta");
            b11 = new Button("cyan");
            b12 = new Button("blue");
            p = new Panel();
            p.setLayout(new GridLayout(4, 3));
            p.add(b1);
            p.add(b2);
            p.add(b3);
            p.add(b4);
            p.add(b5);
            p.add(b6);
            p.add(b7);
            p.add(b8);
            p.add(b9);
            p.add(b10);
            p.add(b11);
            p.add(b12);
            setLayout(new BorderLayout());
            add("North", p);
            add("South", text);
            b1.addActionListener(this);
            b2.addActionListener(this);
            b3.addActionListener(this);
         b4.addActionListener(this);
            b5.addActionListener(this);
            b6.addActionListener(this);
            b7.addActionListener(this);
            b8.addActionListener(this);
            b9.addActionListener(this);
            b10.addActionListener(this);
            b11.addActionListener(this);
            b12.addActionListener(this);
        }

        public void actionPerformed(ActionEvent e) 
    {
     if (e.getActionCommand() == "lightgrey") 
        text.setBackground(Color.LIGHT_GRAY);
     else if (e.getActionCommand() == "grey")
        text.setBackground(Color.GRAY);
     else if (e.getActionCommand() == "darkgrey")
        text.setBackground(Color.DARK_GRAY);
     else if (e.getActionCommand() == "black")
        text.setBackground(Color.black);
     else if (e.getActionCommand() == "red")
        text.setBackground(Color.red);
     else if (e.getActionCommand() == "pink") 
        text.setBackground(Color.pink);
     else if (e.getActionCommand() == "orange") 
        text.setBackground(Color.orange);
     else if (e.getActionCommand() == "yellow") 
        text.setBackground(Color.yellow);
     else if (e.getActionCommand() == "green") 
        text.setBackground(Color.green);
     else if (e.getActionCommand() == "magenta") 
        text.setBackground(Color.magenta);
     else if (e.getActionCommand() == "cyan") 
        text.setBackground(Color.cyan);
     else if (e.getActionCommand() == "blue") 
        text.setBackground(Color.blue);

        }
    }
    //<applet code="colorpalette" width=50 height=50>
    //<\applet>

假设此文件名为colorpalette。当我编译javac colorpalette.java时没有错误,但是当我使用java colorpalette运行程序时,我收到的错误为Exception in thread "main" java.lang.NoSuchMethodError: main

任何人都可以说我,我错了!

3 个答案:

答案 0 :(得分:2)

问题在于您尝试从命令行运行applet,就像它是一个“正常”的Java程序一样。使用appletviewer或将applet嵌入HTML并在浏览器中查看。

有关详细信息,请参阅Java tutorial on applets

答案 1 :(得分:1)

这不是运行Java applet的方法。要运行applet文件,请使用以下命令:appletviewer ColorPalette.java

答案 2 :(得分:1)

  

当我编译javac colorpalette.java时没有错误,但是当我使用java colorpalette运行程序时,我收到错误

编译器不知道您打算如何使用该类,因此它不会检查可能需要但任何抽象超类或接口都不需要的方法。

在运行时,有人可能想调用一个不存在的方法(比如系统试图调用main)。在大多数情况下,这是由于编译时和运行时之间的版本冲突(在编译时使用不同版本的依赖项)或使用反射。