Results 1 to 13 of 13

Thread: gui classes

  1. #1
    phantom135 is offline Member
    Join Date
    Aug 2010
    Posts
    6
    Rep Power
    0

    Default gui classes

    hey i need help urgently. i've been looking for solution for this for a week nw and i need to hand this in by thurs... :( i got the basic look of the application. when i open bmi, it does open a new frame. but when i put values to be calculated. it doesn't show up in the resultfield.

    ps: the calculation is still addition, i'm still trying out.

    1st class, main.


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

    public class JavaProject extends JFrame
    {
    private JButton BMIButton = new JButton("BMI");
    private JButton BMRButton = new JButton("Metabolic Rate");
    private JButton HLPButton = new JButton("Blood Pressure");
    private JButton DiabeteButton = new JButton("Blood Sugar Level");
    private JButton CalorieTakeInButton = new JButton("Calorie Take In");
    private JButton CalorieChartButton = new JButton("Calorie Chart");

    private BMItest bmiFrame = new BMItest();


    public JavaProject()
    {

    this.setLayout(new GridLayout(3,2));
    this.add(BMIButton);
    this.add(BMRButton);
    this.add(HLPButton);
    this.add(DiabeteButton);
    this.add(CalorieTakeInButton);
    this.add(CalorieChartButton);



    BMIButton.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent evt)
    {
    bmiFrame.setVisible(true);
    bmiFrame.setTitle("BMI");
    bmiFrame.setSize(200, 150);
    bmiFrame.setResizable(false);
    }
    });


    }






    public static void main(String[] args)
    {
    JavaProject fitnessMonitor = new JavaProject();
    fitnessMonitor.setTitle("demo");
    fitnessMonitor.setSize(400,400);
    fitnessMonitor.setLocationRelativeTo(null);
    fitnessMonitor.setVisible(true);
    fitnessMonitor.setDefaultCloseOperation(JFrame.EXI T_ON_CLOSE);
    fitnessMonitor.setResizable(false);
    }








    }





    2nd class


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




    class BMItest extends JFrame
    {
    public int bmiHeight,bmiWeight,myBMIResult;
    private JButton calculateBMI = new JButton("Calculate!");
    private JTextField inputWeight = new JTextField("");
    private JTextField inputHeight = new JTextField("");
    private JLabel weight = new JLabel("Input Weight :");
    private JLabel height = new JLabel("input Height :");
    private JTextField bmiResult = new JTextField("");
    private JLabel bmi = new JLabel("BMI");

    public BMItest()
    {

    JPanel paneA = new JPanel();
    paneA.setLayout(new GridLayout(3,2));
    paneA.add(weight);
    paneA.add(inputWeight);
    paneA.add(height);
    paneA.add(inputHeight);
    paneA.add(bmi);
    paneA.add(bmiResult);
    this.add(paneA,BorderLayout.NORTH);
    this.add(calculateBMI, BorderLayout.SOUTH);





    }







    public void actionPerformed(ActionEvent e)
    {
    if(e.getSource()==calculateBMI)
    {
    //try
    // {


    String inHeight = inputHeight.getText();
    bmiHeight = Integer.parseInt(inHeight);
    String inWeight = inputWeight.getText();
    bmiWeight = Integer.parseInt(inWeight);

    myBMIResult = bmiWeight + bmiHeight;


    bmiResult.setText("" + myBMIResult);

    // }
    // catch(NumberFormatException err)
    // {
    // System.out.println("Error");
    // }
    }

    }





    thx to anyone who replies to this..

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,564
    Rep Power
    23

    Default

    it doesn't show up in the resultfield.
    I did a search thru your code for: resultfield and did not find it.
    Did you post the correct code?

    Also I don't see any comments in your code describing what it is supposed to do and how it is going to do it.

  3. #3
    phantom135 is offline Member
    Join Date
    Aug 2010
    Posts
    6
    Rep Power
    0

    Default

    oops... eh.. the name shld be myBMIResult.. sorry XD

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,564
    Rep Power
    23

    Default

    myBMIResult is an int.
    To see its value add: System.out.println("myBMIResult=" + myBMIResult);
    when it has a value.

  5. #5
    phantom135 is offline Member
    Join Date
    Aug 2010
    Posts
    6
    Rep Power
    0

    Default

    but it doesn't display in the GUI?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,564
    Rep Power
    23

    Default

    Does the println() display the value ok?

  7. #7
    phantom135 is offline Member
    Join Date
    Aug 2010
    Posts
    6
    Rep Power
    0

    Default

    it didn't display a value =\

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,564
    Rep Power
    23

    Default

    Which was it?
    It didn't display a value. The output was: "myBMIResult="
    or
    It didn't display anything. There was no output.

    If there was no output, then code wasn't executed.
    Check your program to see why the code is not being executed?
    Did the if() test in the method fail? Add a println() before the if test to see that the method is called.
    Was the method called?

  9. #9
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    Well reason for that is simple:

    You have never added ActionListener to BMIButton.
    You have written code very well and you just need to add actionListener to your button.

  10. #10
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    Your listener is not working because of one line:

    calculateBMI.addActionListener()

    I have combined that line with you entire function actionPerformed, but it is better to work with actionListeners defined in separate private class.

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class JavaProject extends JFrame {
    	private JButton BMIButton = new JButton("BMI");
    	private JButton BMRButton = new JButton("Metabolic Rate");
    	private JButton HLPButton = new JButton("Blood Pressure");
    	private JButton DiabeteButton = new JButton("Blood Sugar Level");
    	private JButton CalorieTakeInButton = new JButton("Calorie Take In");
    	private JButton CalorieChartButton = new JButton("Calorie Chart");
    
    	private BMItest bmiFrame = new BMItest();
    
    
    	public JavaProject() {
    
    		this.setLayout(new GridLayout(3,2));
    		this.add(BMIButton);
    		this.add(BMRButton);
    		this.add(HLPButton);
    		this.add(DiabeteButton);
    		this.add(CalorieTakeInButton);
    		this.add(CalorieChartButton);
    
    
    
    		BMIButton.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent evt) { 
    				bmiFrame.setVisible(true); 
    				bmiFrame.setTitle("BMI"); 
    				bmiFrame.setSize(200, 150);
    				bmiFrame.setResizable(false); 
    			}
    		});
    }
    
    public static void main(String[] args) {
    	JavaProject fitnessMonitor = new JavaProject();
    	fitnessMonitor.setTitle("demo");
    	fitnessMonitor.setSize(400,400);
    	fitnessMonitor.setLocationRelativeTo(null);
    	fitnessMonitor.setVisible(true);
    	fitnessMonitor.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	fitnessMonitor.setResizable(false);
    }
    
    }
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    class BMItest extends JFrame {
    	public int bmiHeight,bmiWeight,myBMIResult;
    	private JButton calculateBMI = new JButton("Calculate!");
    	private JTextField inputWeight = new JTextField("");
    	private JTextField inputHeight = new JTextField("");
    	private JLabel weight = new JLabel("Input Weight :");
    	private JLabel height = new JLabel("input Height :");
    	private JTextField bmiResult = new JTextField("");
    	private JLabel bmi = new JLabel("BMI");
    
    	public BMItest() {
    
    		JPanel paneA = new JPanel();
    		paneA.setLayout(new GridLayout(3,2));
    		paneA.add(weight);
    		paneA.add(inputWeight);
    		paneA.add(height);
    		paneA.add(inputHeight);
    		paneA.add(bmi);
    		paneA.add(bmiResult);
    		this.add(paneA,BorderLayout.NORTH);
    		this.add(calculateBMI, BorderLayout.SOUTH);
    		calculateBMI.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				if(e.getSource()==calculateBMI) {
    
    					String inHeight = inputHeight.getText();
    					bmiHeight = Integer.parseInt(inHeight);
    					String inWeight = inputWeight.getText();
    					bmiWeight = Integer.parseInt(inWeight);
    					
    					myBMIResult = bmiWeight + bmiHeight;
    			
    					bmiResult.setText("" + myBMIResult);
    				}
    			}
    		});
    }
    
    
    }

    Java Code:
    calculateBMI.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				if(e.getSource()==calculateBMI) {
    
    					String inHeight = inputHeight.getText();
    					bmiHeight = Integer.parseInt(inHeight);
    					String inWeight = inputWeight.getText();
    					bmiWeight = Integer.parseInt(inWeight);
    					
    					myBMIResult = bmiWeight + bmiHeight;
    			
    					bmiResult.setText("" + myBMIResult);
    				}
    			}
    Try to write this code with for example this code:
    calculateBMI.addActionListener(new calculateBMIListener());

    private class calculateBMILIstener implements ActionListener {

    // code here
    }
    Last edited by cselic; 08-02-2010 at 04:36 PM.

  11. #11
    phantom135 is offline Member
    Join Date
    Aug 2010
    Posts
    6
    Rep Power
    0

    Default

    ah! so this is the proper one. thx man. i was looking for this all over the net. but some of them didn't do it properly. so i couldn't implement it properly too.. XD

  12. #12
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    It would be better to use JDialog instead JFrame.

    I mean when you click on BMI button its better that dialog is displayed (in which you will work some calculations) instead new frame.

  13. #13
    phantom135 is offline Member
    Join Date
    Aug 2010
    Posts
    6
    Rep Power
    0

    Default

    hmmm... ic.. well.. it was a rushed project. XD.. so this was wat i could think of at that moment. so i just implemented it.. hahaha! thx for the advise. =)

Similar Threads

  1. A little help with classes.
    By ThrashingBoy in forum New To Java
    Replies: 8
    Last Post: 06-10-2010, 09:52 AM
  2. Three classes help!!
    By arrech326 in forum New To Java
    Replies: 4
    Last Post: 11-24-2009, 06:34 AM
  3. Help with classes
    By gnarly hogie in forum New To Java
    Replies: 14
    Last Post: 10-10-2008, 02:29 PM
  4. Get name of available classes
    By escuja in forum CLDC and MIDP
    Replies: 0
    Last Post: 07-26-2008, 12:03 PM
  5. Using a JAR from other classes
    By Joe2003 in forum Advanced Java
    Replies: 1
    Last Post: 01-02-2008, 07:08 PM

Posting Permissions

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