Thursday, 15 August 2013

swing - BMI calculator in GUI Java; -



swing - BMI calculator in GUI Java; -

i wanted create bmi calculator using gui in java. i'm new gui , java. calculator suppose display bmi advice , time , date. however, bmi display while rest can't.. have been searching online on how display results if else status online no avail. code;

public class bmi extends jframe implements actionlistener { private static final jbutton jbutton = null; private jframe frame; private jpanel panel; private jlabel heightlabel, weightlabel, bmilabel; private jtextfield height, weight, result; private jbutton calculate; string height, weight; double number1, number2, bmi; static string output = "results"; static int jopicon = joptionpane.question_message; boolean bflag = true; //state, true means no exception public bmi() { frame = new jframe("bmi calculator"); frame.setdefaultcloseoperation(jframe.exit_on_close); //create labels height , weight textfields heightlabel = new jlabel("your height in meters:"); weightlabel = new jlabel("your weight in kilograms: "); //create "this bmi" label bmilabel = new jlabel("your bmi "); //create result label hold bmi value result = new jtextfield(""); //create jtextfield hold person's height in kilograms height = new jtextfield(1); //create jtextfield hold person's weight in metres weight = new jtextfield(1); calculate = new jbutton("calculate bmi"); //set jpanel go on jframe panel = new jpanel(); panel.add(heightlabel); panel.add(height); //add weight label , weight textfield panel panel.add(weightlabel); panel.add(weight); //add button panel panel.add(bmilabel); //add label holds result panel panel.add(result); //add panel frame panel.add(calculate); //add bmi label panel frame.getcontentpane().add(panel); jpanel p1 = new jpanel(); panel.setlayout(new gridlayout(4, 1)); add(p1, borderlayout.south); calculate.addactionlistener(this); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setsize(400, 400); frame.setvisible(true);//important must have@! if not gui not display } public string getdatetime() { dateformat dateformat = new simpledateformat("yyyy/mm/dd hh:mm:ss"); date date = new date(); homecoming dateformat.format(date); } public void calculatebmi(double number1, double number2) { seek { bmi = number2 / ((number1) * 2); } grab (numberformatexception nfe) { output += "\n\n whoa! input error: must come in valid integers";//if exception comes out, prepare error message jopicon = joptionpane.error_message; } } public void calculate() { height = height.gettext(); weight = weight.gettext();//declare height string jtext height seek { number1 = double.parsedouble(height); number2 = double.parsedouble(weight);//exception may come out calculatebmi(number1, number2); } { if (bmi >= 27.5) { output += "\n\n you're in high risk zone(unhealthy). please start losing weight! it's must!"; } else if (bmi <= 23 || bmi < 27.4) { output += "\n\n you're in moderate risk zone. please start going on diet , lose weight"; } else if (bmi <= 18.5 || bmi < 22.9) { output += " you're in low risk zone(healthy). can maintain way! ^^"; } else if (bmi < 18.4) { output += "\n\n need start eating more. skinny , unhealthy body"; } } } public static void main(string[] args) { bmi bmi = new bmi(); } @override public void actionperformed(actionevent e) { //call calculate this.calculate(); result.settext("" + bmi); // todo auto-generated method stub } }

i this:

@override public void actionperformed(actionevent arg0) { calculate(); result.settext(output); }

the result should of type jtextarea line wrap should set:

result = new jtextarea(); result.setlinewrap(true);

and should think implementing actionlistener in anonymous class rather straight in frame class (especially when add together other buttons or checkboxes)

calculate.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent ev) { calculate(); result.settext(output); } });

java swing user-interface layout

No comments:

Post a Comment