layout - Problems implementing GridBagLayout in Java -
i trying implement gridbaglayout in java sign in dialog have make program building. going clean google sign in. main issue having constraints have set using gridbagconstraints
not working. here dialog like.
and here code dialog trying implement.
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class login_dialog extends jdialog{ // setting required components sign in /** * */ private static final long serialversionuid = 1l; protected jlabel username_label = new jlabel("username"); protected jlabel password_label = new jlabel("username"); protected jtextfield username_field = new jtextfield(30); protected jtextfield password_field = new jtextfield(30); protected jbutton sign_in = new jbutton("sign in!"); public login_dialog() { setsize(600,400); settitle("sign in"); setlayout(new gridbaglayout()); gridbagconstraints c = new gridbagconstraints(); c.fill = gridbagconstraints.horizontal; c.gridx = 0; c.gridy = 0; add(username_label); c.fill = gridbagconstraints.horizontal; // c.weightx = 0.5; c.gridx = 1; c.gridy = 0; add(username_field); c.fill = gridbagconstraints.horizontal; c.weightx = 0.5; c.gridx = 0; c.gridy = 2; add(password_label); c.fill = gridbagconstraints.horizontal; c.weightx = 0.5; c.gridx = 0; c.gridy = 4; add(password_field); c.fill = gridbagconstraints.horizontal; c.weightx = 0.5; c.gridx = 0; c.gridy = 5; add(sign_in); setvisible(true); } }
update:
i have made changes , seems reaching desired result. issue centred , length of button wide. also, textfield , label bigger.
here updated gridbaglayout
//cusotmization of buttons dimension signd = sign_in.getpreferredsize(); signd.height = 50; signd.width = 30; sign_in.setpreferredsize(signd); gridbagconstraints c = new gridbagconstraints(); c.fill = gridbagconstraints.horizontal; c.gridx = 0; c.gridy = 0; add(username_label,c); c.fill = gridbagconstraints.horizontal; // c.weightx = 0.5; c.gridx = 0; c.gridy = 1; add(username_field,c); c.fill = gridbagconstraints.horizontal; // c.weightx = 0.5; c.gridx = 0; c.gridy = 2; add(password_label,c); c.fill = gridbagconstraints.horizontal; // c.weightx = 0.5; c.gridx = 0; c.gridy = 3; add(password_field,c); // // c.fill = gridbagconstraints.horizontal; // c.weightx = 0.5; c.gridx = 0; c.gridy = 5; // add(sign_in,c);
you have add constraint object in add()
example :
add(username_field, c);
where c
gridbagconstraints.
here full example :
package gui; import java.awt.dimension; import java.awt.eventqueue; import java.awt.gridbagconstraints; import java.awt.gridbaglayout; import java.awt.insets; import javax.swing.jbutton; import javax.swing.jdialog; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.jtextfield; import javax.swing.windowconstants; @suppresswarnings("serial") public class loginpanel extends jpanel { private static final dimension tfield_sz = new dimension(120, 20); public loginpanel() { super(new gridbaglayout()); final gridbagconstraints cst = new gridbagconstraints(); cst.insets = new insets(0, 5, 0, 5); cst.weightx = 0.5; cst.weighty = 0.5; cst.anchor = gridbagconstraints.line_start; // username cst.gridx = 0; cst.gridy = 0; this.add(new jlabel("username:"), cst); cst.gridy = 1; cst.gridwidth = 2; final jtextfield utfield = new jtextfield(); utfield.setminimumsize(tfield_sz); utfield.setpreferredsize(tfield_sz); this.add(utfield, cst); // password cst.gridwidth = 1; cst.gridy = 2; this.add(new jlabel("password:"), cst); cst.gridy = 3; cst.gridwidth = 2; final jtextfield pwtfield = new jtextfield(); pwtfield.setminimumsize(tfield_sz); pwtfield.setpreferredsize(tfield_sz); this.add(pwtfield, cst); // buttons cst.anchor = gridbagconstraints.center; cst.gridy = 4; cst.gridwidth = 1; this.add(new jbutton("sign in"), cst); cst.gridx = 1; this.add(new jbutton("sign up"), cst); } public static void main(final string[] args) { eventqueue.invokelater(new runnable() { @override public void run() { final jdialog mydialog = new jdialog(); mydialog.settitle("sign in"); mydialog.setsize(180, 170); mydialog.setcontentpane(new loginpanel()); mydialog.setdefaultcloseoperation(windowconstants.dispose_on_close); mydialog.setvisible(true); } }); } }
Comments
Post a Comment