Java Reference
In-Depth Information
Listing 8-26. A Simple GridBagLayout Application Similar to Listing 8-25
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.anchor = GridBagConstraints.WEST;
JButton one = new JButton("One");
grid.setConstraints(one, constraints);
theFrame.add(one);
constraints.gridwidth = GridBagConstraints.REMAINDER;
theFrame.add(constrain(new JLabel("A very long component")));
constraints.weightx = 0.0;
constraints.gridwidth = 1;
theFrame.add(constrain(new JTextField("Three")));
theFrame.add(constrain(new JTextArea("Four\nFive\nSix")));
theFrame.pack();
theFrame.setVisible(true);
}
private static Component constrain(Component c) {
grid.setConstraints(c, constraints);
return c;
}
}
Figure 8-33. Example of GridBagLayout demonstrating inconsistent cell sizes
Search WWH ::




Custom Search