Sample Source Code
public class AttachLayoutSample implements AttachConstants {
public static void main(String[] args) {
Frame frame = new Frame("AttachLayout Sample");
frame.setBackground(Color.lightGray);
frame.setFont(new Font("HELVETICA", Font.PLAIN, 10));
AttachLayout attachLayout = new AttachLayout();
Panel panel = new Panel(attachLayout);
Label title = new Label("This is a sample");
Label label_1 = new Label("Label..1");
Label label_2 = new Label("Label....2");
Label label_3 = new Label("Label......3");
Button button_1 = new Button("Button..1");
Button button_2 = new Button("Button....2");
Button button_3 = new Button("Button......3");
Button button_yes = new Button("Yes");
Button button_no = new Button("No");
Button button_maybe = new Button("Maybe");
Button button_ok = new Button("OK");
Button button_cancel = new Button("Cancel");
Button button_close = new Button("Close");
TextArea textarea = new TextArea("This sample uses\n"
+ "only one panel\n"
+ "and one layout manager.");
AttachLayout.Constraints cts = new AttachLayout.Constraints();
/*
* Define column_1
*/
cts.attachToBorder(LEFT, FIXED, 5);
cts.attachToBorder(RIGHT, ELASTIC, 120);
attachLayout.defineColumn("column_1", cts);
/*
* Define constraints for label in top-left corner
*/
cts.attachToBorder(TOP, FIXED, 5);
cts.addToColumn("column_1", FIXED, 0, ELASTIC, 0);
cts.attachNone(BOTTOM);
panel.add(title, cts);
/*
* Define constraints for label 1
*/
cts.attachToComponent(TOP, FIXED, 10, title, BOTTOM);
cts.attachToComponent(BOTTOM, ELASTIC, 10, button_yes, TOP);
// LEFT and RIGHT the same as defined for title: no need to redefine
panel.add(label_1, cts);
/*
* Define constraints for label 2
*/
cts.attachToComponent(TOP, FIXED, 5, label_1, BOTTOM);
// LEFT, RIGHT and BOTTOM: no need to redefine
panel.add(label_2, cts);
/*
* Define constraints for label 3
*/
cts.attachToComponent(TOP, FIXED, 5, label_2, BOTTOM);
// LEFT, RIGHT and BOTTOM: no need to redefine
panel.add(label_3, cts);
/*
* Define constraints for button 1: align bottom with label 1
*/
cts.attachToComponent(TOP, ELASTIC, 5, title, BOTTOM);
// cts.attachToComponents(LEFT, FIXED, 3, column_1, RIGHT); //deprecated
cts.attachToBox(LEFT, FIXED, 3, "column_1", RIGHT);
cts.attachToComponent(BOTTOM, FIXED, 0, label_1, BOTTOM);
cts.attachToBorder(RIGHT, ELASTIC, 120);
cts.setPreferredWidth(80);
panel.add(button_1, cts);
/*
* Define constraints for button 2: align bottom with label 2
*/
cts.attachToComponent(TOP, ELASTIC, 3, label_1, BOTTOM);
cts.attachToComponent(BOTTOM, FIXED, 0, label_2, BOTTOM);
// LEFT, RIGHT and preferred width: no need to redefine
panel.add(button_2, cts);
/*
* Define constraints for button 3: align bottom with label 3
*/
cts.attachToComponent(TOP, ELASTIC, 3, label_2, BOTTOM);
cts.attachToComponent(BOTTOM, FIXED, 0, label_3, BOTTOM);
// LEFT, RIGHT and preferred width: no need to redefine
panel.add(button_3, cts);
/*
* Define constraints for button ok:
* Center between left border and one third of the panel's width
*/
cts.attachToComponent(TOP, ELASTIC, 5, title, BOTTOM);
cts.attachToBorder(LEFT, ELASTIC, 5);
cts.attachToRelativePosition(RIGHT, ELASTIC, 2, 33);
cts.attachToBorder(BOTTOM, FIXED, 5);
// preferred width: no need to redefine
panel.add(button_ok, cts);
/*
* Define constraints for button cancel:
* Center between one third and two thirds of the panel's width
*/
cts.attachToRelativePosition(LEFT, ELASTIC, 3, 33);
cts.attachToRelativePosition(RIGHT, ELASTIC, 3, 67);
// TOP, BOTTOM and preferred width: no need to redefine
panel.add(button_cancel, cts);
/*
* Define constraints for button close:
* Center between two thirds of the panel's width and right edge
*/
cts.attachToRelativePosition(LEFT, ELASTIC, 2, 67);
cts.attachToBorder(RIGHT, ELASTIC, 5);
// TOP, BOTTOM and preferred width: no need to redefine
panel.add(button_close, cts);
/*
* Define constraints for button yes
* Center between left border and one third of the panel's width
*/
cts.attachToBorder(LEFT, FIXED, 5);
cts.attachToRelativePosition(RIGHT, FIXED, 2, 33);
cts.attachToComponent(BOTTOM, FIXED, 5, button_ok, TOP);
// TOP and preferred width: no need to redefine
panel.add(button_yes, cts);
/*
* Define constraints for button no:
* Center between one third and two thirds of the panel's width
*/
cts.attachToRelativePosition(LEFT, FIXED, 3, 33);
cts.attachToRelativePosition(RIGHT, FIXED, 3, 67);
// TOP, BOTTOM and preferred width: no need to redefine
panel.add(button_no, cts);
/*
* Define constraints for button maybe:
* Center between two thirds of the panel's width and right edge
*/
cts.attachToRelativePosition(LEFT, FIXED, 2, 67);
cts.attachToBorder(RIGHT, FIXED, 5);
// TOP, BOTTOM and preferred width: no need to redefine
panel.add(button_maybe, cts);
/*
* Define constraints for textarea
*/
cts.attachToBorder(TOP, FIXED, 5);
cts.attachToComponent(LEFT, FIXED, 10, button_1, RIGHT);
cts.attachToComponent(BOTTOM, FIXED, 10, button_yes, TOP);
cts.attachToBorder(RIGHT, FIXED, 5);
cts.setPreferredWidth(145);
cts.setPreferredHeight(50);
panel.add(textarea, cts);
/*
* Show it
*/
frame.add(panel);
frame.pack();
frame.show();
}
}