Results 1 to 5 of 5
Thread: JTextField method numberic
- 03-14-2010, 09:47 PM #1
Member
- Join Date
- Jan 2010
- Posts
- 80
- Rep Power
- 0
JTextField method numberic
Hi
In the following code, I have a problem with JTextField method.
The problem is how I can get the numbers that the user enters in the fields.
Actually, I have tried many times to fix the problem but I couldn't
Java Code:import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Postage extends JApplet implements ActionListener{ private JLabel lengthLabel; private JLabel widthLabel; private JLabel weightLabel; private JLabel stanLabel; private JLabel posAmountLabel; private JTextField lengthField; private JTextField widthField; private JTextField weightField; private JButton submitButton; public void init(){ lengthLabel = new JLabel("Length (in cm):"); widthLabel = new JLabel("Width (in cm) :"); weightLabel = new JLabel("Weight (in g) :"); stanLabel = new JLabel("Welcome to my postage calculator!"); posAmountLabel = new JLabel("Enter the dimensions and weight above."); lengthField = new JTextField(5); widthField = new JTextField(5); weightField = new JTextField(5); submitButton = new JButton("Submit"); submitButton.addActionListener(this); Container contentPane = getContentPane(); contentPane.setLayout(new FlowLayout()); contentPane.add(lengthLabel); contentPane.add(lengthField); contentPane.add(widthLabel); contentPane.add(widthField); contentPane.add(weightLabel); contentPane.add(weightField); contentPane.add(submitButton); contentPane.add(stanLabel); contentPane.add(posAmountLabel); } public void actionPerofrmed(ActionEvent e){ [COLOR="Red"]double length = Integer.parseInt(lengthField.getText()); double width = Integer.parseInt(widthField.getText()); double weight = Integer.parseInt(weightField.getText());[/COLOR] String amount; if(weight<30){amount="0.54$";} else if(weight<51){amount="0.98$";} else if(weight<100){amount="1.18$";} else if(weight<201){amount="1.96$";} else {amount="2.75$";} if ((length>24.5)||(width>15.6)||(weight>50)){ stanLabel.setText("This is non-standard lettermail."); posAmountLabel.setText("The postage amount will be: $"+amount); }else{ stanLabel.setText("This is standard lettermail."); posAmountLabel.setText("The postage amount will be: $"+amount); } } }
-
- 03-14-2010, 10:50 PM #3
Member
- Join Date
- Jan 2010
- Posts
- 80
- Rep Power
- 0
Thank u for reminding me with the spelling
I fixed it and still doesn't work
I think the problem with the three lines that I colored them with red
-
You're quite welcome.
Doesn't work as in shows an error message? As in doesn't run? As in it turns your computer into smoldering pile of ashes? Please help us out on the details here.I fixed it and still doesn't work
I've no idea. You've changed your code and so you'll need to post an update. Otherwise I'll be guessing about code I can't see.I think the problem with the three lines that I colored them with red
- 03-14-2010, 11:57 PM #5
Member
- Join Date
- Jan 2010
- Posts
- 80
- Rep Power
- 0
I finally fixed it
This is the right code
Java Code:import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Postage extends JApplet implements ActionListener{ private JLabel lengthLabel; private JLabel widthLabel; private JLabel weightLabel; private JLabel stanLabel; private JLabel posAmountLabel; private JTextField lengthField; private JTextField widthField; private JTextField weightField; private JButton submitButton; public void init(){ lengthLabel = new JLabel("Length (in cm):"); widthLabel = new JLabel("Width (in cm) :"); weightLabel = new JLabel("Weight (in g) :"); stanLabel = new JLabel("Welcome to my postage calculator!"); posAmountLabel = new JLabel("Enter the dimensions and weight above."); lengthField = new JTextField(5); widthField = new JTextField(5); weightField = new JTextField(5); submitButton = new JButton("Submit"); submitButton.addActionListener(this); Container contentPane = getContentPane(); contentPane.setLayout(new FlowLayout()); contentPane.add(lengthLabel); contentPane.add(lengthField); contentPane.add(widthLabel); contentPane.add(widthField); contentPane.add(weightLabel); contentPane.add(weightField); contentPane.add(submitButton); contentPane.add(stanLabel); contentPane.add(posAmountLabel); } public void actionPerformed(ActionEvent e){ [COLOR="Red"]double length = Double.parseDouble(lengthField.getText()); double width = Double.parseDouble(widthField.getText()); double weight = Double.parseDouble(weightField.getText());[/COLOR] String amount; if(weight<30){amount="0.54$";} else if(weight<51){amount="0.98$";} else if(weight<100){amount="1.18$";} else if(weight<201){amount="1.96$";} else {amount="2.75$";} if ((length>24.5)||(width>15.6)||(weight>50)){ stanLabel.setText("This is non-standard lettermail."); posAmountLabel.setText("The postage amount will be: "+amount); }else{ stanLabel.setText("This is standard lettermail."); posAmountLabel.setText("The postage amount will be: "+amount); } } }
Similar Threads
-
datetime on a jtextfield
By devstarter in forum New To JavaReplies: 1Last Post: 03-02-2010, 01:08 PM -
JTextField
By gancio in forum AWT / SwingReplies: 20Last Post: 08-26-2009, 03:11 PM -
how to access jTextField of one JFrame1 from JFrame2 & Modify JTextField contents
By sumit1mca in forum AWT / SwingReplies: 1Last Post: 01-30-2009, 06:44 PM -
JtextField
By kashifu in forum Advanced JavaReplies: 2Last Post: 06-27-2008, 04:25 PM -
help with JTextfield
By gary in forum New To JavaReplies: 4Last Post: 07-11-2007, 01:58 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks