|
Up to this point, we've constructed applications with
a single JPanel. Now we want to add a JButton into
the program as well.
How the button is added and where is described using a
LayoutManager. By default, the content pane of a
JFrame or JApplet is set to an instance of
BorderLayout You may think of this as dividing the pane up
into five regions: one in the center and four around the sides.
In the example above, we place our JPanel containing the
rectangle in the center:
getContentPane().add(panel, BorderLayout.CENTER);
To have the button appear below, we place it in the southern
location. First, however, we put the button into a JPanel
and then put the JPanel into the content pane.
JPanel buttonPanel = new JPanel();
buttonPanel.add(button);
getContentPane().add(panel, BorderLayout.CENTER);
Without using buttonPanel, the button would be as wide as
the content pane.
|