Java Notes
Example - Second Window
![]() |
The main program of this example is basically identical to that in Example - First Window, but here we define a subclass of JFrame (a window) that constructs a JPanel to put the one component on. In this case the component is a button that does nothing. |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
/** * Program: basic_windows/Second.java - Customizes a subclass of JFrame //Note 1 * @version 2004-10-25 * @author Michael Maus */ import java.awt.*; //Note 2 import javax.swing.*; /////////////////////////////////////////////////////////// class Second class Second { //====================================================== method main public static void main(String[] args) { JFrame window = new SecondGUI(); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//Note 3 window.setVisible(true); // make window visible } } ////////////////////////////////////////////////////// class SecondGUI class SecondGUI extends JFrame { //Note 4 //====================================================== constructor public SecondGUI() { //Note 5 //... Create and initialize the components JButton helloButton; //Note 6 helloButton = new JButton("Hello Earthlings."); //Note 7 //... Create a panel to put the components on. //Note 8 JPanel content = new JPanel(); //Note 9 content.setLayout(new FlowLayout()); //Note 10 content.add(helloButton); //Note 11 this.setContentPane(content); //Note 12 //Note 13 this.setTitle("Second Window"); this.pack(); // finalize the layout //Note 14 } } |
Notes
Note 1: This style of comment is used by many programs because the <code>javadoc</code> program can read the produce HTML documentation.
Note 2: GUI programs will import classes from these packages, among others. The "*" at the end of an import specification imports all classes in that package.
Note 3: Causes a click on the close box to stop the program (the default is to make the window invisible, but the program keeps running). This is OK for amlll programs, but its also common to add a listener to ask the user whether files should be saved first.
Note 4: This class extends JFrame (the Java class for a window), which means that it can do everything a JFrame can, and more. This is an example of inheritance.
Note 5: The constructor tells how to build an object of this class. It is typically declared <i>public</i>. You can tell a constructor from a method because it has no return type and is the same name as the class.
Note 6: This declares a local variable for a button. See JButton for more on buttons.
Note 7: "new" creates an object, which in this case is a button with the appropriate text. It doesn't do anything yet
Note 8: There are two styles for working with the content pane. (1) Create a JPanel and set the content pane to this panel (what we do here), and (2) Get the predefined content pane and use it. Both styles are common.
Note 9: Create a new JPanel to use as the content pane.
Note 10: Java has a number of ways of arranging components on the content pane. This uses FlowLayout (left to right, top to bottom). There are other layouts (see Layouts).
Note 11: Each component must be "added" to the container (eg, JPanel). The layout controls where it goes.
Note 12: The content pane of a window is where all components are placed. Here we set the content pane to the JPanel we just created.
Note 13: this
is a reference to the current object, ie, the JFrame we're creating. "this" is implicitly added by the compiler to instance methods or variables so it's not required here, but it's sometimes written to make things clear. These methods are not defined in this class, but they are defined in one of the parent classes.
Note 14: pack
is called to do the final arranging of the components in the layout.
Next
See Example - Generic Calc for an example of components that really do something.