Page 1 of 2 12 LastLast
Results 1 to 20 of 25
  1. #1
    Join Date
    May 2010
    Posts
    11
    Rep Power
    0

    Exclamation «HELP with Java Program»

    I'm having issues with my JLabels, the setText function isn't changing the label's text. Help Needed Before 05/20/10

    Here's My Code:

    import java.io.*;
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;

    public class Converter extends JFrame
    {
    private final int WINDOW_WIDTH = 524; // Window width
    private final int WINDOW_HEIGHT = 80; // Window height

    public Converter()
    {
    setTitle("Length Converter");

    setSize(WINDOW_WIDTH, WINDOW_HEIGHT);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    setLayout(new GridLayout(2, 3));

    JLabel Miles = new JLabel("Miles: ");
    JLabel Kilometer = new JLabel("Kilometer: ");
    JLabel Meter = new JLabel("Meter: ");
    JLabel Feet = new JLabel("Feet: ");
    JLabel Inches = new JLabel("Inches: ");
    JLabel Centimeter = new JLabel("Centimeter: ");
    JLabel lblMile = new JLabel();
    JLabel lblKilometer = new JLabel();
    JLabel lblMeter = new JLabel();
    JLabel lblFeet = new JLabel();
    JLabel lblInches = new JLabel();
    JLabel lblCentimeter = new JLabel();


    JPanel panel1 = new JPanel();
    JPanel panel2 = new JPanel();
    JPanel panel3 = new JPanel();
    JPanel panel4 = new JPanel();
    JPanel panel5 = new JPanel();
    JPanel panel6 = new JPanel();
    JPanel panel7 = new JPanel();
    JPanel panel8 = new JPanel();
    JPanel panel9 = new JPanel();
    JPanel panel10 = new JPanel();
    JPanel panel11 = new JPanel();
    JPanel panel12 = new JPanel();

    panel1.add(Miles);
    panel2.add(Kilometer);
    panel3.add(Meter);
    panel4.add(Feet);
    panel5.add(Inches);
    panel6.add(Centimeter);
    panel7.add(lblMile);
    panel8.add(lblKilometer);
    panel9.add(lblMeter);
    panel10.add(lblFeet);
    panel11.add(lblInches);
    panel12.add(lblCentimeter);



    add(panel1);
    add(panel2);
    add(panel3);
    add(panel4);
    add(panel5);
    add(panel6);
    add(panel7);
    add(panel8);
    add(panel9);
    add(panel10);
    add(panel11);
    add(panel12);

    // Display the window.
    setVisible(true);
    }

    public static void main(String[] args) throws IOException
    {

    int choose;
    double miles, kilometer, meter, feet, inches, centimeter;
    boolean done = false;
    String strChoose;
    String strMiles, strKilometer, strMeter, strFeet, strInches, strCentimeter;
    String totalKilometer;
    String totalMeter;
    String totalFeet;
    String totalInches;
    String totalCentimeter;

    JLabel lblMile = new JLabel();
    JLabel lblKilometer = new JLabel();
    JLabel lblMeter = new JLabel();
    JLabel lblFeet = new JLabel();
    JLabel lblInches = new JLabel();
    JLabel lblCentimeter = new JLabel();

    new Converter();



    while (!done)
    {

    strChoose = JOptionPane.showInputDialog("What you would like to convert?" + "\n" + "1) Miles 2) Kilometer 3) Meter"+ "\n"
    + "4) Feet 5) Inches 6) Centimeter" + "\n" + "7) Exit", "Enter Your choice");
    choose = Integer.parseInt(strChoose);

    if ((choose <= 0) || (choose >= 8))
    {
    JOptionPane.showMessageDialog(null, "\tInvalid entry, please try again!");
    }

    switch (choose)
    {
    case 1:
    strMiles = JOptionPane.showInputDialog("Enter value for Miles: ");
    miles = Double.parseDouble(strMiles) * 0.6214;
    String totalMiles = Double.toString(miles);

    lblMile.setText(totalMiles);
    JOptionPane.showMessageDialog(null, "\t\tMeter: " + (miles*1609.3));
    JOptionPane.showMessageDialog(null, "\t\tFeet: " + (miles*5279.85565107));
    JOptionPane.showMessageDialog(null, "\t\tInches: " + (miles*63358.26781284));
    JOptionPane.showMessageDialog(null, "\t\tCentimeter: " + (miles*160930.0002446136));
    break;

    case 2:
    strKilometer = JOptionPane.showInputDialog("Enter value for Kilometer: ");
    kilometer = Double.parseDouble(strKilometer);
    JOptionPane.showMessageDialog(null, "\n\t\tMiles: " + (kilometer*0.6213882));
    JOptionPane.showMessageDialog(null, "\t\tMeter: " + (kilometer*1000));
    JOptionPane.showMessageDialog(null, "\t\tFeet: " + (kilometer*3280.8399));
    JOptionPane.showMessageDialog(null, "\t\tInches: " + (kilometer*39370.0788));
    JOptionPane.showMessageDialog(null, "\t\tCentimeter: " + (kilometer*100000));
    break;

    case 3:
    strMeter = JOptionPane.showInputDialog("Enter value for Meter: ");
    meter = Double.parseDouble(strMeter);
    JOptionPane.showMessageDialog(null, "\n\t\tMiles: " + (meter*621.3882));
    JOptionPane.showMessageDialog(null, "\t\tKilometer: " + (meter*1000));
    JOptionPane.showMessageDialog(null, "\t\tFeet: " + (meter*3.2808399));
    JOptionPane.showMessageDialog(null, "\t\tInches: " + (meter*39.3700788));
    JOptionPane.showMessageDialog(null, "\t\tCentimeter: " + (meter*100));
    break;

    case 4:
    strFeet = JOptionPane.showInputDialog("Enter value for Feet: ");
    feet = Double.parseDouble(strFeet);
    JOptionPane.showMessageDialog(null, "\n\t\tMiles: " + (feet*0.0001893991176));
    JOptionPane.showMessageDialog(null, "\t\tKilometer: " + (feet*0.0003048));
    JOptionPane.showMessageDialog(null, "\t\tMeter: " + (feet*0.3048));
    JOptionPane.showMessageDialog(null, "\t\tInches: " + (feet*12));
    JOptionPane.showMessageDialog(null, "\t\tCentimeter: " + (feet*30.48));
    break;

    case 5:
    strInches = JOptionPane.showInputDialog("Enter value for Inches: ");
    inches = Double.parseDouble(strInches);
    JOptionPane.showMessageDialog(null, "\n\t\tMiles: " + (inches*0.000015783254));
    JOptionPane.showMessageDialog(null, "\t\tKilometer: " + (inches*0.000025349));
    JOptionPane.showMessageDialog(null, "\t\tMeter: " + (inches*0.02534));
    JOptionPane.showMessageDialog(null, "\t\tFeet: " + (inches*0.08333));
    JOptionPane.showMessageDialog(null, "\t\tCentimeter: " + (inches*2.51));
    break;

    case 6:
    strCentimeter = JOptionPane.showInputDialog("Enter value for Centimeter: ");
    centimeter = Double.parseDouble(strCentimeter);
    JOptionPane.showMessageDialog(null, "\n\t\tMiles: " + (centimeter*0.000006218797));
    JOptionPane.showMessageDialog(null, "\t\tKilometer: " + (centimeter*0.000004));
    JOptionPane.showMessageDialog(null, "\t\tMeter: " + (centimeter*0.004));
    JOptionPane.showMessageDialog(null, "\t\tFeet: " + (centimeter*0.032808386877));
    JOptionPane.showMessageDialog(null, "\t\tCentimeter: " + (centimeter*0.3937008));
    break;

    case 7:
    done = true;
    JOptionPane.showMessageDialog(null, "Thank you for using this program!");
    System.exit(0);
    break;

    default: throw new NumberFormatException();

    } // end switch
    } // end while
    } // end main

    //short cut for print
    private static void print(String s)
    {
    System.out.print(s);
    }

    //short cut for println
    private static void println(String s)
    {
    System.out.println(s);
    }
    }

    Any Thoughts?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Tired Of Not Getting Help View Post
    Any Thoughts?
    Yes; I'm sure that blob of code looks quite different where it came from. Please use the code tags for readability; this is hardly readable.

    kind regards,

    Jos

  3. #3
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Follow the link in my signature.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  4. #4
    Join Date
    May 2010
    Posts
    11
    Rep Power
    0

    Default «HELP with Java Program»

    Ok Sorry Here Revised:
    I'm having issues with my JLabels, the setText function isn't changing the label's text. Help Needed Before 05/20/10

    Here's My Code:
    Java Code:
    import java.io.*;
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    
    public class Converter extends JFrame
    {
    	private final int WINDOW_WIDTH = 524;  // Window width
    	private final int WINDOW_HEIGHT = 80;  // Window height
    
    	public Converter()
    	{
    	      setTitle("Length Converter");
    
    	      setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
    
    	      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    	      setLayout(new GridLayout(2, 3));
    
    	      JLabel Miles = new JLabel("Miles: ");
    	      JLabel Kilometer = new JLabel("Kilometer: ");
    	      JLabel Meter = new JLabel("Meter: ");
    	      JLabel Feet = new JLabel("Feet: ");
    	      JLabel Inches = new JLabel("Inches: ");
    	      JLabel Centimeter = new JLabel("Centimeter: ");
    	      JLabel lblMile = new JLabel();
    	      JLabel lblKilometer = new JLabel();
    	      JLabel lblMeter = new JLabel();
    	      JLabel lblFeet = new JLabel();
    	      JLabel lblInches = new JLabel();
    	      JLabel lblCentimeter = new JLabel();
    
    
    	      JPanel panel1 = new JPanel();
    	      JPanel panel2 = new JPanel();
    	      JPanel panel3 = new JPanel();
    	      JPanel panel4 = new JPanel();
    	      JPanel panel5 = new JPanel();
    	      JPanel panel6 = new JPanel();
    	      JPanel panel7 = new JPanel();
    	      JPanel panel8 = new JPanel();
    	      JPanel panel9 = new JPanel();
    	      JPanel panel10 = new JPanel();
    	      JPanel panel11 = new JPanel();
    	      JPanel panel12 = new JPanel();
    
    	      panel1.add(Miles);
    	      panel2.add(Kilometer);
    	      panel3.add(Meter);
    	      panel4.add(Feet);
    	      panel5.add(Inches);
    	      panel6.add(Centimeter);
    	      panel7.add(lblMile);
    	      panel8.add(lblKilometer);
    	      panel9.add(lblMeter);
    	      panel10.add(lblFeet);
    	      panel11.add(lblInches);
    	      panel12.add(lblCentimeter);
    
    
    
    	      add(panel1);
    	      add(panel2);
    	      add(panel3);
    	      add(panel4);
    	      add(panel5);
    	      add(panel6);
    	      add(panel7);
    	      add(panel8);
    	      add(panel9);
    	      add(panel10);
    	      add(panel11);
    	      add(panel12);
    
    	      // Display the window.
    	      setVisible(true);
    		}
    
    public static void main(String[] args) throws IOException
    {
    
    	int choose;
    	double miles, kilometer, meter, feet, inches, centimeter;
    	boolean done = false;
    	String strChoose;
    	String strMiles, strKilometer, strMeter, strFeet, strInches, strCentimeter;
    	String totalKilometer;
    	String totalMeter;
    	String totalFeet;
    	String totalInches;
    	String totalCentimeter;
    
     	      JLabel lblMile = new JLabel();
     	      JLabel lblKilometer = new JLabel();
     	      JLabel lblMeter = new JLabel();
     	      JLabel lblFeet = new JLabel();
     	      JLabel lblInches = new JLabel();
     	      JLabel lblCentimeter = new JLabel();
    
    	new Converter();
    
    
    
    	while (!done)
    	{
    
    		strChoose = JOptionPane.showInputDialog("What you would like to convert?" + "\n" + "1) Miles 2) Kilometer 3) Meter"+ "\n"
    										+ "4) Feet 5) Inches 6) Centimeter" + "\n" + "7) Exit", "Enter Your choice");
    		choose = Integer.parseInt(strChoose);
    
    		if ((choose <= 0) || (choose >= 8))
    		{
    				JOptionPane.showMessageDialog(null, "\tInvalid entry, please try again!");
    		}
    
    			switch (choose)
    			{
    			case 1:
    			strMiles = JOptionPane.showInputDialog("Enter value for Miles: ");
    			miles = Double.parseDouble(strMiles) * 0.6214;
    			String totalMiles = Double.toString(miles);
    			lblMile.setText(strMiles);
    			JOptionPane.showMessageDialog(null, totalMiles);
    			JOptionPane.showMessageDialog(null, "\t\tMeter: " + (miles*1609.3));
    			JOptionPane.showMessageDialog(null, "\t\tFeet: " + (miles*5279.85565107));
    			JOptionPane.showMessageDialog(null, "\t\tInches: " + (miles*63358.26781284));
    			JOptionPane.showMessageDialog(null, "\t\tCentimeter: " + (miles*160930.0002446136));
    			break;
    
    			case 2:
    			strKilometer = JOptionPane.showInputDialog("Enter value for Kilometer: ");
    			kilometer = Double.parseDouble(strKilometer);
    			JOptionPane.showMessageDialog(null, "\n\t\tMiles: " + (kilometer*0.6213882));
    			JOptionPane.showMessageDialog(null, "\t\tMeter: " + (kilometer*1000));
    			JOptionPane.showMessageDialog(null, "\t\tFeet: " + (kilometer*3280.8399));
    			JOptionPane.showMessageDialog(null, "\t\tInches: " + (kilometer*39370.0788));
    			JOptionPane.showMessageDialog(null, "\t\tCentimeter: " + (kilometer*100000));
    			break;
    
    			case 3:
    			strMeter = JOptionPane.showInputDialog("Enter value for Meter: ");
    			meter = Double.parseDouble(strMeter);
    			JOptionPane.showMessageDialog(null, "\n\t\tMiles: " + (meter*621.3882));
    			JOptionPane.showMessageDialog(null, "\t\tKilometer: " + (meter*1000));
    			JOptionPane.showMessageDialog(null, "\t\tFeet: " + (meter*3.2808399));
    			JOptionPane.showMessageDialog(null, "\t\tInches: " + (meter*39.3700788));
    			JOptionPane.showMessageDialog(null, "\t\tCentimeter: " + (meter*100));
    			break;
    
    			case 4:
    			strFeet = JOptionPane.showInputDialog("Enter value for Feet: ");
    			feet = Double.parseDouble(strFeet);
    			JOptionPane.showMessageDialog(null, "\n\t\tMiles: " + (feet*0.0001893991176));
    			JOptionPane.showMessageDialog(null, "\t\tKilometer: " + (feet*0.0003048));
    			JOptionPane.showMessageDialog(null, "\t\tMeter: " + (feet*0.3048));
    			JOptionPane.showMessageDialog(null, "\t\tInches: " + (feet*12));
    			JOptionPane.showMessageDialog(null, "\t\tCentimeter: " + (feet*30.48));
    			break;
    
    			case 5:
    			strInches = JOptionPane.showInputDialog("Enter value for Inches: ");
    			inches = Double.parseDouble(strInches);
    			JOptionPane.showMessageDialog(null, "\n\t\tMiles: " + (inches*0.000015783254));
    			JOptionPane.showMessageDialog(null, "\t\tKilometer: " + (inches*0.000025349));
    			JOptionPane.showMessageDialog(null, "\t\tMeter: " + (inches*0.02534));
    			JOptionPane.showMessageDialog(null, "\t\tFeet: " + (inches*0.08333));
    			JOptionPane.showMessageDialog(null, "\t\tCentimeter: " + (inches*2.51));
    			break;
    
    			case 6:
    			strCentimeter = JOptionPane.showInputDialog("Enter value for Centimeter: ");
    			centimeter = Double.parseDouble(strCentimeter);
    			JOptionPane.showMessageDialog(null, "\n\t\tMiles: " + (centimeter*0.000006218797));
    			JOptionPane.showMessageDialog(null, "\t\tKilometer: " + (centimeter*0.000004));
    			JOptionPane.showMessageDialog(null, "\t\tMeter: " + (centimeter*0.004));
    			JOptionPane.showMessageDialog(null, "\t\tFeet: " + (centimeter*0.032808386877));
    			JOptionPane.showMessageDialog(null, "\t\tCentimeter: " + (centimeter*0.3937008));
    			break;
    
    			case 7:
    			done = true;
    			JOptionPane.showMessageDialog(null, "Thank you for using this program!");
    			System.exit(0);
    			break;
    
    			default: throw new NumberFormatException();
    
    			} // end switch
    	} // end while
    } // end main
    
    	//short cut for print
    	private static void print(String s)
    	{
    	System.out.print(s);
    	}
    
    	//short cut for println
    	private static void println(String s)
    	{
    	System.out.println(s);
    	}
    }

  5. #5
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Which ones do you want to change? Those class JLabels, or the method local ones?
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  6. #6
    Join Date
    May 2010
    Posts
    11
    Rep Power
    0

    Default

    Java Code:
    strMiles = JOptionPane.showInputDialog("Enter value for Miles: ");
    miles = Double.parseDouble(strMiles) * 0.6214;
    String totalMiles = Double.toString(miles);
    lblMile.setText(strMiles); //<--- This isn't changing the labels text at all

  7. #7
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    It is, maybe just the wrong one's. You know that you have two labels called lblMile?
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  8. #8
    Join Date
    May 2010
    Posts
    11
    Rep Power
    0

    Default

    yes i know i had to redefine those labels otherwise i kept getting errors

  9. #9
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    That's what you get for putting logic into the main method.
    And you didn't redefine them, you've hidden the one's placed in you GUI.
    Last edited by PhHein; 05-20-2010 at 03:09 PM.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  10. #10
    Join Date
    May 2010
    Posts
    11
    Rep Power
    0

    Default

    How does this help me fix it?

  11. #11
    Sno's Avatar
    Sno
    Sno is offline Senior Member
    Join Date
    Apr 2010
    Posts
    196
    Rep Power
    5

    Default

    What error are you getting with you change 1 of the values, I sense Its attempting to change the converter lblmile
    :rolleyes: ~ Sno ~ :rolleyes:
    '-~ B.S. Computer Science ~-'

  12. #12
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    First of all move the JLabel declaration out of the methods to be able to access them everywhere in your class. Work from there.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  13. #13
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Quote Originally Posted by Sno View Post
    What error are you getting with you change 1 of the values, I sense Its attempting to change the converter lblmile
    He doesn't get any error. The code's fine, but doesn't do what the OP expects.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  14. #14
    Join Date
    May 2010
    Posts
    11
    Rep Power
    0

    Default

    [code]
    import java.io.*;
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;

    public class Converter extends JFrame
    {
    private final int WINDOW_WIDTH = 524; // Window width
    private final int WINDOW_HEIGHT = 80; // Window height

    JLabel Miles = new JLabel("Miles: ");
    JLabel Kilometer = new JLabel("Kilometer: ");
    JLabel Meter = new JLabel("Meter: ");
    JLabel Feet = new JLabel("Feet: ");
    JLabel Inches = new JLabel("Inches: ");
    JLabel Centimeter = new JLabel("Centimeter: ");
    JLabel lblMile = new JLabel();
    JLabel lblKilometer = new JLabel();
    JLabel lblMeter = new JLabel();
    JLabel lblFeet = new JLabel();
    JLabel lblInches = new JLabel();
    JLabel lblCentimeter = new JLabel();

    JPanel panel1 = new JPanel();
    JPanel panel2 = new JPanel();
    JPanel panel3 = new JPanel();
    JPanel panel4 = new JPanel();
    JPanel panel5 = new JPanel();
    JPanel panel6 = new JPanel();
    JPanel panel7 = new JPanel();
    JPanel panel8 = new JPanel();
    JPanel panel9 = new JPanel();
    JPanel panel10 = new JPanel();
    JPanel panel11 = new JPanel();
    JPanel panel12 = new JPanel();

    public Converter()
    {
    setTitle("Length Converter");

    setSize(WINDOW_WIDTH, WINDOW_HEIGHT);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    setLayout(new GridLayout(2, 3));


    JPanel panel1 = new JPanel();
    JPanel panel2 = new JPanel();
    JPanel panel3 = new JPanel();
    JPanel panel4 = new JPanel();
    JPanel panel5 = new JPanel();
    JPanel panel6 = new JPanel();
    JPanel panel7 = new JPanel();
    JPanel panel8 = new JPanel();
    JPanel panel9 = new JPanel();
    JPanel panel10 = new JPanel();
    JPanel panel11 = new JPanel();
    JPanel panel12 = new JPanel();

    panel1.add(Miles);
    panel2.add(Kilometer);
    panel3.add(Meter);
    panel4.add(Feet);
    panel5.add(Inches);
    panel6.add(Centimeter);
    panel7.add(lblMile);
    panel8.add(lblKilometer);
    panel9.add(lblMeter);
    panel10.add(lblFeet);
    panel11.add(lblInches);
    panel12.add(lblCentimeter);



    add(panel1);
    add(panel2);
    add(panel3);
    add(panel4);
    add(panel5);
    add(panel6);
    add(panel7);
    add(panel8);
    add(panel9);
    add(panel10);
    add(panel11);
    add(panel12);

    // Display the window.
    setVisible(true);
    }

    public static void main(String[] args) throws IOException
    {

    int choose;
    double miles, kilometer, meter, feet, inches, centimeter;
    boolean done = false;
    String strChoose;
    String strMiles, strKilometer, strMeter, strFeet, strInches, strCentimeter;
    String totalKilometer;
    String totalMeter;
    String totalFeet;
    String totalInches;
    String totalCentimeter;

    new Converter();



    while (!done)
    {

    strChoose = JOptionPane.showInputDialog("What you would like to convert?" + "\n" + "1) Miles 2) Kilometer 3) Meter"+ "\n"
    + "4) Feet 5) Inches 6) Centimeter" + "\n" + "7) Exit", "Enter Your choice");
    choose = Integer.parseInt(strChoose);

    if ((choose <= 0) || (choose >= 8))
    {
    JOptionPane.showMessageDialog(null, "\tInvalid entry, please try again!");
    }

    switch (choose)
    {
    case 1:
    strMiles = JOptionPane.showInputDialog("Enter value for Miles: ");
    miles = Double.parseDouble(strMiles) * 0.6214;
    String totalMiles = Double.toString(miles);

    lblMile.setText(totalMiles);
    JOptionPane.showMessageDialog(null, "\t\tMeter: " + (miles*1609.3));
    JOptionPane.showMessageDialog(null, "\t\tFeet: " + (miles*5279.85565107));
    JOptionPane.showMessageDialog(null, "\t\tInches: " + (miles*63358.26781284));
    JOptionPane.showMessageDialog(null, "\t\tCentimeter: " + (miles*160930.0002446136));
    break;

    case 2:
    strKilometer = JOptionPane.showInputDialog("Enter value for Kilometer: ");
    kilometer = Double.parseDouble(strKilometer);
    JOptionPane.showMessageDialog(null, "\n\t\tMiles: " + (kilometer*0.6213882));
    JOptionPane.showMessageDialog(null, "\t\tMeter: " + (kilometer*1000));
    JOptionPane.showMessageDialog(null, "\t\tFeet: " + (kilometer*3280.8399));
    JOptionPane.showMessageDialog(null, "\t\tInches: " + (kilometer*39370.0788));
    JOptionPane.showMessageDialog(null, "\t\tCentimeter: " + (kilometer*100000));
    break;

    case 3:
    strMeter = JOptionPane.showInputDialog("Enter value for Meter: ");
    meter = Double.parseDouble(strMeter);
    JOptionPane.showMessageDialog(null, "\n\t\tMiles: " + (meter*621.3882));
    JOptionPane.showMessageDialog(null, "\t\tKilometer: " + (meter*1000));
    JOptionPane.showMessageDialog(null, "\t\tFeet: " + (meter*3.2808399));
    JOptionPane.showMessageDialog(null, "\t\tInches: " + (meter*39.3700788));
    JOptionPane.showMessageDialog(null, "\t\tCentimeter: " + (meter*100));
    break;

    case 4:
    strFeet = JOptionPane.showInputDialog("Enter value for Feet: ");
    feet = Double.parseDouble(strFeet);
    JOptionPane.showMessageDialog(null, "\n\t\tMiles: " + (feet*0.0001893991176));
    JOptionPane.showMessageDialog(null, "\t\tKilometer: " + (feet*0.0003048));
    JOptionPane.showMessageDialog(null, "\t\tMeter: " + (feet*0.3048));
    JOptionPane.showMessageDialog(null, "\t\tInches: " + (feet*12));
    JOptionPane.showMessageDialog(null, "\t\tCentimeter: " + (feet*30.48));
    break;

    case 5:
    strInches = JOptionPane.showInputDialog("Enter value for Inches: ");
    inches = Double.parseDouble(strInches);
    JOptionPane.showMessageDialog(null, "\n\t\tMiles: " + (inches*0.000015783254));
    JOptionPane.showMessageDialog(null, "\t\tKilometer: " + (inches*0.000025349));
    JOptionPane.showMessageDialog(null, "\t\tMeter: " + (inches*0.02534));
    JOptionPane.showMessageDialog(null, "\t\tFeet: " + (inches*0.08333));
    JOptionPane.showMessageDialog(null, "\t\tCentimeter: " + (inches*2.51));
    break;

    case 6:
    strCentimeter = JOptionPane.showInputDialog("Enter value for Centimeter: ");
    centimeter = Double.parseDouble(strCentimeter);
    JOptionPane.showMessageDialog(null, "\n\t\tMiles: " + (centimeter*0.000006218797));
    JOptionPane.showMessageDialog(null, "\t\tKilometer: " + (centimeter*0.000004));
    JOptionPane.showMessageDialog(null, "\t\tMeter: " + (centimeter*0.004));
    JOptionPane.showMessageDialog(null, "\t\tFeet: " + (centimeter*0.032808386877));
    JOptionPane.showMessageDialog(null, "\t\tCentimeter: " + (centimeter*0.3937008));
    break;

    case 7:
    done = true;
    JOptionPane.showMessageDialog(null, "Thank you for using this program!");
    System.exit(0);
    break;

    default: throw new NumberFormatException();

    } // end switch
    } // end while
    } // end main

    //short cut for print
    private static void print(String s)
    {
    System.out.print(s);
    }

    //short cut for println
    private static void println(String s)
    {
    System.out.println(s);
    }
    }
    [\code]
    now i have this error:
    C:\Documents and Settings\bayb\My Documents\Converter.java:130: non-static variable lblMile cannot be referenced from a static context
    lblMile.setText(totalMiles);
    ^
    1 error

    Tool completed with exit code 1

  15. #15
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    1) Please fix your code tags (the second tag uses a slash, not a back-slash). Surely you must be able to see that your code is unreadable. You don't have to re-post the code, just edit your post and you should be able to do it.

    2) You're trying to access instance fields from within the static main method without associating the instance fields with an object, and this will never work.

    3) You need to ask yourself what you're trying to create here:
    a) a GUI program vs. a non-GUI program (all those JOptionPane calls don't count as a true GUI), and
    b) an OOP project or a static project.

    Myself, I recommend an OOP GUI project which means getting all that code out of your main method and getting the logic (not that mess of code with JOptionPane calls) into the class. This would likely require a complete re-write with your creating a GUI that was able to collect the input from the user and then process it.

    Que tenga buena suerte.

  16. #16
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by Tired Of Not Getting Help View Post
    now i have this error:
    C:\Documents and Settings\bayb\My Documents\Converter.java:130: non-static variable lblMile cannot be referenced from a static context
    lblMile.setText(totalMiles);

    this is ok and you can fix it by declaring your JLabel lblMile with

    static JLabel lblMile = new JLabel();

  17. #17
    Join Date
    May 2010
    Posts
    11
    Rep Power
    0

    Default

    Revised:
    Java Code:
    import java.io.*;
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    
    public class Converter extends JFrame
    {
    private final int WINDOW_WIDTH = 524; // Window width
    private final int WINDOW_HEIGHT = 80; // Window height
    
    JLabel Miles = new JLabel("Miles: ");
    JLabel Kilometer = new JLabel("Kilometer: ");
    JLabel Meter = new JLabel("Meter: ");
    JLabel Feet = new JLabel("Feet: ");
    JLabel Inches = new JLabel("Inches: ");
    JLabel Centimeter = new JLabel("Centimeter: ");
    JLabel lblMile = new JLabel();
    JLabel lblKilometer = new JLabel();
    JLabel lblMeter = new JLabel();
    JLabel lblFeet = new JLabel();
    JLabel lblInches = new JLabel();
    JLabel lblCentimeter = new JLabel();
    
    JPanel panel1 = new JPanel();
    JPanel panel2 = new JPanel();
    JPanel panel3 = new JPanel();
    JPanel panel4 = new JPanel();
    JPanel panel5 = new JPanel();
    JPanel panel6 = new JPanel();
    JPanel panel7 = new JPanel();
    JPanel panel8 = new JPanel();
    JPanel panel9 = new JPanel();
    JPanel panel10 = new JPanel();
    JPanel panel11 = new JPanel();
    JPanel panel12 = new JPanel();
    
    public Converter()
    {
    setTitle("Length Converter");
    
    setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
    
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    setLayout(new GridLayout(2, 3));
    
    
    JPanel panel1 = new JPanel();
    JPanel panel2 = new JPanel();
    JPanel panel3 = new JPanel();
    JPanel panel4 = new JPanel();
    JPanel panel5 = new JPanel();
    JPanel panel6 = new JPanel();
    JPanel panel7 = new JPanel();
    JPanel panel8 = new JPanel();
    JPanel panel9 = new JPanel();
    JPanel panel10 = new JPanel();
    JPanel panel11 = new JPanel();
    JPanel panel12 = new JPanel();
    
    panel1.add(Miles);
    panel2.add(Kilometer);
    panel3.add(Meter);
    panel4.add(Feet);
    panel5.add(Inches);
    panel6.add(Centimeter);
    panel7.add(lblMile);
    panel8.add(lblKilometer);
    panel9.add(lblMeter);
    panel10.add(lblFeet);
    panel11.add(lblInches);
    panel12.add(lblCentimeter);
    
    
    
    add(panel1);
    add(panel2);
    add(panel3);
    add(panel4);
    add(panel5);
    add(panel6);
    add(panel7);
    add(panel8);
    add(panel9);
    add(panel10);
    add(panel11);
    add(panel12);
    
    // Display the window.
    setVisible(true);
    }
    
    public static void main(String[] args) throws IOException
    {
    
    int choose;
    double miles, kilometer, meter, feet, inches, centimeter;
    boolean done = false;
    String strChoose;
    String strMiles, strKilometer, strMeter, strFeet, strInches, strCentimeter;
    String totalKilometer;
    String totalMeter;
    String totalFeet;
    String totalInches;
    String totalCentimeter;
    
    new Converter();
    
    
    
    while (!done)
    {
    
    strChoose = JOptionPane.showInputDialog("What you would like to convert?" + "\n" + "1) Miles 2) Kilometer 3) Meter"+ "\n"
    + "4) Feet 5) Inches 6) Centimeter" + "\n" + "7) Exit", "Enter Your choice");
    choose = Integer.parseInt(strChoose);
    
    if ((choose <= 0) || (choose >= 8))
    {
    JOptionPane.showMessageDialog(null, "\tInvalid entry, please try again!");
    }
    
    switch (choose)
    {
    case 1:
    strMiles = JOptionPane.showInputDialog("Enter value for Miles: ");
    miles = Double.parseDouble(strMiles) * 0.6214;
    String totalMiles = Double.toString(miles);
    
    lblMile.setText(totalMiles);
    JOptionPane.showMessageDialog(null, "\t\tMeter: " + (miles*1609.3));
    JOptionPane.showMessageDialog(null, "\t\tFeet: " + (miles*5279.85565107));
    JOptionPane.showMessageDialog(null, "\t\tInches: " + (miles*63358.26781284));
    JOptionPane.showMessageDialog(null, "\t\tCentimeter: " + (miles*160930.0002446136));
    break;
    
    case 2:
    strKilometer = JOptionPane.showInputDialog("Enter value for Kilometer: ");
    kilometer = Double.parseDouble(strKilometer);
    JOptionPane.showMessageDialog(null, "\n\t\tMiles: " + (kilometer*0.6213882));
    JOptionPane.showMessageDialog(null, "\t\tMeter: " + (kilometer*1000));
    JOptionPane.showMessageDialog(null, "\t\tFeet: " + (kilometer*3280.8399));
    JOptionPane.showMessageDialog(null, "\t\tInches: " + (kilometer*39370.0788));
    JOptionPane.showMessageDialog(null, "\t\tCentimeter: " + (kilometer*100000));
    break;
    
    case 3:
    strMeter = JOptionPane.showInputDialog("Enter value for Meter: ");
    meter = Double.parseDouble(strMeter);
    JOptionPane.showMessageDialog(null, "\n\t\tMiles: " + (meter*621.3882));
    JOptionPane.showMessageDialog(null, "\t\tKilometer: " + (meter*1000));
    JOptionPane.showMessageDialog(null, "\t\tFeet: " + (meter*3.2808399));
    JOptionPane.showMessageDialog(null, "\t\tInches: " + (meter*39.3700788));
    JOptionPane.showMessageDialog(null, "\t\tCentimeter: " + (meter*100));
    break;
    
    case 4:
    strFeet = JOptionPane.showInputDialog("Enter value for Feet: ");
    feet = Double.parseDouble(strFeet);
    JOptionPane.showMessageDialog(null, "\n\t\tMiles: " + (feet*0.0001893991176));
    JOptionPane.showMessageDialog(null, "\t\tKilometer: " + (feet*0.0003048));
    JOptionPane.showMessageDialog(null, "\t\tMeter: " + (feet*0.3048));
    JOptionPane.showMessageDialog(null, "\t\tInches: " + (feet*12));
    JOptionPane.showMessageDialog(null, "\t\tCentimeter: " + (feet*30.48));
    break;
    
    case 5:
    strInches = JOptionPane.showInputDialog("Enter value for Inches: ");
    inches = Double.parseDouble(strInches);
    JOptionPane.showMessageDialog(null, "\n\t\tMiles: " + (inches*0.000015783254));
    JOptionPane.showMessageDialog(null, "\t\tKilometer: " + (inches*0.000025349));
    JOptionPane.showMessageDialog(null, "\t\tMeter: " + (inches*0.02534));
    JOptionPane.showMessageDialog(null, "\t\tFeet: " + (inches*0.08333));
    JOptionPane.showMessageDialog(null, "\t\tCentimeter: " + (inches*2.51));
    break;
    
    case 6:
    strCentimeter = JOptionPane.showInputDialog("Enter value for Centimeter: ");
    centimeter = Double.parseDouble(strCentimeter);
    JOptionPane.showMessageDialog(null, "\n\t\tMiles: " + (centimeter*0.000006218797));
    JOptionPane.showMessageDialog(null, "\t\tKilometer: " + (centimeter*0.000004));
    JOptionPane.showMessageDialog(null, "\t\tMeter: " + (centimeter*0.004));
    JOptionPane.showMessageDialog(null, "\t\tFeet: " + (centimeter*0.032808386877));
    JOptionPane.showMessageDialog(null, "\t\tCentimeter: " + (centimeter*0.3937008));
    break;
    
    case 7:
    done = true;
    JOptionPane.showMessageDialog(null, "Thank you for using this program!");
    System.exit(0);
    break;
    
    default: throw new NumberFormatException();
    
    } // end switch
    } // end while
    } // end main
    
    //short cut for print
    private static void print(String s)
    {
    System.out.print(s);
    }
    
    //short cut for println
    private static void println(String s)
    {
    System.out.println(s);
    }
    }
    now i have this error:
    C:\Documents and Settings\bayb\My Documents\Converter.java:130: non-static variable lblMile cannot be referenced from a static context
    lblMile.setText(totalMiles);
    ^
    1 error

    Tool completed with exit code 1

  18. #18
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by j2me64 View Post
    this is ok and you can fix it by declaring your JLabel lblMile with
    static JLabel lblMile = new JLabel();
    You can't be serious? Really? Do you really want to give advice to fix the symptom not the disease?

  19. #19
    Join Date
    May 2010
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by j2me64 View Post
    this is ok and you can fix it by declaring your JLabel lblMile with

    static JLabel lblMile = new JLabel();
    Thanks Program is FULLY working thanks to you.

  20. #20
    Join Date
    May 2010
    Posts
    11
    Rep Power
    0

    Default

    how do i go about coding a splashscreen in this code, i found guides on how to but is there an easier way to code it?

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 4
    Last Post: 04-06-2010, 11:06 AM
  2. execute java program within java program
    By popey in forum New To Java
    Replies: 2
    Last Post: 10-22-2009, 05:32 PM
  3. New to Java, need help with program
    By javakid in forum New To Java
    Replies: 7
    Last Post: 12-05-2008, 04:16 AM
  4. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  5. Replies: 0
    Last Post: 10-04-2007, 09:33 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
  •