Java: 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 JPanel that we will put in the window. JPanel in a container that can old other components. The JPanle subclass adds one component to itself -- in this case just 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 
 /**
 * Program:  Second.java - Customizes a subclass of JPanel  //Note 1
 * @version 2003-00-00
 * @author Mickey Mouse
 */

import java.awt.*;                                         //Note 2
import javax.swing.*;


/////////////////////////////////////////////////////////// class Second
class Second {
    //====================================================== method main
    public static void main(String[] args) {
        JFrame myWindow = new JFrame("SecondWindow");
        myWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//Note 3
        myWindow.getContentPane().add(new SecondPanel()); //Note 4
        myWindow.pack();       // finalize the layout     //Note 5
        myWindow.show();       // make window visible
    }//end main
}//endclass Second


////////////////////////////////////////////////////// class SecondPanel
class SecondPanel extends JPanel {                       //Note 6
    //====================================================== constructor
    SecondPanel() {                                      //Note 7
        //--- create the components
        JButton helloButton;                             //Note 8
        helloButton = new JButton("Hello Earthlings.");  //Note 9
                                                         //Note 10
        this.setLayout(new FlowLayout());                //Note 11
        this.add(helloButton);                           //Note 12
    }//end constructor
}//endclass Secondpanel
The text from the above example can be selected, copied, and pasted into an editor.

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: Most of your programs will import the classes from these four packages. 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 for nothing to happen). This is OK for these programs, but its also common to add a listener to ask the user whether files should be saved first.

Note 4: The content pane of a window is where all components are placed. This gets the content pane and adds our panel to it. The "new" operator creates a new object of this class (new SecondPanel()), The constructor of the class is called to tell how to build the new object. This constructor has instructions to build a customized panel.

Note 5: pack is called to do the final arranging of the components in the layout.

Note 6: This class extends JFrame (the Java class for a window), which means that it can do everything a JPanel can, and more. This is an example of inheritance.

Note 7: The constructor tells how to build an object of this class.

Note 8: This declares a local variable will hold a button. See JButton for more on buttons. A panel will typically contain several different components.

Note 9: "new" creates an object, which in this case is a button with the appropriate text. It doesn't do anything yet

Note 10: this is a reference the current object, ie, the panel we're creating. It's implicitly added by the compiler to instance methods or fields, but it's sometimes written to make things clear.

Note 11: 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 12: Each component must be "added" to the container (eg, JPanel). The layout controls where it goes.

Next

See Example - Generic Calc for an example of components that really do something.