-
Applet help get text and use it as a double
Hello to all
I am sure this is easy to others that are more experience with java applets.
I need an example of when the user enters a double type number into a text field, then change it from text to a workable double for calculation them display results.
I can get this to work in an application but not in a applet.
So far using the getText() I can display text but when I try to convert text to a workable double using
String typHoursIn = HoursField.getText();
double hours;
hours = Double.parseDouble( typHoursIn );
I get error: cannot resolve symbol on Total, hours, and rate.
example code below
-----------------------------------------------------------
public void changeToNum(double hours,double rate)
{
String typHoursIn = HoursField.getText();
hours = Double.parseDouble( typHoursIn );
String typRateIn = RateField.getText();
rate = Double.parseDouble( typRateIn );
}
public double calcu(double Total)
{
double hrs= changeToNum(hours);
double r= changeToNum(rate);
double sum=0;
double Otime = 0;
if(hrs <= 40){
Total = r * hrs;
}else{
sum = r * 40;
Otime = (hrs - 40)*(r * 1.50);
Total = sum + Otime;
}
return Total;
}
public void paint(Graphics g)
{
// get the text out the TextField
g.setColor(Color.blue);
g.drawString("Name: " + NameField.getText(),140,150);
g.drawString("Hours worked: "+ HoursField.getText(),140,170);
g.drawString("Pay Rate: " + RateField.getText(),140,190);
g.drawString("Gross Pay is: " + calcu(Total),140,220);
}
I look forward to any help
Mike
-
JTextField t = new JTextField();
double d = Double.parseDouble(t.getText());
-
full code
Thanks Phaelax
I still can not get it to work like I said before I get error: cannot resolve symbol on Total, hours, and rate I am posting the the whole code.
-----------------------------------------------------------------------
import java.io.*;
import java.awt.*;
import java.applet.*;
// import an extra class for the ActionListener
import java.awt.event.*;
public class wageCalApplet extends Applet implements ActionListener
{
// A Button to click
Button OkButton;
// 3 textFieldS to get text input
TextField NameField;
TextField HoursField;
TextField RateField;
// labelS to A fix staionary text
Label Label1;
Label Label2;
Label Label3;
public void init()
{
setLayout(new FlowLayout());
// Tell the applet not to use a layout manager.
setLayout(null);
// initialze the button and give it text value.
OkButton = new Button("NEXT");
// text and length of the field
NameField = new JTextField(" ",50);
HoursField = new JTextField(" ",50);
RateField = new JTextField(" ",50);
Label1 = new Label("Enter Name");
Label2 = new Label("Enter Hours Worked");
Label3 = new Label("Enter Pay Rate");
// now we will specify the positions of the GUI components.
// this is done by specifying the x and y coordinate and
//the width and height.
Label1.setBounds(20,20,140,20);
NameField.setBounds(140,20,225,20);
Label2.setBounds(20,55,140,20);
HoursField.setBounds(140,55,75,20);
Label3.setBounds(20,90,140,20);
RateField.setBounds(140,90,75,20);
OkButton.setBounds(20,145,70,20);
// now that all is set we can add these components to the applet
add(OkButton);
add(NameField);
add(HoursField);
add(RateField);
add(Label1);
add(Label2);
add(Label3);
// Attach actions to the components
OkButton.addActionListener(this);
}
public double calcu(double Total)
{
//this is were the problem is I thank
double hours = Double.parseDouble(HoursField.getText());
double rate= Double.parseDouble(RateField.getText());
double sum=0;
double Otime = 0;
if(hours <= 40){
Total = rate * hours;
}else{
sum = rate * 40;
Otime = (hours - 40)*(rate * 1.50);
Total = sum + Otime;
}
return Total;
}
// Here we will show the results of our actions
public void paint(Graphics g)
{
// get the text out the TextField
g.setColor(Color.blue);
g.drawString("Name: " + NameField.getText(),140,150);
g.drawString("Hours worked: "+ HoursField.getText(),140,170);
g.drawString("Pay Rate: " + RateField.getText(),140,190);
//display the Total
g.drawString("Gross Pay is: " + calcu(Total),140,220);
}
// When the button is clicked this method will get automatically called // This is where you specify all actions.
public void actionPerformed(ActionEvent evt)
{
// Here we will ask what component called this method
if (evt.getSource() == OkButton){
// Let the applet perform Paint again.
repaint();
}
}
}
----------------------------------------------------
I am using this applet in a pressentation in school and that is why things may be broken down a little different
I appreciate any help or out put
Mike
-
Well Total is a local variable to that method calcu(), so you cannot use it outside of that methos unless you declare it first as a global variable.
Also, when you call the method you are doing this:
calcu(Total)
Total is not defined at all as a variable, you need to define it.
One more thing, it is kind of an unwritten law to name variables in lowercase (first letter) and classes in uppercase (first letter).
A kram a day keeps the doctor......guessing
-
Thanks Kram,
Just as you were posting I found that to be so.
I just made Total public in the class and it all works fine. I should have know better, but I am kinda new to writing java applets.
Once again thanks everybody
Mike
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks