java - JPanels appear to go off the frame? -


i working on assignment "of sorts" not school assignment. having said that, ideas great.

i using windowbuilder in eclipse , have created basic form. have used nested jpanel components on border layout create it. reason, panels appear though spilling on edges of jframe. have frame dimensions set (500, 400) , panels various sizes, none greater 400 wide.

code:

    package swingassignment;  import java.awt.dimension; import java.awt.eventqueue;  import javax.swing.jframe;  import com.jgoodies.forms.layout.formlayout; import com.jgoodies.forms.layout.columnspec; import com.jgoodies.forms.layout.rowspec;  import java.awt.gridbaglayout;  import javax.swing.boxlayout;  import net.miginfocom.swing.miglayout;  import java.awt.borderlayout;  import javax.swing.jpanel;  import java.awt.flowlayout;  import javax.swing.borderfactory; import javax.swing.jcombobox; import javax.swing.jtextfield; import javax.swing.jtextarea;  import java.awt.component; import java.awt.gridbagconstraints; import java.awt.insets;  import com.jgoodies.forms.factories.formfactory;  import java.awt.gridlayout;  import javax.swing.jlabel; import javax.swing.border.bevelborder; import javax.swing.swingconstants;  public class swing_assignemnt {      private jframe frmwindowbuilderassignment;     private jpanel center_panel;     private jtextarea textarea;     private jpanel panel_1;     private jpanel panel;     private jtextfield textfield;     private jpanel panel_2;     private jtextarea txtrtextarea_0;     private jtextarea txtrtextarea_1;     private jpanel panel_3;     private jtextarea txtrtextareasouth;     private jlabel lblnewlabel;      /**      * launch application.      */     public static void main(string[] args) {         eventqueue.invokelater(new runnable() {             public void run() {                 try {                     swing_assignemnt window = new swing_assignemnt();                     window.frmwindowbuilderassignment.setvisible(true);                 } catch (exception e) {                     e.printstacktrace();                 }             }         });     }      /**      * create application.      */     public swing_assignemnt() {         initialize();     }      /**      * initialize contents of frame.      */     private void initialize() {         frmwindowbuilderassignment = new jframe();         frmwindowbuilderassignment.settitle("window builder assignment");         //frmwindowbuilderassignment.setbounds(500, 500, 650, 600);         frmwindowbuilderassignment.setdefaultcloseoperation(jframe.exit_on_close);         frmwindowbuilderassignment.getcontentpane().setlayout(new borderlayout(0, 0));         frmwindowbuilderassignment.setvisible(true);         frmwindowbuilderassignment.setsize(394, 500);         panel.setborder(borderfactory.createemptyborder(10, 10, 10, 10));         //frmwindowbuilderassignment.pack();         //frmwindowbuilderassignment.pack();         frmwindowbuilderassignment.setvisible( true );          panel = new jpanel();         panel.setborder(new bevelborder(bevelborder.lowered, null, null, null, null));         frmwindowbuilderassignment.getcontentpane().add(panel, borderlayout.north);         panel.setpreferredsize(new dimension(200, 40));         panel.setlayout(new flowlayout(flowlayout.left, 5, 5));         //panel.setbounds(20, 10, 200, 400);         panel.setvisible(true);          jcombobox combobox = new jcombobox();         panel.add(combobox);         combobox.setpreferredsize(new dimension(125, 20));         combobox.setvisible(true);          textfield = new jtextfield();         panel.add(textfield);         textfield.setcolumns(10);         textfield.setvisible(true);          panel_2 = new jpanel();         panel_2.setborder(new bevelborder(bevelborder.lowered, null, null, null, null));         frmwindowbuilderassignment.getcontentpane().add(panel_2, borderlayout.center);         panel_2.setlayout(new gridlayout(1, 2, 2, 2));         panel_2.setpreferredsize(new dimension(200, 400));         panel_2.setvisible(true);           txtrtextarea_0 = new jtextarea();         txtrtextarea_0.settext("textarea_0");         panel_2.add(txtrtextarea_0);         txtrtextarea_0.setpreferredsize(new dimension(50, 30));         txtrtextarea_0.setvisible(true);          txtrtextarea_1 = new jtextarea();         txtrtextarea_1.settext("textarea_1");         panel_2.add(txtrtextarea_1);         txtrtextarea_1.setvisible(true);         txtrtextarea_1.setpreferredsize(new dimension(50, 30));          panel_3 = new jpanel();         panel_3.setborder(new bevelborder(bevelborder.lowered, null, null, null, null));         frmwindowbuilderassignment.getcontentpane().add(panel_3, borderlayout.south);         panel_3.setlayout(new gridlayout(2, 1, 2, 2));         panel_3.setvisible(true);          txtrtextareasouth = new jtextarea();         txtrtextareasouth.settext("textarea_south");         panel_3.add(txtrtextareasouth);         txtrtextareasouth.setvisible(true);         txtrtextareasouth.setpreferredsize(new dimension(200, 150));          lblnewlabel = new jlabel("status label");         panel_3.add(lblnewlabel);         lblnewlabel.sethorizontalalignment(swingconstants.center);         lblnewlabel.setpreferredsize(new dimension(200, 20));         } } 

what looks like:

window screen shot

after code changes, have, not sure how resize label @ bottom. needs 15 tall.

enter image description here

don't use setbounds() or setpreferredsize(). each swing component should determine own preferred size , layout manager position components based on rules of layout manager.

don't use setvisible(true) on swing components (except jframe). default swing components visible.

you should add components frame before using:

frame.pack() frame.setvisible( true ); 

Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -