Java Reference
In-Depth Information
Listing 8-27. A GridBagLayout Application Demonstrating Components Spanning Multiple Cells
import java.awt.*;
import javax.swing.*;
public class MyFrame {
static GridBagLayout grid = new GridBagLayout();
static GridBagConstraints constraints = new GridBagConstraints();
public static void main(String[] args) throws Exception {
JFrame theFrame = new JFrame();
theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
theFrame.setLayout(grid);
constraints.fill = GridBagConstraints.BOTH;
constraints.gridheight = 2;
theFrame.add(constrain(new JButton("Deep")));
constraints.gridheight = 1;
constraints.gridwidth = GridBagConstraints.REMAINDER;
theFrame.add(constrain(new JButton("Wide")));
constraints.gridwidth = 1;
theFrame.add(constrain(new JButton("One")));
theFrame.add(constrain(new JButton("Two")));
theFrame.pack();
theFrame.setVisible(true);
}
private static Component constrain(Component c) {
grid.setConstraints(c, constraints);
return c;
}
}
Figure 8-34. Example of GridBagLayout demonstrating components spanning multiple cells
Search WWH ::




Custom Search