java - Buttons will not be displayed horizontal to each other. Only overlapped on top of each other -


i having issues printing buttons java swing project. class, suppose replicate gui. far, have been able fine. having issue buttons overlapping each other in same location opposed next each other horizontally. below image of how buttons being printed onto the panel.

so have 2 panels, 1 houses labels , text boxes (toppane) , 1 houses buttons, 5 in total (bottompane). trying 5 buttons print across bottom of gui , having hard time. feel missing simple.

-------------------------------------------------------------- |                 label [textfield]                          | |                 label [textfield]                          | |                 label [textfield]                          | |------------------------------------------------------------- |  [button] [button] [button] [button] [button]              | -------------------------------------------------------------- 

but this:

-------------------------------------------------------------- |                 label [textfield]                          | |                 label [textfield]                          | |                 label [textfield]                          | |------------------------------------------------------------- |              [        button's 12345       ]               | -------------------------------------------------------------- 

code:

package book;  import java.awt.borderlayout; import java.awt.color; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.jbutton; import javax.swing.jlabel; import javax.swing.jtextfield; import java.awt.gridbaglayout; import java.awt.gridbagconstraints;   /**  *  * @author kj4cc  */ public class book extends jframe  {     /**      * @param args command line arguments      */     public static void main(string[] args) {        book book = new book();         book.bookingui();     }     public static void  bookingui(){          //sets windows, , pane in ui          jframe frame = new jframe("ye old book store");         jpanel toppane = new jpanel(new gridbaglayout());         jpanel bottompane = new jpanel(new gridbaglayout());         gridbagconstraints c = new gridbagconstraints();         frame.setsize(1000, 600);         frame.setvisible(true);          //adds labels  window         jlabel num = new jlabel("enter number of items in order");         jlabel bookid = new jlabel("111111");         jlabel quantityitem = new jlabel("222222");         jlabel iteminfo = new jlabel("333zfgfsfg333");         jlabel subtotal = new jlabel("4444444");          //adding labels panel          c.anchor = gridbagconstraints.east;         c.weighty = 1;         c.gridx = 2;         c.gridy = 1;         toppane.add(num, c);         c.gridx = 2;         c.gridy = 2;         toppane.add(bookid, c);         c.gridx = 2;         c.gridy = 3;         toppane.add(quantityitem, c);         c.gridx = 2;         c.gridy = 4;         toppane.add(iteminfo,c);         c.gridx = 2;         c.gridy = 5;         toppane.add(subtotal,c);         bottompane.setbackground(color.green);         frame.add(toppane,borderlayout.east);         //adds textfields frame          jtextfield amount = new jtextfield();         jtextfield id = new jtextfield();         jtextfield quantity = new jtextfield();         jtextfield info = new jtextfield();         jtextfield total = new jtextfield();          //add textfield panel         c.ipadx = 230;         c.gridx = 3;         c.gridy= 1;         toppane.add(amount, c);         c.gridx = 3;         c.gridy = 2;         toppane.add(id, c);         c.gridx = 3;         c.gridy = 3;         toppane.add(info, c);         c.gridx = 3;         c.gridy = 4;         toppane.add(total, c);         c.gridx = 3;         c.gridy = 5;          toppane.add(quantity,c);      //setting buttons placed onto bottompanel      jbutton processitem = new jbutton("process item");     jbutton confirmitem = new jbutton("confirm item");     jbutton vieworder = new jbutton("view order");     jbutton finishorder = new jbutton("finish order ");     jbutton neworder = new jbutton("new order");     jbutton exit = new jbutton("exit");      //adding buttons pane.     gridbagconstraints b = new gridbagconstraints();     b.anchor = gridbagconstraints.northwest;     bottompane.add(processitem, c);     bottompane.add(confirmitem,c);     bottompane.add(vieworder, c);     bottompane.add(finishorder,c);     bottompane.add(neworder,c);      bottompane.add(exit, c);     bottompane.setbackground(color.blue);     frame.add(bottompane,borderlayout.south);     } } 

personally feel has layout manager using. don't know if using right application. have been using gridbaglayout, , have used school far.

your issue using same constraint c every 1 of new buttons:

bottompane.add(processitem, c); bottompane.add(confirmitem,c); bottompane.add(vieworder, c); bottompane.add(finishorder,c); bottompane.add(neworder,c); 

the last modification made c above when did:

c.gridx = 3; c.gridy = 5;  

and you're reusing same constraint 5 new buttons , adding them same grid location.

you'll need set constraints accordingly (e.g. set c's values, you've got stray unused b there, too) each one.


Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -