Using Layouts

The standard layouts described in Bell can be layered within other panels. The applet below shows how you can use layouts and panels to create different layouts of your GUIs.

Code for Layouts.java

Click here for the code --> Layouts code

This applet uses an overall BorderLayout.

  • The north portion (top) is a panel that has three buttons. The default "flow" layout is used. If you press any of these buttons, the background of the drawing area is changed.
  • The west portion (left) is a panel as well. Because I wanted the buttons to be laid out vertically rather than horizontally (the default of the "flow" layout), I had to assign a Grid Layout to the panel. Because I wanted spaces between the buttons, I made more rows than required, using a blank label as a "place holder".
  • The center portion (remaining portion of this applet) is a canvas or drawing area of the applet. I chose to use a canvas object to try it out. I could have used a panel as well. Both the canvas and panel object have a Graphics object associated with it. This Graphics object can be used to draw either strings and shapes or images. When you press the Picture button, I use the drawString, drawOval, fillRect and setColor methods associated with the Graphics objects. Note that these are drawn on top of what is already drawn.
  • If you wanted, you could also define items in the other border areas (east, south).
  • Layouts can be associated with Panels, but not canvases.
  • Note that I still need to add the Listeners for the individual GUI objects.

 

Java
Sample Code