- CardLayout:
- GridLayout: Components in a grid-like fashion rather than "Center" or "North".
- GridBagLayout: The style used in HTMLTable.
- FlowLayout: Component to be laid in a flow (or row) and aligned (left, right, center).
- DefaultLayout: No layout, the Container will not change position during an update.
To use a layout we must call setLayout() for the Container with an instance of a LayoutManager. Use of each layout is different. For BorderLayout you specify the type of layout (North, South, etc.) by passing a string (i.e "North", "South", etc. to the method add() in a Container.
For example:
add("Center", myComponent);
adds myComponent to the Container and centers it. For different layouts, the first String syntax is different.
The following example displays a simple AWT program containing a tollbar and 3 buttons (cut, copy and paste buttons).
import java.awt.*;
import java.awt.event.*;
public class ToolbarFrame23 extends Frame implements ActionListener {
Button cutButton, copyButton, pasteButton;
public ToolbarFrame23() {
super("Toolbar Example (AWT)");
setSize(450, 250);
addWindowListener(new BasicWindowMonitor());
Panel toolbar = new Panel();
toolbar.setLayout(new FlowLayout(FlowLayout.LEFT));
cutButton = new Button("Cut");
cutButton.addActionListener(this);
toolbar.add(cutButton);
copyButton = new Button("Copy");
copyButton.addActionListener(this);
toolbar.add(copyButton);
pasteButton = new Button("Paste");
pasteButton.addActionListener(this);
toolbar.add(pasteButton);
add(toolbar, BorderLayout.NORTH);
}
public void actionPerformed(ActionEvent ae) {
System.out.println(ae.getActionCommand());
}
public static void main(String args[]) {
ToolbarFrame1 tf1 = new ToolbarFrame1();
tf1.setVisible(true);
}
}
The BasicWindowMonitor Class is also needed and is frequently used in other later examples:
import java.awt.event.*;
import java.awt.Window;
public class BasicWindowMonitor extends WindowAdapter {
public void windowClosing(WindowEvent e) {
Window w = e.getWindow();
w.setVisible(false);
w.dispose();
System.exit(0);
}
}
Output:
Menus
To use menus you need to create a MenuBar and Menus with MenuItems, then attach the MenuBar to your application via setMenuBar. You can even embed Menus in MenuItems.
- MenuBar mb = new MenuBar();
- Menu top = new Menu("File");
- Menu sub = new Menu("New");
- sub.add(new MenuItem("Document"));
- sub.add(new MenuItem("Image"));
- sub.add(new MenuItem("Message"));
- top.add(sub);
- top.add(new MenuItem("Save..));
- top.add(new MenuItem("Open"));
- setMenuBar(mb);
Drawing Lines
The following is an example of drawing lines:
g.drawLine(x1, y1, width, height);
Drawing Rectangles:
To draw a rectangle you would use one of the following:
- drawRect(x1, y1, width, height)
- drawRect3D(x1, y1, width, height, raised)
- drawRoundRect(x1, y1, width, height, arcWidth, arcHeight)
Drawing images
Drawing an image is done with drawImage, a method in the Graphics class. All Components implement the ImageObserver interface and applets can use getImage that retrives a gif/jpeg and returns an Image class, as in the following:
Image img = getImage(getDocumentBase(), "mypicture.gif");
public void paint(Graphics g)
{ g.drawImage(img, 10, 20, this);
}