The init() method is handled once—and only once—when the applet is run. As a result, it's an ideal place to set up values for any objects and variables that are needed for the applet to run successfully. This method is also a good place to set up fonts, colors, and the screen's background color. Here's an example:
public void init() {
FlowLayout flo = new FlowLayout();
setLayout(flo);
JButton run = new JButton("Run");
add(run);
}
If you are going to use a variable in other methods, it should not be created inside an init() method because it will only exist within the scope of that method.
For example, if you create an integer variable called displayRate inside the init() method and try to use it in the paint() method, you'll get an error when you attempt to compile the program. Create any variables you need to use throughout a class as object variables right after the class statement and before any methods.


8:56 AM
mitraaa
0 comments:
Post a Comment