Sunday, February 12, 2012

Layout concepts

Specifying the position of onscreen components in GUIs can be repetitious for programmers. Sound principles to bear in mind when designing a GUI are
  • ease of use
  • logical positioning
  • grouping
Object-oriented programming enables the functionality associated with laying out components to be incorporated into a class.
The appearance of Swing components is independent of the host platform. Swing comes with its own decorations – which allow its components to achieve a uniform look and feel across platforms.
Java enables you to create a screen layout that is consistent across different platforms, without having to specify absolute positioning.
Each layout manager ensures that components will be laid out in a reasonable way, regardless of platform. Layout managers allow you to make intelligent suggestions about layout, but cannot guarantee exact results across all platforms.
Java provides layout manager classes that deal with onscreen layout. These include
  • the original set of layout managers
  • new layout classes
the original set of layout managers
The original layout managers date from the Java Development Kit (JDK) 1.0 and are

  • FlowLayout
  • GridLayout
  • GridBagLayout
  • BorderLayout
  • CardLayout
new layout classes
New layout classes are continually being added. The newer layout classes include

  • BoxLayout
  • OverlayLayout
  • ScrollPanelLayout
  • SpringLayout
  • ViewportLayout
The layout manager classes implement the interface java.awt.LayoutManager and are used in conjunction with containers.
The java.awt.Component class is an abstract class, so it cannot be instantiated.

However, three of its derived concrete classes – Window, Applet, and Panel – play vital roles in constructing a graphical interface.
A complex GUI can consist of an applet or frame divided into different sections – normally panels. The panels can incorporate components such as labels, entry fields, and buttons.
A GUI can consist of panels contained within other panels, and each panel can use its own layout manager.
After you create a container, you associate a layout manager with the container by passing an instance of a specific layout manager object as an argument to the container's setLayout method.
You can then add mutually exclusive components. For example, you can add a label called l1 and add the text you wish to display as a parameter.
import java.awt.* ;
import java.applet.Applet;

public class NewApplet extends Applet {
  public void init () {
    Panel p1 = new Panel ();
    //set layout for panel p1
    p1.setLayout (new BorderLayout ());
    Label l1 = new Label ("First Name: ");
    TextField t1 = new TextField (12);
    Label l2 = new Label ("Last Name: ");
    TextField t2 = new TextField (12);
    p1.add(l1);
    p1.add(t1);
    p1.add(l2);
    p1.add(t2);
  }
}
If you use the add method to place components in a container, you need to know which layout manager is being used by that container.
Certain layout managers, such as BorderLayout, may require you to pass the component's relative location within the container as an argument.
import java.awt.* ;
import java.applet.Applet;

public class NewApplet extends Applet {
  public void init () {
    Panel p1 = new Panel ();
    //set layout for panel p1
    p1.setLayout (new BorderLayout ());
    Label l1 = new Label ("First Name: ");
    TextField t1 = new TextField (12);
    Label l2 = new Label ("Last Name: ");
    TextField t2 = new TextField (12);
    p1.add(l1);
    p1.add(t1);
    p1.add(l2);
    p1.add(t2);
  }
}

1 comments:

" Whoa! I’m enjoying the template/theme of this website. It’s simple, yet effective. A lot of times it’s very hard to get that “perfect balance” between superb usability and visual appeal. I must say you’ve done a very good job with this.
"
apple iphone service center in chennai | apple ipad service center in chennai | apple iphone service center in chennai

Post a Comment