java - Best way to pass values from JTextField into a method? -


currently working on bmr/tdee calculator. calculator consists of taking users age, gender, weight , height, , uses information calculate bmr , tdee value. i've created gui this, , final step actual calculations. (some minor changes left i.e. changing height panel thats irrelevant question).

what best way pass instance variables static methods? jtextfield's instance variables within class, , have 2 methods calculate bmr , tdee respectively (static methods).

code posted below.

the variables need use pretty textfield ones, , these need passed calcbmr() , calctdee(). initial idea make of these variables static , use gettext() , plug these values formula i'm using, goes along lines of:

10 x weight (kg) + 6.25 x height (cm) - 5 x age (y) + 5.

does way sound feasible or there better way given code? advice on how efficiently appreciated.

public class bmrcalcv2 extends jframe {      // frames , main panels     static jframe mainframe;     static jpanel mainpanel;     static jpanel combinedgahwpanel; // combines genderpanel, agepanel, heightpanel, weightpanel -                                      // borderlayout, whereas                                      // gender/agepanel flowlayouts.     static jpanel weightheightpanel; // combines weight , height panel out flowlayout panel, combined above borderlayout panel      // image components     static jpanel imgpanel;     private jlabel imglabel;     private jlabel activitylevelhelp;      // menu-bar components     static jmenubar menubar;     static jmenu savemenu, optionmenu, helpmenu;      // age components     static jpanel agepanel;     private jlabel agelabel;     private jlabel yearslabel;     private jtextfield agetextfield;      // gender components     static jpanel genderpanel;     private jlabel genderlabel;     private jradiobutton gendermale;     private jradiobutton genderfemale;       // height components     static jpanel heightpanel;     private jlabel heightlabel;     private jtextfield heightcmfield;     private jlabel heightftlabel;     private jlabel heightinchlabel;     private jtextfield heightftfield;     private jtextfield heightinchfield;     private jtogglebutton cmbutton;     private jtogglebutton feetbutton;       // weight components     static jpanel weightpanel;     private jlabel weightlabel;     private jtextfield weightfield;     private jtogglebutton kgbutton;     private jtogglebutton lbbutton;       // tdee , bmr components     static jpanel tdeepanel;     static jpanel tdeebmrpanel;     static jpanel activitylevelpanel;     static jpanel bmrtdeevaluespanel;     static jpanel bmrvaluepanel;     static jpanel tdeevaluepanel;     private jlabel tdeequestionlabel;     private jlabel activitylevellabel;     private jcombobox activitylevelbox;     private jradiobutton tdeeyes;     private jradiobutton tdeeno;     private jlabel bmrlabel;     private jlabel tdeelabel;     private jbutton calculate;         // default values gender/weight/height , other variables      string[] activitylevels = {"sedentary", "lightly active", "moderately active", "very active", "extra active"};     string genderselection = "m";        string weightselection = "kg";     string heightselection = "cm";     string tdeeselection = "no";      string agevalue;     string weightvalue;     string heightvalue;     static string bmrvalue = "n/a";     static string tdeevalue = "n/a";        public bmrcalcv2(string title) {          // main jframe         settitle("bmr/tdee calculator");         mainpanel = new jpanel();          // jpanel declarations         menubar = new jmenubar();         imgpanel = new jpanel();         agepanel = new jpanel();         genderpanel = new jpanel();         heightpanel = new jpanel();         weightpanel = new jpanel();         weightheightpanel = new jpanel(new borderlayout());         combinedgahwpanel = new jpanel(new borderlayout()); // create new panel used combine                                                             // genderpanel, agepanel, weightpanel, heightpanel below         tdeebmrpanel = new jpanel(new borderlayout());         tdeepanel = new jpanel();         activitylevelpanel = new jpanel();         bmrtdeevaluespanel = new jpanel(new borderlayout());         bmrvaluepanel = new jpanel();         tdeevaluepanel = new jpanel();            // image panel declaration         imglabel = new jlabel(new imageicon("filesrc//mainlogo.png"));         activitylevelhelp = new jlabel(new imageicon("filesrc//question-mark.png"));         imgpanel.add(imglabel);          // jpanel layout managers         agepanel.setlayout(new flowlayout(flowlayout.center, 5, 5));         genderpanel.setlayout(new flowlayout(flowlayout.center, 5, 5));           // menu jcomponents         savemenu = new jmenu("save");         optionmenu = new jmenu("options");         helpmenu = new jmenu("help");         menubar.add(savemenu);         menubar.add(optionmenu);         menubar.add(helpmenu);          // age jcomponents         agelabel = new jlabel("age:");         yearslabel = new jlabel("<html><i>years</i><html>");         agetextfield = new jtextfield(5);         agepanel.add(agelabel);         agepanel.add(agetextfield);         agepanel.add(yearslabel);          // gender jcomponents         genderlabel = new jlabel("gender:");         gendermale = new jradiobutton("male", true);         genderfemale = new jradiobutton("female");            genderpanel.add(genderlabel);         genderpanel.add(gendermale);         genderpanel.add(genderfemale);           buttongroup gendergroup = new buttongroup(); // groups male , female radio buttons 1 can selected         gendergroup.add(gendermale);         gendergroup.add(genderfemale);           gendermale.addactionlistener(new actionlistener(){             public void actionperformed(actionevent e) {               string genderselection = "m";                }          });          genderfemale.addactionlistener(new actionlistener(){             public void actionperformed(actionevent e) {               string genderselection = "f";             }          });          // height jcomponents         heightlabel = new jlabel("height:");         heightcmfield = new jtextfield(4);         heightftfield = new jtextfield(3);         heightftlabel = new jlabel("ft");         heightinchlabel = new jlabel("inch");         heightinchfield = new jtextfield(3);         cmbutton = new jtogglebutton("cm", true);         feetbutton = new jtogglebutton("feet");         heightpanel.add(heightlabel);           buttongroup heightgroup = new buttongroup();         heightgroup.add(cmbutton);         heightgroup.add(feetbutton);           heightpanel.add(heightcmfield);         heightpanel.add(heightftfield);         heightpanel.add(heightftlabel);         heightpanel.add(heightinchfield);         heightpanel.add(heightinchlabel);         heightpanel.add(cmbutton);         heightpanel.add(feetbutton);         heightftfield.setvisible(false);         heightftlabel.setvisible(false);         heightinchfield.setvisible(false);         heightinchlabel.setvisible(false);           cmbutton.addactionlistener(new actionlistener(){             public void actionperformed(actionevent e) {                   heightselection = "cm";                   heightinchfield.setvisible(false);                   heightftfield.setvisible(false);                   heightftlabel.setvisible(false);                   heightinchlabel.setvisible(false);                   heightcmfield.setvisible(true);                    weightpanel.revalidate();                   weightpanel.repaint();                 }             });          feetbutton.addactionlistener(new actionlistener(){             public void actionperformed(actionevent e) {                   heightselection = "feet";                   heightinchfield.setvisible(true);                   heightftfield.setvisible(true);                   heightftlabel.setvisible(true);                   heightinchlabel.setvisible(true);                   heightcmfield.setvisible(false);                    weightpanel.revalidate();                   weightpanel.repaint();                  }             });           // weight jcomponents         weightlabel = new jlabel("weight:");         weightfield = new jtextfield(4);         kgbutton = new jtogglebutton("kg", true);         lbbutton = new jtogglebutton("lbs");            weightpanel.add(weightlabel);         weightpanel.add(weightfield);         weightpanel.add(kgbutton);         weightpanel.add(lbbutton);            buttongroup weightgroup = new buttongroup();         weightgroup.add(kgbutton);         weightgroup.add(lbbutton);             kgbutton.addactionlistener(new actionlistener(){             public void actionperformed(actionevent e) {                 weightselection = "kg";              }         });          lbbutton.addactionlistener(new actionlistener(){             public void actionperformed(actionevent e) {                 weightselection = "lb";              }         });           // tdee jcomponents         tdeequestionlabel = new jlabel("calculate tdee also?");         tdeeyes = new jradiobutton("yes");         tdeeno = new jradiobutton("no", true);          buttongroup tdeebutton = new buttongroup();         tdeebutton.add(tdeeyes);         tdeebutton.add(tdeeno);          tdeepanel.add(tdeequestionlabel);         tdeepanel.add(tdeeyes);         tdeepanel.add(tdeeno);           // activitylevel jcomponents          activitylevellabel = new jlabel("activity level: ");         activitylevelbox = new jcombobox(activitylevels);         activitylevelbox.setselectedindex(0);          activitylevelpanel.add(activitylevellabel);         activitylevelpanel.add(activitylevelbox);         activitylevelpanel.add(activitylevelhelp);         activitylevelbox.setenabled(false);          activitylevelhelp                 .settooltiptext("<html><b>sedentary:</b> little or no exercise, deskjob<<br /><b>lightly active:</b> little exercise/sports 1-3 days/week<br /><b>moderately active:</b> moderate exercise/sports 3-5 days/week<br /><b>very active:</b> hard exercise or sports 6-7 days/week<br /><b>extra active:</b> hard daily exercise or sports & physical labor job </html>");          tdeeyes.addactionlistener(new actionlistener(){             public void actionperformed(actionevent e) {                   tdeeselection = "yes";                   activitylevelbox.setenabled(true);                 }             });          tdeeno.addactionlistener(new actionlistener(){             public void actionperformed(actionevent e) {                   tdeeselection = "no";                   activitylevelbox.setenabled(false);                 }             });            // tdee , bmr value components          bmrvalue = calcbmr();          bmrlabel = new jlabel("<html><br /><br /><font size=4>you have <i><font color=red>bmr</font></i> of: " + bmrvalue + "<font></html>");         tdeelabel = new jlabel("<html><br /><font size=4>you have <i><font color=red>tdee</font></i> of: " + tdeevalue + "<font></html>");         calculate = new jbutton("calculate");          bmrtdeevaluespanel.add(calculate, borderlayout.north);         bmrtdeevaluespanel.add(bmrlabel, borderlayout.center);         bmrtdeevaluespanel.add(tdeelabel, borderlayout.south);          // debugging panels buttons (remove when complete)         calculate.addactionlistener(new actionlistener(){             public void actionperformed(actionevent e) {                   system.out.println("male:" + gendermale.isselected());                   system.out.println("female:" + genderfemale.isselected() + "\n");                   system.out.println("kg:" + kgbutton.isselected());                   system.out.println("lb:" + lbbutton.isselected() + "\n");                   system.out.println("cm:" + cmbutton.isselected());                   system.out.println("feet:" + feetbutton.isselected() + "\n");                   system.out.println("tdee yes:" + tdeeyes.isselected());                   system.out.println("tdee no:" + tdeeno.isselected());                   system.out.println("-------------------------------------");                  }             });             // adding sub jpanels main jpanel         mainpanel.add(imgpanel);          combinedgahwpanel.add(agepanel, borderlayout.north); // combine genderpanel , agepanel (which both flowlayouts)                                                              // single borderlayout panel agepanel given northern spot ,                                                              // genderpanel given center spot in panel         weightheightpanel.add(weightpanel, borderlayout.north); // nested borderlayouts, weightheightpanel borderlayout nested                                                                  // southern position of combinedgahw border layout.         weightheightpanel.add(heightpanel, borderlayout.center);         weightheightpanel.add(tdeebmrpanel, borderlayout.south);             combinedgahwpanel.add(genderpanel, borderlayout.center);         combinedgahwpanel.add(weightheightpanel, borderlayout.south);           mainpanel.add(combinedgahwpanel);           // adding tdeebmrpanel         tdeebmrpanel.add(tdeepanel, borderlayout.north);         tdeebmrpanel.add(activitylevelpanel, borderlayout.center);           tdeebmrpanel.add(bmrtdeevaluespanel, borderlayout.south);           // adding main jpanel , menubar jframe         setjmenubar(menubar);         add(mainpanel);     }        public static string calcbmr() {           return bmrvalue;     }       public static string calctdee() {          return tdeevalue;     }       public static void main(string[] args) {          bmrcalcv2 gui = new bmrcalcv2("bmr/tdee calculator");          gui.setdefaultcloseoperation(jframe.exit_on_close);         gui.setvisible(true);         gui.setsize(330, 500);         gui.setresizable(false);      } } 

thanks.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -