Я сделал некоторую программу апплета в Java, которая будет изменять цвет текста при нажатии кнопки, кодировка приведена ниже:
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
.
Может кто-нибудь сказать мне, где я ошибся!