java - How to set a proper layout -
i want set layout following: 1 column , 5 rows , in center. tried using gridlayout(5,0)
remains in left, suggestions how make center?
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.jpanel; public class testswinglisteners1 { private static int cnt1; private static int cnt2; public static void main(string[] args) { jframe fr1 = new jframe("swing window"); container cp; jbutton bt1; jbutton bt2; cnt1 = 0; cnt2 = 0; final string scr = null; final string wnr = null; jbutton btok, btcancel; fr1.setdefaultcloseoperation(jframe.exit_on_close); fr1.setsize(200, 200); fr1.setresizable(true); cp = fr1.getcontentpane(); cp.setlayout(new gridlayout(5,0)); // cp.setlayout(new boxlayout(boxlayout.y_axis)); btok = new jbutton("ac milan"); btcancel = new jbutton("real madrid"); final jlabel lbl1 = new jlabel("result: " + cnt1 + "x" + cnt2); final jlabel lbl2 = new jlabel("last scorer: " + scr); final jlabel lbl3 = new jlabel("winner: " + wnr); cp.add(btok); cp.add(btcancel); cp.add(lbl1); cp.add(lbl2); cp.add(lbl3); fr1.add(cp, borderlayout.center); cp.setlayout(new boxlayout(lbl1, boxlayout.line_axis)); //lbl1.settext(displaytext); btok.addactionlistener(new actionlistener(){ public void actionperformed(actionevent ae) { cnt1++; lbl1.settext("result: " + cnt1 + "x" + cnt2); lbl2.settext("last scorer: ac milan"); if(cnt1>cnt2){ lbl3.settext("winner: ac milan"); } else if(cnt1<cnt2){ lbl3.settext("winner: real madrid"); } else if(cnt1 == cnt2){ lbl3.settext("winner: draw"); } } }); btcancel.addactionlistener(new actionlistener(){ public void actionperformed(actionevent ae) { cnt2++; lbl1.settext("result: " + cnt1 + "x" + cnt2); lbl2.settext("last scorer: real madrid"); if(cnt1>cnt2){ lbl3.settext("winner: ac milan"); } else if(cnt1<cnt2){ lbl3.settext("winner: real madrid"); } else if(cnt1 == cnt2){ lbl3.settext("winner: draw"); } } }); fr1.show(); } }
gridlayout grid = new gridlayout(5, 0); cp.setlayout(grid); ... grid.addcomponent(btok, 0, 0); grid.setcomponentalignment(btok, alignment.middle_center); ...
Comments
Post a Comment