java - How to set TableLayout's cells vertical & horizontal alignments -
is there way set alignments in tablelayout's cells?
if want component in cell set example right bottom?
this example of using tablelayoutconstraints:
import javax.swing.jbutton; import javax.swing.jframe; import layout.tablelayout; import layout.tablelayoutconstraints; public class tablelayoutcellalignmentexample { public static void main(string args[]) { jframe frame = new jframe("example of tablelayout"); frame.setsize(700, 400); double size[][] = {{200,200,200}, // columns {100,100,100}}; // rows frame.setlayout(new tablelayout(size)); string[] labels = { "should top left", "should top center", "should top right", "should middle left", "should middle center", "should middle right", "should bottom left", "should bottom center", "should bottom right", }; frame.add(new jbutton(labels[0]), new tablelayoutconstraints(0, 0, 0, 0, tablelayout.left, tablelayout.top)); frame.add(new jbutton(labels[1]), new tablelayoutconstraints(1, 0, 1, 0, tablelayout.center, tablelayout.top)); frame.add(new jbutton(labels[2]), new tablelayoutconstraints(2, 0, 2, 0, tablelayout.right, tablelayout.top)); frame.add(new jbutton(labels[3]), new tablelayoutconstraints(0, 1, 0, 1, tablelayout.left, tablelayout.center)); frame.add(new jbutton(labels[4]), new tablelayoutconstraints(1, 1, 1, 1, tablelayout.center, tablelayout.center)); frame.add(new jbutton(labels[5]), new tablelayoutconstraints(2, 1, 2, 1, tablelayout.right, tablelayout.center)); frame.add(new jbutton(labels[6]), new tablelayoutconstraints(0, 2, 0, 2, tablelayout.left, tablelayout.bottom)); frame.add(new jbutton(labels[7]), new tablelayoutconstraints(1, 2, 1, 2, tablelayout.center, tablelayout.bottom)); frame.add(new jbutton(labels[8]), new tablelayoutconstraints(2, 2, 2, 2, tablelayout.right, tablelayout.bottom)); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setvisible(true); frame.pack(); } }
result:
Comments
Post a Comment