//When ever I add TextFields the frame disappears.line 73-76,108-111;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class fitness extends JFrame {

    private double height=13.5;
    private double weight=45.3;
    private int age=20;
    private String sex="Male";


    public fitness(int x){
     this.age=x;

    }
    public fitness (double wei,double hei){
        this.weight=wei;
        this.height=hei;
    }

    public fitness (double height,double weight,int age,String s){
        this.height=height;
        this.weight=weight;
        this.age=age;
        this.sex=s;
    }
    public String getsex(){
        return sex;
    }
    public void setsex(String sex){
        this.sex=sex;
    }
    public double getheight(){
        return height;
    }
    public void setheight(double height){
        this.height=height;
    }
    public double getweight(){
        return weight;
    }
    public void setweight(double weight){
        this.weight=weight;

    }
    public int getage(){
        return age;
    }
    public void setage(int age){
        this.age=age;
    }


private JButton b1,b2,b3,b4;
private JLabel callab;
private JFrame caluframe;
private JPanel calupanel;
//private JTextField txtheight,txtweight,txtage,txtgender;

public fitness(){
gui();
}
public void gui(){
caluframe =new JFrame("Fitness Pal");
caluframe.setVisible(true);
caluframe.setSize(600,300);
caluframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//txtheight=new JTextField();
//txtweight=new JTextField();
//txtage=new JTextField();
//txtgender=new JTextField();


calupanel=new JPanel(new GridBagLayout());
calupanel.setBackground(Color.GREEN);


GridBagConstraints calc = new GridBagConstraints();
GridBagConstraints calc1 = new GridBagConstraints();
GridBagConstraints calc2 = new GridBagConstraints();
GridBagConstraints calc3 = new GridBagConstraints();
GridBagConstraints lab = new GridBagConstraints();


calc.gridx =2;
calc.gridy =3;
calc2.gridx =2;
calc2.gridy =6;
calc1.gridx =2;
calc1.gridy =8;
calc3.gridx =2;
calc3.gridy =10;
lab.gridx=2;
lab.gridy=2;


b1=new JButton("Calories Burner with Activity ");
b2=new JButton("Calories Burner with Heart Rate");
b3=new JButton("Body Mass Index Calculator");
b4=new JButton("Maximum Heart Rate Calculator");

callab=new JLabel("Welcome,What would you like to do?");
//calupanel.add(txtage);
//calupanel.add(txtweight);
//calupanel.add(txtheight);
//calupanel.add(txtgender);
calupanel.add(b1,calc);
calupanel.add(b2,calc2);
calupanel.add(b3,calc1);
calupanel.add(b4,calc3);
calupanel.add(callab,lab);
caluframe.add(calupanel);

b1.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){
JFrame cr = new JFrame();
cr.setVisible(true);
cr.setSize(600,200);
cr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton cal1=new JButton("Calculate Calories");
JPanel crp1 =new JPanel(new GridLayout());

GridBagConstraints t1=new GridBagConstraints();
GridBagConstraints t2=new GridBagConstraints();
GridBagConstraints t3=new GridBagConstraints();
t1.gridx=0;
t1.gridy=1;
t2.gridx=0;
t2.gridy=2;
t3.gridx=0;
t3.gridy=3;

crp1.setBackground(Color.GREEN);
JTextField txt1=new JTextField("Enter TimeDuration");
JTextField txt2=new JTextField("Enter Activity");
crp1.add(txt1,t1);
crp1.add(txt2,t1);
crp1.add(cal1,t3);
cr.add(crp1);



}});

b2.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){
JFrame cr = new JFrame();
cr.setVisible(true);
cr.setSize(600,200);
cr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JButton cal1=new JButton("Calories");
JPanel crp1 =new JPanel(new GridLayout());
GridBagConstraints t1=new GridBagConstraints();
GridBagConstraints t2=new GridBagConstraints();
GridBagConstraints t3=new GridBagConstraints();
t1.gridx=0;
t1.gridy=1;
t2.gridx=0;
t2.gridy=2;
t3.gridx=0;
t3.gridy=3;

crp1.setBackground(Color.GREEN);
JTextField txt1=new JTextField("Enter TimeDuration:");
JTextField txt2=new JTextField("Enter Your Average Heart rate:");
crp1.add(txt1,t1);
crp1.add(txt2,t1);
crp1.add(cal1,t3);
cr.add(crp1);


}});
b3.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){
JFrame cr = new JFrame();
cr.setVisible(true);
cr.setSize(300,100);
cr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton cal1=new JButton("Click To Calculate Body Mass");
JPanel crp1 =new JPanel(new GridLayout());

GridBagConstraints t3=new GridBagConstraints();
t3.gridx=0;
t3.gridy=3;

crp1.setBackground(Color.GREEN);
crp1.add(cal1,t3);
cr.add(crp1);
class BodyMassIndex extends fitness{
    double BodyIndex;
    BodyMassIndex(double weight,double height){
        super(weight,height);

    BodyIndex= getweight()/(getheight()*getheight());
    }


    {
if (BodyIndex<=18){
    System.out.printf("You are underwight");
   }
else if (BodyIndex<=18.5){
    System.out.printf("Thin for the height");
    }
   else if((18.5<=BodyIndex)&&(BodyIndex<=24.5)){
         System.out.printf("Wow your weight is healthy");
         }
        else if ((25<=BodyIndex)&&(BodyIndex<=29.5)){
           System.out.printf("You are over weight, Please take good care");
            }
            else if(BodyIndex>=30){
                System.out.printf("You are too over weight, Please consider a doctor");
                }
            else{
System.out.printf("error");
}       }
void display(){System.out.println("overriding"); }


}

}});
b4.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){
JFrame cr = new JFrame();
cr.setVisible(true);
cr.setSize(300,100);
cr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JButton cal1=new JButton("Click To Calculate Maximum Heart Rate");
JPanel crp1 =new JPanel(new GridLayout());
GridBagConstraints t3=new GridBagConstraints();
t3.gridx=0;
t3.gridy=3;

crp1.setBackground(Color.GREEN);
crp1.add(cal1,t3);
cr.add(crp1);


}});



}
public static void main(String[] args){

new fitness();
}}

Edited 2 Years Ago by umaima_1

That code is completely unreadable - blocks that go on forever, no comments, no indentation...
But as far as I can see the order of creating the frame is maybe wrong and missing steps. You should:
create the JFrame
add all the components
pack(); the JFrame (couldn't find this in your code)
finally, make it visible.

Edited 2 Years Ago by JamesCherrill

This article has been dead for over six months. Start a new discussion instead.