Java Reference
In-Depth Information
Note how the multi-cell spanning works. The component placed in the
cell at (column, row)
(0, 1) is assigned gridheight = 2 . This means it will
span 2 rows (row 1 to row 2). Similarly, the component placed at (1, 1) has
gridwidth = 2 so it spans two columns.
=
public class GridBagApplet extends JApplet
{
...init () sets up interface ...
} // class GridBagApplet
/** Creates a panel with 5 buttons using GridBagLayout. **/
class GridBagPanel extends JPanel
{
GridBagConstraints constraints = new GridBagConstraints ();
GridBagPanel () {
setLayout (new GridBagLayout ());
// Create a 3 row grid
// Fill the grid squares with the component
// in both x and y directions
constraints.fill = GridBagConstraints.BOTH;
// Keep same weight in vertical dimension
constraints.weighty = 1.0;
// Top row will include three components, each
// weighted differently in x
// 0,0
constraints.weightx = 1.0;
constraints.gridx = 0; constraints.gridy = 0;
add (new JButton ( " 0,0 " ), constraints);
// 0,1
constraints.weightx = 0.5;
constraints.gridx = 1; constraints.gridy = 0;
add (new JButton ( " 1,0 " ), constraints);
// 0,2
constraints.weightx = 0.1;
constraints.gridx = 2; constraints.gridy = 0;
add (new JButton ( " 2,0 " ), constraints);
// Middle row has two components. First takes up two
 
Search WWH ::




Custom Search