繁体   English   中英

无法启动我的applet程序!

[英]Can't start my applet program!

我已经在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出现Exception in thread "main" java.lang.NoSuchMethodError: main

谁能说我,我哪里出错了!

问题是您试图从命令行运行小程序,就像它是“普通” Java程序一样。 使用appletviewer或将applet嵌入HTML并在浏览器中查看。

有关更多详细信息,请参见有关appletJava教程

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

而且当我编译javac colorpalette.java时没有错误,但是当我使用java colorpalette运行程序时却出现了错误

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM