java - Text boxes squished in Swing layout -


i have frame opens when click file>new user, text fields squished together.

i'm trying have them stacked vertically use new gridlayout(3, 2) layoutmanager. however, stuff new window way @ bottom.

import java.awt.*; import javax.swing.*; import java.awt.event.*; public class app extends jframe implements actionlistener {     private final int width = 300;     private final int height = 550;     private int control = 0;     string[] username = new string[10];     string[] pass = new string[10];     private string tempname;     private string temppass;      container con = getcontentpane();     jpanel panel = new jpanel();     private jtextfield name = new jtextfield();     private jpasswordfield password = new jpasswordfield();      jmenubar mainbar = new jmenubar();     jmenu menu1 = new jmenu("file");     jmenu menu2 = new jmenu("edit");     jmenuitem newuser = new jmenuitem("new user");      jbutton but1 = new jbutton("log in");     jbutton but2 = new jbutton("test");      jlabel error = new jlabel("login info not corret\n or user not registered.");     jlabel success = new jlabel("success!");      /////////////stuff dialog///////////////////     jpanel panel2 = new jpanel();     jtextfield newmodaluser = new jtextfield();     jpasswordfield newmodalpass = new jpasswordfield();     jpasswordfield newmodalpasscon = new jpasswordfield();     jbutton register = new jbutton("register");     ////////////////////////////////////////////////     public static void main(string[] args)     {        app frame = new app();     }      public app()     {        //just settin stuff        super("for bold");        setsize(width, height);         setdefaultcloseoperation(jframe.exit_on_close);        setlocationrelativeto(null);        //setresizable(false);        setvisible(true);         //add menubar        setjmenubar(mainbar);        mainbar.add(menu1);        menu1.add(newuser);         //background of main jframe        setcontentpane(new jlabel(new imageicon("//users//ryanchampin//desktop//gui app//image.png")));         //test names in arrays         username[0] = "ryan";        pass[0] = "test";         //main stuff in middle        //panel.setbackground(color.red);        panel.setsize(300,300);        panel.add(name);        panel.add(password);        panel.setlayout(new boxlayout(panel, boxlayout.page_axis ));        panel.add(but1);         panel.add(but2);         add(panel,new gridbagconstraints());        setlayout(new gridbaglayout());         //assign action listener        but1.addactionlistener(this);        newuser.addactionlistener(this);         register.addactionlistener(this);    }      public void actionperformed(actionevent e)    {       object source = e.getsource();       tempname = name.gettext();       temppass = password.gettext();        if(source == but1)       {             for(int x = 0; x < username.length; x++)             {                 if(tempname.equalsignorecase(username[x]) && temppass.equals(pass[x]))                 {                                    //display success jlabel                     add(success);                     system.out.println("success");                     break;                 }                 else                  {                     success.settext(null);                     add(error);                     name.settext(null);                     password.settext(null);                 }                }        }        else           if(source == newuser)           {                panel.setvisible(false);                setlayout(new gridlayout(3,2));                add(panel2);                panel2.add(newmodaluser);                panel2.add(newmodalpass);                panel2.add(newmodalpasscon);                panel2.add(register);           }           else if(source == register)                system.out.println("yay worked");    } } 

  • avoid using setsize(...) or setpreferredsize(...) if possible.
  • instead let components , layout managers set own sizes.
  • use cardlayout swap views instead of you're doing. if this, cardlayout size container fit "cards" has been given.
  • don't forget call pack() on gui after adding components
  • don't forget call setvisible(true) after adding components , after calling pack.
  • when creating new jtextfields , jpasswordfields, pass in int number of columns constructors.

edit
ask:

whats pack() used for?

the pack() method tells gui have layout managers of constituent containers lay out components, , set best size of gui after every component has been placed.


Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -