I have done some applet program in java, which will change the color of the text when button is been clicked, the coding is given below:
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>
Assume that this file name is colorpalette. And when I compile javac colorpalette.java
there is no error, but when i run the program using java colorpalette
I'm getting an error as Exception in thread "main" java.lang.NoSuchMethodError: main
.
Can anyone say me, where I went wrong!
The problem is that you're trying to run an applet from the command line as if it were a "normal" Java program. Either use appletviewer
or embed the applet in HTML and view it in your browser.
See the Java tutorial on applets for more details.
This is not the way to run Java applets. To run applet file use following command: appletviewer ColorPalette.java
And when i compile javac colorpalette.java there is no error, but when i run the program using java colorpalette I'm getting an error
The compiler doesn't know how you intend to use that class and thus it doesn't check for methods that might be needed but are not required by any abstract superclass or interface.
At runtime someone might want to call a method which does not exist (like the system trying to call main
). In most cases this happens due to either a version clash between compile time and runtime (a different version of a dependency is used at compile time) or use of reflection.
精彩评论