Java Reference
In-Depth Information
Figure 10•8. Hardcoded component positions
Example 10•8: NullLayoutPane.java
package com.davidflanagan.examples.gui;
import java.awt.*;
import javax.swing.*;
public class NullLayoutPane extends JPanel {
public NullLayoutPane() {
// Get rid of the default layout manager.
// We'll arrange the components ourselves.
this.setLayout(null);
// Create some buttons and set their sizes and positions explicitly
for(int i = 1; i <= 9; i++) {
JButton b = new JButton("Button #" + i);
b.setBounds(i*30, i*20, 125, 30); // use reshape() in Java 1.0
this.add(b);
}
}
// Specify how big the panel should be.
public Dimension getPreferredSize() { return new Dimension(425, 250); }
}
Creating Custom Layout Managers
When none of the predefined AWT layout managers is appropriate for the GUI
you want to implement, you have the option of writing your own custom layout
manager by implementing LayoutManager or LayoutManager2 . This is actually eas-
ier to do that it might seem. The primary method of interest is layoutContainer() ,
which the container calls when it wants the components it contains to be laid out.
This method should loop through the components contained in that container and
Search WWH ::




Custom Search