Results 1 to 13 of 13
Thread: gui classes
- 08-02-2010, 02:19 PM #1
Member
- Join Date
- Aug 2010
- Posts
- 6
- Rep Power
- 0
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..
- 08-02-2010, 02:43 PM #2
I did a search thru your code for: resultfield and did not find it.it doesn't show up in the resultfield.
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.
- 08-02-2010, 03:00 PM #3
Member
- Join Date
- Aug 2010
- Posts
- 6
- Rep Power
- 0
oops... eh.. the name shld be myBMIResult.. sorry XD
- 08-02-2010, 03:08 PM #4
myBMIResult is an int.
To see its value add: System.out.println("myBMIResult=" + myBMIResult);
when it has a value.
- 08-02-2010, 03:20 PM #5
Member
- Join Date
- Aug 2010
- Posts
- 6
- Rep Power
- 0
but it doesn't display in the GUI?
- 08-02-2010, 03:51 PM #6
Does the println() display the value ok?
- 08-02-2010, 04:03 PM #7
Member
- Join Date
- Aug 2010
- Posts
- 6
- Rep Power
- 0
it didn't display a value =\
- 08-02-2010, 04:22 PM #8
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?
- 08-02-2010, 04:22 PM #9
Senior Member
- Join Date
- Apr 2010
- Location
- Belgrade, Serbia
- Posts
- 278
- Rep Power
- 4
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.
- 08-02-2010, 04:27 PM #10
Senior Member
- Join Date
- Apr 2010
- Location
- Belgrade, Serbia
- Posts
- 278
- Rep Power
- 4
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); } } }); } }
Try to write this code with for example this code: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); } }
calculateBMI.addActionListener(new calculateBMIListener());
private class calculateBMILIstener implements ActionListener {
// code here
}Last edited by cselic; 08-02-2010 at 04:36 PM.
- 08-02-2010, 04:33 PM #11
Member
- Join Date
- Aug 2010
- Posts
- 6
- Rep Power
- 0
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
- 08-02-2010, 04:54 PM #12
Senior Member
- Join Date
- Apr 2010
- Location
- Belgrade, Serbia
- Posts
- 278
- Rep Power
- 4
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.
- 08-05-2010, 05:28 PM #13
Member
- Join Date
- Aug 2010
- Posts
- 6
- Rep Power
- 0
Similar Threads
-
A little help with classes.
By ThrashingBoy in forum New To JavaReplies: 8Last Post: 06-10-2010, 09:52 AM -
Three classes help!!
By arrech326 in forum New To JavaReplies: 4Last Post: 11-24-2009, 06:34 AM -
Help with classes
By gnarly hogie in forum New To JavaReplies: 14Last Post: 10-10-2008, 02:29 PM -
Get name of available classes
By escuja in forum CLDC and MIDPReplies: 0Last Post: 07-26-2008, 12:03 PM -
Using a JAR from other classes
By Joe2003 in forum Advanced JavaReplies: 1Last Post: 01-02-2008, 07:08 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks