Reputation: 877
I don't believe this is a duplicate because the other questions were in regards to JButtons and JPanels.
I was wondering why the following in java isn't working like one would assume:
import javax.swing.JApplet;
import java.awt.*;
public class Example extends JApplet
{
public void paint(Graphics page)
{
setBackground (Color.cyan);
}
}
Basically when I run the applet the background won't change, regardless of color. I realize there are other options to get the same effect, but I am using examples from a textbook and would like to know why it doesn't work on my machine.
Upvotes: 0
Views: 5790
Reputation: 324098
but I am using examples from a textbook
Get rid of the text book. You should never override the paint() method of JApplet (that is an old AWT technique and is not used with Swing).
Applets in Swing are just like applications in Swing. You add components to the content pane of the applet. Custom painting if you need to do it is done by overriding the paintComponent() method of a JPanel (or JComponent) and then you add the panel to the content pane.
If you want to change the background of the applet then you change the background of the content pane (or the background of the panel you add to the CENTER of the content pane). Something like:
getContentPane().setBackground( Color.CYAN );
This code would be executed in the init() method.
Start by reading the Swing tutorial. There are section on How to Make Applets
and 'Performing Custom Painting`.
Upvotes: 3