Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2010
    Posts
    80
    Rep Power
    0

    Red face 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);
    		}
    		
    	}	
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by ŖΫ ỏƒ Ңόρę View Post
    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{
    	//....
    	
    	public void init(){
    		//...
    	}
    	
    	public void [color="red"]actionPerofrmed[/color](ActionEvent e){
    		//....
    	}	
    }
    Look at your program's code skeleton above. Check the spelling and I think your problem may be solved.

  3. #3
    Join Date
    Jan 2010
    Posts
    80
    Rep Power
    0

    Default

    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

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by ŖΫ ỏ Ңόρę View Post
    Thank u for reminding me with the spelling
    You're quite welcome.

    I fixed it and still doesn't work
    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 think the problem with the three lines that I colored them with red
    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.

  5. #5
    Join Date
    Jan 2010
    Posts
    80
    Rep Power
    0

    Default

    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

  1. datetime on a jtextfield
    By devstarter in forum New To Java
    Replies: 1
    Last Post: 03-02-2010, 01:08 PM
  2. JTextField
    By gancio in forum AWT / Swing
    Replies: 20
    Last Post: 08-26-2009, 03:11 PM
  3. Replies: 1
    Last Post: 01-30-2009, 06:44 PM
  4. JtextField
    By kashifu in forum Advanced Java
    Replies: 2
    Last Post: 06-27-2008, 04:25 PM
  5. help with JTextfield
    By gary in forum New To Java
    Replies: 4
    Last Post: 07-11-2007, 01:58 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
  •