I have a class that extends JFrame. Somewhere in this class I have the following piece of code.
Could somebody please help me with this particular statement.
add(<one of these objects of a class that extends JComponent - sort of a custom canvas >, BorderLayout.NORTH);
what is happening exactly when I type the above statement? The description of the method says - 'appends the specified component to the end of the container'. So the specified component must be the custom canvas and the container is the this object. Would that be right? So here that is SomeClass object. Right? In effect it is the JFrame. So is this statement adding a custom canvas to the JFrame? What is the purpose of BorderLayout.North? Is this part specifying where in the JFrame, a canvas is to be added?
Effectively yes, the component is your custom canvas and the container is the SomeClass object. The statement is adding a component to the JFrame, and it expects to be using a layout called the BorderLayout.
Java provides a variety of layout managers that handle placing your components within your window. They vary from the default FlowLayout, which just puts the components one after another in a line, to the highly complicated GridBagLayout, which allows you to divide the frame up into an arbitrary number of cells of potentially different sizes. The BorderLayout is one of the simpler ones. To use it, you need to call setLayout() with a BorderLayout instance, to change the layout from the FlowLayout to a BorderLayout.
When adding components to a frame with a border layout, the component is placed along one of the edges and stretched to fit, or placed in the middle of the frame and stretched to fit. You add the component to the frame with an argument of where the component should be placed. In your example, it is placed against the top of the frame and stretched horizontally to fit the left and right edges.