1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
class MyPanel {
JPanel panel;
posX = 0;
posY = 0;
// Add 1 component on the line
public void addRow1Component(JComponent label) {
GridBagConstraints c = new GridBagConstraints();
setConstraintEndRow(c);
c.insets = new Insets(0, 0, 5, 0);
panel.add(label, c);
}
// Position la contrainte et permet de passer à la ligne ensuite
private void setConstraintEndRow(GridBagConstraints c) {
setConstraintSize(
GridBagConstraints.REMAINDER, GridBagConstraints.RELATIVE,
new Insets(MARGIN_TOP_COMPONENT, MARGIN_LEFT_COMPONENT, MARGIN_BOTTOM_COMPONENT, MARGIN_RIGHT_COMPONENT), 1,
1, c);
setConstraintsForm(GridBagConstraints.NONE, GridBagConstraints.WEST, 0, 0, posX, posY, c);
posY++;
posX = 0;
}
private void setConstraintSize(
int gridwidth, int gridheight, Insets insets, double weightx, double weighty, GridBagConstraints c) {
c.gridwidth = gridwidth;
c.gridheight = gridheight;
c.insets = insets;
c.weightx = weightx;
c.weighty = weighty;
}
private void setConstraintsForm(
int fill, int anchor, int ipadx, int ipady, int gridx, int gridy, GridBagConstraints c) {
c.fill = fill;
c.anchor = anchor;
c.ipadx = ipadx;
c.ipady = ipady;
c.gridx = gridx;
c.gridy = gridy;
}
// Ajoute une ligne avec un certain nombre de composant
public void addRow(JComponent... components) {
if (0 == components.length) {
return;
}
GridBagConstraints c = new GridBagConstraints();
setConstraintEndRow(c);
c.gridwidth = GridBagConstraints.RELATIVE;
for (int i = 0; i < (components.length - 1); i++) {
panel.add(components[i], c);
c.gridx = c.gridx + 1;
}
c.gridwidth = GridBagConstraints.REMAINDER;
panel.add(components[components.length - 1], c);
} |
Partager