Wednesday 2 March 2016

Layout

Having created a JButton object, we need to place it in the container, but where? An object called a LayoutManager determines the location within the HelloComponent3 container at which to display the JButton. A LayoutManager object embodies a particular scheme for arranging components on the screen and adjusting their sizes. There are several standard layout managers to choose from, and we can, of course, create new ones. In our case, we specify one of the standard managers, a FlowLayout . The net result is that the button is centered at the top of the HelloComponent3 container. Our JFrame has another kind of layout, called BorderLayout. You’ll learn more about layout managers. To add the button to the layout, we invoke the add() method that HelloComponent3 inherits from Container, passing the JButton object as a parameter:

 add( theButton );  
add() is a method inherited by our class from the Container class. It appends our JButton to the list of components that the HelloComponent3 container manages. Thereafter, HelloComponent3 is responsible for the JButton: it causes the button to be displayed and it determines where in its window the button should be placed.

0 comments:

Post a Comment