// Make a new Panel and add the TextFields to it.
  Panel p1 = new Panel();
  p1.setLayout(new GridLayout(2, 1));
  p1.add(number1);
  p1.add(number2);

  // Make a second Panel and add the Button and Label to it
  Panel p2 = new Panel();
  p2.setLayout(new GridLayout(2,1));
  p2.add(greeting);
  p2.add(pressMe);

  // don't forget to add the Panels to the Applet!
  add(p1);
  add(p2);

  pressMe.addActionListener(this);

How to improve Applet layout

Students always ask me this, so try this quick and dirty technique for now.

We use another awt object called a Panel, then we set GridLayout to the panel.

By using more than one Panel we can group items together. It's a bit of a kludge but will keep you off my back for a while.

Hopefully.

If you replace the init code with that shown on the left, you will get a neater layout.

The first parameter to GridLayout is the number of rows and the second, the number of columns. You add the items across, then down.


 
Close this window to return to the previous page


Questions or problems related to this web site should be addressed to Richard Jones who asserts his right to be identified as the author and owner of these materials - unless otherwise indicated. Please feel free to use the material presented here and to create links to it for non-commercial purposes; an acknowledgement of the source is required by the Creative Commons licence. Use of materials from this site is conditional upon your having read the additional terms of use on the about page and the Creative Commons Licence. View privacy policy.

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License.

© 2001 - 2006 Richard Jones, PO BOX 246, Cambridge, New Zealand; This page was last modified on April 4, 2006