Results 1 to 6 of 6
  1. #1
    Irish Rayray is offline Member
    Join Date
    Apr 2012
    Posts
    21
    Rep Power
    0

    Default "Feet to Meters" & "Meters to Feet" Converter

    Hi All,

    I'm writing a Java GUI that converts Feet to Meters and Meter to Feet depending on the which is chosen in the Drop Box.
    So far im not having any luck with it.
    I have the GUI Designed but i cannot get it to do the conversion for me.

    1 Foot = .3 meters
    1 Meter = 3.28 feet;
    Im using these Conversions.

    This is my code so far
    Java Code:
    import java.awt.Color;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    public class Converter extends JFrame implements ActionListener {
    
    	private JLabel EnterValue = new JLabel("Enter Value to Convert:");
    	private JTextField TextField = new JTextField(5);
    	String[] Choose = { "Meters to Feet", "Feet to Meters" };
    	private JComboBox DropBox = new JComboBox(Choose);
    	private JButton Convert = new JButton("Convert");
    	private JLabel ResultIs = new JLabel("Result is:");
    	private JLabel Result = new JLabel("0");
    
    	Converter(String s) {
    		super(s);
    		getContentPane().setLayout(new GridLayout(4, 2));
    		DropBox.setSelectedIndex(1);
    
    		getContentPane().add(EnterValue);
    		getContentPane().add(TextField);
    		getContentPane().add(DropBox);
    		getContentPane().add(Convert);
    		getContentPane().add(ResultIs);
    		getContentPane().add(Result);
    		
    		Convert.addActionListener(this);
    		
    		setSize(400, 250);
    		pack();
    		setVisible(true);
    	}
    	public void actionPerformed(ActionEvent e) {
    		double Number, ResultT;
    		String text = TextField.getText();		
    		Number = Double.parseDouble (text);
    		
    		if("Convert".equals(e.getActionCommand()))
    		{
    			Object selectedItem = DropBox.getSelectedItem();
    			if(selectedItem.equals("Feet to Meters"))
    			{
    			{			 
    				ResultT = (Number) * 3.28;
    				Result.setText (Double.toString (ResultT));
    				
    			}else
    			{		 
    				ResultT = (Number) * .3;				
    				Result.setText (Double.toString (ResultT));
    				
    			}
    		}
    		
    	}
    
    	public static void main(String[] args) {
    		Converter thePanel = new Converter("Converter");
    	}
    }
    Thanks in advance.
    Last edited by Irish Rayray; 02-13-2013 at 03:14 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: "Feet to Meters" & "Meters to Feet" Converter

    Your button has no action listener attached to it, so does nothing at all.
    The actionPerformed method you have does nothing.

    Fix both those and then it'll work.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Irish Rayray is offline Member
    Join Date
    Apr 2012
    Posts
    21
    Rep Power
    0

    Default Re: "Feet to Meters" & "Meters to Feet" Converter

    Ok i have it updated to where i am now but i cant get the "Feet to Meters" to convert for me.

  4. #4
    Irish Rayray is offline Member
    Join Date
    Apr 2012
    Posts
    21
    Rep Power
    0

    Default Re: "Feet to Meters" & "Meters to Feet" Converter

    Never Mind I believe I may have it working now :)

  5. #5
    Irish Rayray is offline Member
    Join Date
    Apr 2012
    Posts
    21
    Rep Power
    0

    Default Re: "Feet to Meters" & "Meters to Feet" Converter

    I just need to now restrict the text field to only allow Integers..

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: "Feet to Meters" & "Meters to Feet" Converter

    Moved from Advanced Java. Please don't post elementary problems in that section.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 0
    Last Post: 12-07-2012, 09:29 AM
  2. Replies: 17
    Last Post: 04-21-2011, 07:34 PM
  3. conversion between feet and meters
    By Latanyar in forum New To Java
    Replies: 7
    Last Post: 10-10-2010, 09:20 PM
  4. program with converting feet to meters
    By Smitty300 in forum New To Java
    Replies: 5
    Last Post: 03-16-2010, 12:41 PM
  5. Replies: 1
    Last Post: 10-20-2008, 08:35 AM

Posting Permissions

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