Java: Example - CircleMain.java
Here is the program which uses the CirclePanel class
(see Example - CirclePanel.java).
/**
* Program: CircleMain.java - Uses the CirclePanel component
* @version 2002-00-00
* @author Mickey Mouse
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
/////////////////////////////////////////////////////////////////// CircleMain
class CircleMain extends JFrame{
//================================================ instance variables
CirclePanel drawing = new CirclePanel(); // Note 1
//======================================================= constructor
CircleMain() {
//--- Get content pane, set layout, add components
Container content = this.getContentPane();
content.setLayout(new BorderLayout());
content.add(drawing, BorderLayout.CENTER); // Note 2
this.setTitle("Circles");
this.pack(); // finalize the layout
}//end constructor
//============================================================== main
public static void main(String[] args) {
JFrame myWindow = new CircleMain();
myWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myWindow.setVisible(true);
}//end main
}//endclass CircleMain
Notes
- The drawing is declared as an instance variable, which can be referenced
in all methods. There is no need for that here since no one every
references after it has been built. However, it is very common to
interact with a drawing during execution of a program, and an instance
variable is useful for later interaction.
- Because the variable doesn't need to be an instance variable
or indeed any variable, this
could be rewritten as
content.add(new CirclePanel(), BorderLayout.CENTER);