Java Reference
In-Depth Information
wndSize.width/2, wndSize.height/2); // Size
aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SpringLayout layout = new SpringLayout();
// Create a
layout manager
Container content = aWindow.getContentPane();
// Get the
content pane
content.setLayout(layout);
// Set the
container layout mgr
JButton[] buttons = new JButton[6];
// Array to
store buttons
SpringLayout.Constraints constr = null;
for(int i = 0; i < buttons.length; ++i) {
buttons[i] = new JButton("Press " + (i+1));
content.add(buttons[i]);
// Add a Button
to content pane
}
Spring xSpring = Spring.constant(5,15,25);
// x constraint
for 1st button
Spring ySpring = Spring.constant(10,30, 50);
// y constraint
for 1st button
// Connect x,y for first button to left and top of container by
springs
constr = layout.getConstraints(buttons[0]);
constr.setX(xSpring);
constr.setY(ySpring);
// Hook buttons together with springs
for(int i = 1 ; i < buttons.length ; ++i) {
constr = layout.getConstraints(buttons[i]);
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
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createWindow();
}
});
}
}
Search WWH ::




Custom Search