Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Sep 2004
    Posts
    202
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Java Help Needed w/ error update

    I have 2 classes (really 3 but the third doesn't need to be involved here)

    This program calculates the BMI of a person (I'll work on the formulas as soon as I can get this problem fixed)

    I want to get the input (text well it will be a number in this case) from the heighttf Text Field (GUI.java) (comments expalianing at the code area) and put it in a formula under the class getHeight() (bodymassindex.java).

    GUI .java
    package bmi;

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.*;
    import bmi.GUI.*;

    public class GUI extends JFrame implements Observer {
    //Create JTextFields
    private JTextField heighttf;
    private JTextField weighttf;
    private JTextField bmitf;
    private JTextField wvtf;
    private bodymassindex Body;
    private static int height;


    //Default Constructor
    public GUI(bodymassindex bm) {
    super();
    Body = bm;
    Container contents = getContentPane();

    //Create JPanels
    JPanel panel1 = new JPanel();
    panel1.setLayout(new BorderLayout());

    JPanel panel2 = new JPanel();
    panel2.setLayout(new GridLayout (1, 3, 5, 20));

    JPanel panel3 = new JPanel();
    panel3.setLayout(new GridLayout (1, 3, 5, 20));

    JPanel panel4 = new JPanel();
    panel4.setLayout(new GridLayout (1, 2, 5, 20));

    //Create JTextFields
    JTextField heighttf = new JTextField();
    JTextField weighttf = new JTextField();
    JTextField bmitf = new JTextField();
    JTextField wvtf = new JTextField();


    //Set Editable to False on bmitf and wvtf
    heighttf.setEditable(true);
    weighttf.setEditable(true);
    bmitf.setEditable(false);
    wvtf.setEditable(false);

    //Create JLabels
    JLabel heightl = new JLabel("Height");
    JLabel weightl = new JLabel("Weight");
    JLabel bmil = new JLabel("BMI");

    //Create JButtons
    JButton calcb = new JButton("Calculate");

    //Add Panels to Panel1
    panel1.add(panel2,BorderLayout.NORTH);
    panel1.add(panel3,BorderLayout.CENTER);
    panel1.add(panel4,BorderLayout.SOUTH);

    //Add buttons, text fields, etc to panel2
    panel2.add(heightl);
    panel2.add(weightl);
    panel2.add(bmil);

    //Add buttons, text fields, etc to panel3
    panel3.add(heighttf);
    panel3.add(weighttf);
    panel3.add(bmitf);

    //Add buttons, text fields, etc to panel4
    panel4.add(wvtf);
    panel4.add(calcb);

    //Set Text Align to Center
    weighttf.setHorizontalAlignment(JTextField.CENTER);
    heighttf.setHorizontalAlignment(JTextField.CENTER);
    bmitf.setHorizontalAlignment(JTextField.CENTER);
    wvtf.setHorizontalAlignment(JTextField.CENTER);

    //Resgister Listener
    weighttf.addActionListener(new weighttfhandler());
    heighttf.addActionListener(new heighttfhandler());
    calcb.addActionListener(new calcbuttonhandler());

    //Frame information
    contents.add(panel1);
    setTitle("BMI Calculator");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(200, 100);
    setVisible(false);
    bmitf.setText("" +Body.getbmi());
    }



    public void update (Observable o, Object arg) {


    }

    private class calcbuttonhandler implements ActionListener {
    public void actionPerformed (ActionEvent e) {
    System.out.println("The JButton Works!");
    Body.calc();
    }
    }
    public class postActionEvent {
    String heightget = heighttf.getText();
    int height = Integer.parseInt(heightget);
    }
    public class heighttfhandler implements ActionListener {
    public void actionPerformed (ActionEvent e) {
    String heightget = heighttf.getText();
    int height = Integer.parseInt(heightget);
    System.out.println("Hello" + height);

    }
    }

    private class weighttfhandler implements ActionListener {
    public void actionPerformed (ActionEvent e) {
    if (e.getSource() == weighttf) {
    int weight =
    Integer.parseInt(weighttf.getText().trim());
    System.out.println("" + weight);
    }
    }
    }
    }

    bodymassindex .java
    package bmi;

    import javax.swing.*;
    import java.util.*;

    public class bodymassindex extends Observable {
    private void GUI();
    private int height;
    private int weight;
    private int bmi;
    private char wv;
    private String heightString;


    public bodymassindex () {
    super();

    height = 0;
    weight = 0;
    bmi = 0;
    }

    public void calc() {
    getHeight();
    getWeight();
    getbmi();
    setwvtf();
    setChanged();
    notifyObservers();
    }

    public int getHeight() {
    height = GUI.postActionEvent.height;
    return height;
    }

    public int getWeight() {

    return weight;
    }
    public int getbmi() {
    getHeight();
    getWeight();
    bmi = weight + height;
    return bmi;
    }
    public char setwvtf() {
    return wv;
    }
    }


    I get an error:
    • non-static variable height cannot be referenced from a static context at line 33 (33:34)


    PLEASE HELP ME!
    Last edited by NeilKelty; 09-04-2004 at 03:04 AM. Reason: Updating Error message

  • #2
    New Coder
    Join Date
    Jan 2004
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i don't know which height it talk about since you have two variable height in two classes

    try change

    private static int height;

    to private int height;


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •