Java Reference
In-Depth Information
}
Spring xSpring = Spring.constant(5,15,25); // x constraint for 1st button
Spring ySpring = Spring.constant(10,30, 50); // y constraint for first button
Spring wSpring = Spring.constant(30,80,130); // Width constraint for buttons
// Connect x,y for first button to left and top of container by springs
SpringLayout.Constraints buttonConstr = layout.getConstraints(buttons[0]);
buttonConstr.setX(xSpring);
buttonConstr.setY(ySpring);
// Set width and height of buttons and hook buttons together
for(int i = 0 ; i< buttons.length ; i++) {
buttonConstr = layout.getConstraints(buttons[i]);
buttonConstr.setHeight(ySpring); // Set the button height constraint
buttonConstr.setWidth(wSpring); // and its width constraint
// For buttons after the first tie W and N edges to E and N of predecessor
if(i>0) {
layout.putConstraint(SpringLayout.WEST, buttons[i],
xSpring,SpringLayout.EAST, buttons[i-1]);
layout.putConstraint(SpringLayout.NORTH, buttons[i],
ySpring,SpringLayout.SOUTH, buttons[i-1]);
}
}
aWindow.setVisible(true); // Display the window
}
}
When you compile and run this you should get a window with the buttons laid out as shown below.
Search WWH ::




Custom Search