GridBagLayout is a layout manager that lays out a container’s components in a grid of cells with each component occupying one or more cells, called its display area. The display area aligns components vertically and horizontally, without requiring that the components be of the same size.
GridBagLayout Source Code
import java.awt.*;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class GridBagLayoutDemo {
public static void addComponentsToPane(Container pane) {
JButton jbnButton;
pane.setLayout(new GridBagLayout());
GridBagConstraints gBC = new GridBagConstraints();
gBC.fill = GridBagConstraints.HORIZONTAL;
jbnButton = new JButton("Button 1");
gBC.weightx = 0.5;
gBC.gridx = 0;
gBC.gridy = 0;
pane.add(jbnButton, gBC);
JTextField jtf = new JTextField("TextField 1");
gBC.gridx = 2;
gBC.gridy = 0;
jtf.setEditable(false);
pane.add(jtf, gBC);
jbnButton = new JButton("Button 3");
gBC.gridx = 2;
gBC.gridy = 0;
pane.add(jbnButton, gBC);
jbnButton = new JButton("Button 4");
|
Output
After Expanding the Frame
No comments:
Post a Comment