Java GridBagConstraints gridx
Syntax
GridBagConstraints.gridx has the following syntax.
public int gridx
Example
In the following code shows how to use GridBagConstraints.gridx field.
/*w w w . j av a 2 s . c o m*/
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main extends JPanel {
GridBagConstraints constraints = new GridBagConstraints();
public Main() {
setLayout(new GridBagLayout());
int x, y; // for clarity
addGB(new JButton("North"), x = 1, y = 0);
addGB(new JButton("West"), x = 0, y = 1);
addGB(new JButton("Center"), x = 1, y = 1);
addGB(new JButton("East"), x = 2, y = 1);
addGB(new JButton("South"), x = 1, y = 2);
}
void addGB(Component component, int x, int y) {
constraints.gridx = x;
constraints.gridy = y;
add(component, constraints);
}
public static void main(String[] args) {
JFrame frame = new JFrame("GridBag1");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(225, 150);
frame.setLocation(200, 200);
frame.setContentPane(new Main());
frame.setVisible(true);
}
}
Home »
Java Tutorial »
java.awt »
Java Tutorial »
java.awt »