swing - Java Applet Layout Doesn't Appear As It Is Supposed To o_O -


i taking java class semester , working on take-home quiz. in create calculator of sorts supposed 1st image attachedcalculator_applet_intended

as can see base panel supposed divided 2 rows , 1 column. upper panel divided 3 rows , 2 columns. lower panel has 4 buttons calculations performed.

for quiz created 2 files, wholepanel.java , layoutpanel.java. here code wholepanel.java:

package layoutpanel;  import java.awt.borderlayout; import static java.awt.borderlayout.center; import java.awt.*; import java.awt.event.*; import javax.swing.*;   /**  *  * @author ross satchell  */  public class wholepanel extends jpanel{     public jtextfield textbox1, textbox2, resultbox;     public double num1, num2, result;     public jlabel label1, label2, label3;     public jbutton addbutton, subbutton, multipbutton, dividbutton;   public wholepanel(){                            // constructor     jpanel upperpanel, lowerpanel, subpanel;     upperpanel = new jpanel();   // panel top section     lowerpanel = new jpanel();   // panel lower section     subpanel = new jpanel();     // panel hold both upperpanel , lowerpanel     label1 = new jlabel("enter number 1:");     label2 = new jlabel("enter number 2:");     label3 = new jlabel("result:");     textbox1 = new jtextfield(10);     textbox2 = new jtextfield(10);     resultbox = new jtextfield(10);     addbutton  =new jbutton("+");     subbutton = new jbutton("-");     multipbutton = new jbutton("*");     dividbutton = new jbutton("/");        upperpanel.setlayout(new gridlayout(3,2));     upperpanel.add(label1);     upperpanel.add(textbox1);       // set layout ,      upperpanel.add(label2);         // components upperpanel     upperpanel.add(textbox2);     upperpanel.add(label3);     upperpanel.add(resultbox);      lowerpanel.setlayout(new flowlayout());     lowerpanel.add(addbutton);     lowerpanel.add(subbutton);           // set toplowerpanel      lowerpanel.add(multipbutton);        // components , layout     lowerpanel.add(dividbutton);      subpanel.setlayout(new borderlayout());     subpanel.add(upperpanel, borderlayout.north);     subpanel.add(lowerpanel, borderlayout.south);               // add each panel main subpanel     this.add(subpanel);      addbutton.addactionlistener(new buttonlistener());      // add button listeners     subbutton.addactionlistener(new buttonlistener());      // each of      multipbutton.addactionlistener(new buttonlistener());   // 4 buttons     dividbutton.addactionlistener(new buttonlistener());    // }   private class buttonlistener implements actionlistener{       public void actionperformed(actionevent e){         jbutton buttonx = (jbutton) e.getsource();         num1 = double.parsedouble(textbox1.gettext());  // read values in textbox1         num2 = double.parsedouble(textbox2.gettext());  // , textbox2          if (buttonx == addbutton){             result = num1 + num2;         }else if (buttonx == subbutton){                // performed required calculation             result = num1 - num2;         }else if (buttonx == multipbutton){             result = num1 * num2;         }else if (buttonx == dividbutton){             result = num1 / num2;         }         resultbox.settext("" + result);         }   //  end actionperformed() method      }   // end buttonlistener class  }       // end wholepanel class 

and here code layoutpanel.java

package layoutpanel;  import javax.swing.*; import java.awt.*; import java.awt.event.*;  public class layoutpanel extends japplet{   public void init(){  wholepanel wholepanel = new wholepanel(); getcontentpane().add(wholepanel); setsize(300,200); } 

when run applet appears thus: enter image description here
can see, appears if both toppanel , lowerpanel somehow both in top row of subpanel.

for curiosity's sake tried changing subpanel borderlayout , positioned toppanel north , lowerpanel south. when ran applet still appeared same!

i have pored on code trying see have done wrong, i'm stumped. i'm hoping fresh set of eyes see careless mistake have made.

any , suggestions welcome.

you have compound of issues...

jpanel default uses flowlayout, since never change layout manager wholepanel, that's keeps on using.

you should change use borderlayout

public wholepanel() {                            // constructor     setlayout(new borderlayout()); 

now, based on original layout, might tempted use gridbaglayout subpanel

subpanel.setlayout(new gridbaglayout()); gridbagconstraints gbc = new gridbagconstraints(); gbc.gridx = 0; gbc.gridy = 0; gbc.fill = gridbagconstraints.horizontal; subpanel.add(upperpanel, gbc); gbc.gridy = 1; gbc.weighty = 1; gbc.anchor = gridbagconstraints.south; subpanel.add(lowerpanel, gbc);               // add each panel main subpanel this.add(subpanel); 

which can result in like...

layout


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -