Web Pro

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 20

Up

Previous

Next

Using Java Layouts AWT containers

The four AWT containers for which you can specify a layout policy include:

Frame

Panel

Applet

Dialog

Builder Xcessory uses a default layout policy of No Layout for all four containers to help you when creating and placing children. In nearly all cases, you will select a specific layout policy. These policies are described briefly in the following sections.

Generally with Builder Xcessory, the container (or parent) has resources corresponding to the type of layout object created, and any information passed to the layout object's constructor. The children typically have resources related to the position or resize behavior of that particular child. These resources are treated as constraints and are listed last in the Resource Editor. No Layout

It is possible, though rare, to create a Java application that does not use a layout manager (similar to using a Motif Bulletin Board). Any object you place into a container with No Layout specified remains in

its initial position and size. With Builder Xcessory, this is the only layout policy where you can manipulate objects directly with the mouse and various alignment facilities. The AWT method: setBounds(int x, int y, int width, int height)

allows you to move and resize objects programmatically. Flow

The Flow layout causes the container to line up its children horizontally. When room on one line is exhausted, the Flow layout creates an additional line for its children. You can specify how the Flow positions the children on each line: centered, left aligned, or right aligned.

Resizing the container results in the (possible) rearrangement of the children into however many rows are appropriate, but children are not resized.

Set the following resources for the container: Layout: Flow Layout Alignment: center|left|right

Children have the following constraints: None Border

The Border layout causes the container to place its children into five areas: north, south, east, west, and center (see Border Layout ). The borders are set first, with the remaining space taken up by the center child. Each area can have only one visible child.

Resizing the container does not change the thickness of the borders, but changes both the height and width of the "center" child.

Set the following resources for the container: Layout: Border Layout

Children have the following constraints: Direction:center|north|east|south|west

Border Layout Card

The Card layout causes the container to place its children atop one another, providing show , first , last , next , and previous methods to control which child is visible.

The card layout sizes its child to fill the entire space of the container. Resizing the container resizes its children.

Set the following resources for the container: Layout: Card Layout

Children have the following constraints: None Grid

The Grid layout causes the container to manage its children into rows and columns. You specify how many rows and columns to create. Children are placed into the rows from left to right, one row at a time, in the order in which you add them to the container. All children are identically sized. Resizing the container resizes its children.

Set the following resources for the container: Layout: Grid Layout Columns: [integer] Rows: [integer]

Children have the following constraints: None Hint: To change the order in which the children are laid out, use the Raise/Lower operations (Browser:Edit or MB3 Quick Access menu). GridBag

The GridBag is the most powerful and the most complex layout in the AWT (the only AWT layout object that requires its own, distinct constraints object). With the GridBag, you can control where each individual child is placed, how it is moved, and how it is resized.

Each child is placed at a cell position with the gridx and gridy resources. You can then specify how many columns wide and how many rows high each child is with the gridwidth and gridheight resources. The GridBag automatically calculates the total number of rows and columns. Note: The GridBag automatically uses the fewest possible number of rows and columns. Specifying particular gridy and gridx values does not guarantee that the GridBag will work with that number of rows or columns.

Within each allocated position, you can specify whether or not the child should grow to fill extra space in the grid cell with the fill resource. This extra space might be present because the default size of the child is smaller than the cell size, or the space might become available when the container is resized. If a child is not set to fill the entire area available, you use the anchor resource to align the child within its grid cell. Partial fills

For partial fills, use the resources ipadx , ipady , and the various insets . The ipad resources specify internal padding, that is, how much bigger than its default size the child should be. The insets specify external padding, in other words, margins.

The two most powerful constraints for GridBag children are weightx and weighty . While fill controls how the children grow to fill a grid cell, weightx and weighty control how the actual grid cells grow (or shrink) when the container is resized, that is, how new space is apportioned to the grid cells. Values for weightx and weighty are proportional. A value of 0.0 indicates that the cell does not grow (or shrink). A cell with a weight of 10 grows twice as much as a cell with a weight of 5. Cells with identical weights grow the same amount.

Set the following resources for the container: Layout: GridBag Layout

Children have the following constraints: Gridx: [integer] Gridy: [integer] Gridwidth: [integer] Gridheight: [integer] Fill: horizontal|vertical|both|none Anchor:center|east|north|northeast|northwest|south| southeast|southwest|west

Ipadx: [integer] Ipady: [integer] TopInset: [integer] RightInset: [integer] BottomInset: [integer] LeftInset: [integer] Weightx: [double] Weighty: [double]

Figures GridBag After Resize with All Weights Equal to Zero and GridBag After Resize with Weights as Indicated show the results of resizing a GridBag with weights set to 0 and non-zero, respectively.

GridBag After Resize with All Weights Equal to Zero

Notice that with all the weights set to zero, the GridBag does not increase the size of any of its cells, but maintains the grid centered in the container.

When you add weights , the grid resizes in accordance to the weight values. Notice that fills now come into play, along with anchor positions.

GridBag After Resize with Weights as Indicated

Examining the weightx values, we see that objects A and D do not resize at all, but objects B and C do. Because neither B nor C is in column 0, that column does not resize. Column 1 resizes to take up all of

the remaining space. Notice that B and C have different values for weightx . Because they are proportional to the other columns (column 0), the difference becomes irrelevant.

Looking at the weighty values, we see that the sole object (D) in the bottom row does not resize, but all others do resize. The rows occupied by object A take up 100% of the new space. The rows occupied by objects B and C share the new space equally.

The weight values are also be used by the GridBag, along with the object sizes, to calculate the initial row heights and column widths.

Flow Layout, Border Layout, Grid layout, Grid bag layout and card layout in java program Posted by AMAR at 6:36 PM Labels: JAVA, Online Programs

Aim To implement layout like border, grid bag, flow card ,grid layout in java program | CS1404 INTERNET PROGRAMMING LABORATORY in Java Program Algorithm Start the program Import at the necessary package Declare the class in public FlowLayoutDemo Then declare the layout with their syntax In the graphics function to display the object Terminate the program Source Code | Java Program | Flow layout import java.awt.*; import java.awt.event.*;

import java.applet.*; /* <applet code="FlowLayoutDemo" width=300 height=300> </applet> */ public class FlowLayoutDemo extends Applet implements ItemListener { Checkbox chkWinXP, chkWin2003, chkRed, chkFed; public void init() { setLayout(new FlowLayout(FlowLayout.LEFT)); Label lblOS = new Label("Operating System(s) Knowledge :- "); chkWinXP = new Checkbox("Windows XP"); chkWin2003 = new Checkbox("Windows 2003 Server"); chkRed = new Checkbox("Red Hat Linux"); chkFed = new Checkbox("Fedora"); add(lblOS); add(chkWinXP); add(chkWin2003); add(chkRed); add(chkFed); chkWinXP.addItemListener(this); chkWin2003.addItemListener(this); chkRed.addItemListener(this);

chkFed.addItemListener(this); } public void itemStateChanged(ItemEvent ie) { repaint(); } public void paint(Graphics g) { g.drawString("Operating System(s) Knowledge : ", 10, 130); g.drawString("Windows Xp : " + chkWinXP.getState(), 10, 150); g.drawString("Windows 2003 Server : " + chkWin2003.getState(), 10, 170); g.drawString("Red Hat Linux : " + chkRed.getState(), 10, 190); g.drawString("Fedora : " + chkFed.getState(), 10, 210); } } Output:-

Compile java FlowLayoutDemo program

C:\IPLAB>javac FlowLayoutDemo.java C:\IPLAB>appletviewer FlowLayoutDemo.java

Source Code | Java Program | BorderLayout import java.awt.*; import java.applet.*; import java.util.*; /* <applet code="BorderLayoutDemo" width=500 height=250> </applet> */ public class BorderLayoutDemo extends Applet { public void init() { setLayout(new BorderLayout()); add(new Button("Rajalakshmi Engineering College"), BorderLayout.NORTH); add(new Label("Rajalakshmi Nagar, Thandalam, Chennai - 602 105"), BorderLayout.SOUTH); add(new Button("Mission"), BorderLayout.EAST); add(new Button("Vision"), BorderLayout.WEST); String msg = "Rajalakshmi Engineering College was established \n" + "in the year 1997 under the aegis of Rajalakshmi Educational Trust \n" + "whose members have had consummate experience in the

fields of \n" + "education and industry."; add(new TextArea(msg), BorderLayout.CENTER); } } Output:C:\IPLAB>javac BorderLayoutDemo.java C:\IPLAB>appletviewer BorderLayoutDemo.java

Source Code | Java Program | Grid layout import java.awt.*; import java.applet.*; /* <applet code="GridLayoutDemo" width=400 height=200> </applet> */ public class GridLayoutDemo extends Applet { public void init() { setLayout(new GridLayout(4, 4)); setFont(new Font("SansSerif", Font.BOLD, 24)); for(int i = 1; i <=15 ; i++) { add(new Button("" + i)); } }

} Output:C:\IPLAB>javac GridLayoutDemo.java C:\IPLAB>appletviewer GridLayoutDemo.java

Source Code | Java Program | CardLayout import java.awt.*; import java.awt.event.*; import java.applet.*; /* <applet code="CardLayoutDemo" width=300 height=100> </applet> */ public class CardLayoutDemo extends Applet implements ActionListener, MouseListener { Checkbox chkVB, chkASP, chkJ2EE, chkJ2ME; Panel pnlTech; CardLayout cardLO;

Button btnMicrosoft, btnJava; public void init() { btnMicrosoft = new Button("Microsoft Products"); btnJava = new Button("Java Products"); add(btnMicrosoft); add(btnJava); cardLO = new CardLayout(); pnlTech = new Panel(); pnlTech.setLayout(cardLO); chkVB = new Checkbox("Visual Basic"); chkASP = new Checkbox("ASP"); chkJ2EE = new Checkbox("J2EE"); chkJ2ME = new Checkbox("J2ME"); Panel pnlMicrosoft = new Panel(); pnlMicrosoft.add(chkVB); pnlMicrosoft.add(chkASP); Panel pnlJava = new Panel(); pnlJava.add(chkJ2EE); pnlJava.add(chkJ2ME); pnlTech.add(pnlMicrosoft, "Microsoft"); pnlTech.add(pnlJava, "Java"); add(pnlTech); btnMicrosoft.addActionListener(this); btnJava.addActionListener(this);

addMouseListener(this); } public void mousePressed(MouseEvent me) { cardLO.next(pnlTech); } public void mouseClicked(MouseEvent me) { } public void mouseEntered(MouseEvent me) { } public void mouseExited(MouseEvent me) { } public void mouseReleased(MouseEvent me) { } public void actionPerformed(ActionEvent ae) { if(ae.getSource() == btnMicrosoft) { cardLO.show(pnlTech, "Microsoft"); } else { cardLO.show(pnlTech, "Java"); } } } Output:C:\IPLAB>javac CardLayoutDemo.java

C:\IPLAB>appletviewer CardLayoutDemo.java

Several AWT and Swing classes provide layout managers for general use: BorderLayout BoxLayout CardLayout FlowLayout GridBagLayout GridLayout GroupLayout SpringLayout

This section shows example GUIs that use these layout managers, and tells you where to find the howto page for each layout manager. You can find links for running the examples in the how-to pages and in the example index. Note: This lesson covers writing layout code by hand, which can be challenging. If you are not interested in learning all the details of layout management, you might prefer to use the GroupLayout layout manager combined with a builder tool to lay out your GUI. One such builder tool is the NetBeans IDE.

Otherwise, if you want to code by hand and do not want to use GroupLayout, then GridBagLayout is recommended as the next most flexible and powerful layout manager. BorderLayout

Every content pane is initialized to use a BorderLayout. (As Using Top-Level Containers explains, the content pane is the main container in all frames, applets, and dialogs.) A BorderLayout places components in up to five areas: top, bottom, left, right, and center. All extra space is placed in the center area. Tool bars that are created using JToolBar must be created within a BorderLayout container, if you want to be able to drag and drop the bars away from their starting positions. For further details, see How to Use BorderLayout. BoxLayout

The BoxLayout class puts components in a single row or column. It respects the components' requested maximum sizes and also lets you align components. For further details, see How to Use BoxLayout.

CardLayout

The CardLayout class lets you implement an area that contains different components at different times. A CardLayout is often controlled by a combo box, with the state of the combo box determining which panel (group of components) the CardLayout displays. An alternative to using CardLayout is using a tabbed pane, which provides similar functionality but with a pre-defined GUI. For further details, see How to Use CardLayout. FlowLayout

FlowLayout is the default layout manager for every JPanel. It simply lays out components in a single row, starting a new row if its container is not sufficiently wide. Both panels in CardLayoutDemo, shown previously, use FlowLayout. For further details, see How to Use FlowLayout. GridBagLayout

GridBagLayout is a sophisticated, flexible layout manager. It aligns components by placing them within a grid of cells, allowing components to span more than one cell. The rows in the grid can have different heights, and grid columns can have different widths. For further details, see How to Use GridBagLayout.

GridLayout

GridLayout simply makes a bunch of components equal in size and displays them in the requested number of rows and columns. For further details, see How to Use GridLayout. GroupLayout

GroupLayout is a layout manager that was developed for use by GUI builder tools, but it can also be used manually. GroupLayout works with the horizontal and vertical layouts separately. The layout is defined for each dimension independently. Consequently, however, each component needs to be defined twice in the layout. The Find window shown above is an example of a GroupLayout. For further details, see How to Use GroupLayout. SpringLayout

SpringLayout is a flexible layout manager designed for use by GUI builders. It lets you specify precise relationships between the edges of components under its control. For example, you might define that the left edge of one component is a certain distance (which can be dynamically calculated) from the right edge of a second component. SpringLayout lays out the children of its associated container according to a set of constraints, as shall be seen in How to Use SpringLayout.

You might also like