java - MigLayout, aligning all components at center of JFrame -
how can align components @ jpanel @ center of jframe.
here codes , got
i want this
mainframe
import javax.swing.jframe; import javax.swing.jpanel; public class mainframe extends jframe { private static mainframe instance = new mainframe(); public static mainframe getinstance() { return instance; } public static void switchtopanel(jpanel p) { getinstance().setcontentpane(p); getinstance().validate(); } private mainframe() { settitle("favmovies"); setsize(400, 400); setlocationrelativeto(null); setvisible(true); setdefaultcloseoperation(jframe.exit_on_close); } public static void main(string[] args) { mainframe.switchtopanel(new loginpanel()); } }
loginpanel
import javax.swing.*; import net.miginfocom.swing.miglayout; public class loginpanel extends jpanel { // properties private jtextfield inputusername; private jpasswordfield inputpassword; // constructor public loginpanel() { setsize(400,400); // components inputusername = new jtextfield(10); inputpassword = new jpasswordfield(10); jbutton loginbutton = new jbutton("login"); jbutton createbutton = new jbutton( "create user"); setlayout(new miglayout()); // first row add(new jlabel( "username: ")); add(inputusername, "wrap 5"); // second row add(new jlabel( "password: ")); add(inputpassword, "wrap 10"); // final row add(loginbutton); add(createbutton); } }
i hope explain :( hope explain :( hope explain :( hope explain :(
the gridbaglayout
center components automatically. set layout of frame use gridbaglayout
, add login panel frame:
//getinstance().setcontentpane(p); getinstance().setlayout( new gridbaglayout() ); getinstance().add(p);
edit:
however, when resize it, able see loginpanel @ center.
it looks adding component visible gui. need manually invoke layout manager , repaint component.
getinstance().validate(); getinstance().revalidate(); getinstance().repaint();
the structure of code complicated. should read swing tutorial better examples , won't have problem. maybe start labeldemo
how use labels.
if need ability swap panels, take @ section swing tutorial on how use cardlayout
, designed purpose.
Comments
Post a Comment