Home Answers Viewqa Java-Beginners GUI and how to convert a distance

 
 


zam
GUI and how to convert a distance
1 Answer(s)      3 years and 2 months ago
Posted in : Java Beginners

i need help..
how to create a GUI application that can be is used to convert a distance unit in miles into its corresponding units in kilometer, meter, inches and centimeter. When a user enters a mile value in a text field and followed by pressing a button, its equivalent values in unit of kilometer, meter, inches and centimeter will be displayed in the corresponding text fields. If the user enters the value of miles that is less or equals to zero, a dialogue error message that contains a message ?The value must be greater than zero? will be displayed.
The formula for each of unit conversion is as follows:
1 mile = 2.5 km
1 km = 1000 m
1 meter = 60 inches
1 inches = 2.54 m

--------
i'm use netbeans soffware..
View Answers

March 15, 2010 at 5:31 PM


Hi Friend,

Through the following values, we have converted miles into different units.
1 mile=1.609344, 1 meter=39.3700787 inches and 1 inch=0.0254 meter.

Try the following:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.regex.*;
import java.text.*;

class ConvertMile extends JFrame{
JTextField t1,t2,t3,t4,t5;
JLabel l1,l2, l3, l4, l5;
JPanel p,p1,p2;
JButton b;
ConvertMile(){
p=new JPanel(new GridLayout(6,2));
p1=new JPanel(new GridLayout(2,2));
p2=new JPanel(new GridLayout(4,2));
t1=new JTextField(20);
t2=new JTextField(20);
t3=new JTextField(20);
t4=new JTextField(20);
t5=new JTextField(20);
l1=new JLabel("Enter distance( in Miles)");
l2=new JLabel("In KM");
l3=new JLabel("In M");
l4=new JLabel("In Inches");
l5=new JLabel("In CM");
b=new JButton("Convert");
t1.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
String input=t1.getText();
Pattern p = Pattern.compile("[A-Z,a-z,&%$#@!()*^]");
Matcher m = p.matcher(input);
if (m.find()) {
JOptionPane.showMessageDialog(null, "Please enter only numbers");
}
}
});
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String value=t1.getText();
double distance=Double.parseDouble(value);
if(distance<=0){
JOptionPane.showMessageDialog(null,"The value must be greater than zero");
}
else{
double km=distance * 1.609344;
double m=km * 1000;
double cm=m * 100;
double inch=m * 39.3700787;
DecimalFormat f = new DecimalFormat("##.### ");
t2.setText(f.format(km)+" KM");
t3.setText(f.format(m)+" M");
t4.setText(f.format(inch)+" Inches");
t5.setText(f.format(cm)+" CM");

}
}
});
p1.add(l1);
p1.add(t1);
p1.add(b);

p2.add(l2);
p2.add(t2);
p2.add(l3);
p2.add(t3);
p2.add(l4);
p2.add(t4);
p2.add(l5);
p2.add(t5);
p.add(p1);
p.add(p2);
add(p);
setVisible(true);
pack();
}
public static void main(String[] args)
{

ConvertMile m=new ConvertMile();
}
}
Thanks









Related Pages:
GUI and how to convert a distance - Java Beginners
GUI and how to convert a distance  i need help.. how to create a GUI application that can be is used to convert a distance unit in miles into its...=new JTextField(20); t5=new JTextField(20); l1=new JLabel("Enter distance
Convert the code to GUI
How to Convert the code to GUI   How to convert a code into GUI
Convert the code to GUI
Convert the code   How to convert a code to GUI look alike
HOW TO CONVERT THIS CODE INTO GUI
HOW TO CONVERT THIS CODE INTO GUI   System.out.println("\n\t UGANDA CHRISTIAN UNIVERSITY\n"); System.out.println("\n\tFACULTY OF SCIENCE AND TECHNOLOGY\n"); System.out.println("\n BACHELOR OF SCIENCE IN COMPUTER
Convert the code to GUI
How to create GUI application in Java   How to create GUI application in Java
How to convert this Java code into GUI?
How to convert this Java code into GUI?   import java.util.Scanner; public class StudentMarks { double totalMarks; String grade; public void setTotalMarks(double totalMarks) { this.totalMarks = totalMarks
Edit Distance
Edit Distance  I want java programming ask from user input two string and the program find the edit distance between two strings and find table and optimal solution using GUI
convert to GUI
convert to GUI  import java.util.Scanner; public class Exam { public static void main (String args[]) { int numberStudent, mark, markAplus = 0, markA = 0, markBplus = 0, markB = 0, markBminus = 0, i; int markCplus = 0, markC
GUI convert to celsius program - Java Beginners
GUI convert to celsius program  how to write java GUI program to convert Fahrenheit to Celsius that need user to input value of Fahrenheit then click button convert and value of Celsius will display as a output.   Hi
Convert the code to GUI
Is it possible to convert a code into GUI  Is it possible to convert a code into GUI
GUI
GUI  How to GUI in Net-beans ... ??   Please visit the following link: http://www.roseindia.net/java/java-tips/background/30java_tools/netbeans.shtml
Convert the code to GUI
GUI code  GUI code
Convert the code to GUI
Java Code to GUI   can any one convert My code to GUI code
Convert the code to GUI
GUI Application example  GUI Application example
Convert the code to GUI
GUI Example  GUI Example code to learn
Convert the code to GUI
Java GUI Class Example  Java GUI Class Example
Convert the code to GUI
GUI Java JSP application  GUI Java JSP application
Convert the code to GUI
GUI Application Development   GUI Application Development
Convert the code to GUI
Java and GUI application Example  Java and GUI application Example
Convert the code to GUI
Write a GUI Application  best way to write a GUI based application
Distance in meters - Java Beginners
the program The program will convert the distance to kilometers, inches...?s input is shown in bold. Enter a distance in meters: 500 1. Convert...Distance in meters  Write a program that asks the user to enter
Distance conversion - Java Beginners
the user. ? Write three methods to convert the distance to kilometres, feet and inches...Distance conversion  Write an application that reads a distance... the distance to kilometres, feet and inches, according to the user?s selection
Convert the code to GUI ??
Convert the code to GUI ??  hi >> can anyone help me to conver this code to GUI ?? /** * @(#)RegistorClass.java * *. * @author * @version 1.00 2011/12/16 */ import java.util.Scanner; public class RegistorClass
Convert the code to GUI
Convert the code to GUI   can any one convert My code to GUI code import java.util.Scanner; public class RegistorClass { private int accumulator; private int instructionCounter; private int instructionRegister
Convert the code to GUI
GUI example for beginners  GUI example for beginners  sory ,, I will posted my code again import java.util.Scanner; public class RegistorClass { private int accumulator; private int instructionCounter; private
Another distance conversion - Java Beginners
Another distance conversion  Write an application that reads a distance in metres. The program will then convert the distance to kilometres, feet...[]){ System.out.print("Enter distance in meters : "); BufferedReader br = new BufferedReader(new
convert this code to GUI
convert this code to GUI  hello.. this is my code.. import java.util.Scanner; public class StudentGrade { double totalMarks; String grade; public void setTotalMarks(double totalMarks) { this.totalMarks
convert this code to GUI
convert this code to GUI  import java.util.Scanner; public class StudentGrade { double totalMarks; String grade; public void setTotalMarks(double totalMarks) { this.totalMarks = totalMarks; //"this" keyword
convert this code to GUI
convert this code to GUI  import java.util.Scanner; public class StudentGrade { double totalMarks; String grade; public void setTotalMarks(double totalMarks) { this.totalMarks = totalMarks; //"this" keyword
convert this code to GUI
convert this code to GUI  import java.util.*; class Author{ public String name; public BookList<Book>books=new BookList<Book>(); public Author(){ } public boolean equals(Object node){ return name.equals
convert this code to GUI
convert this code to GUI  hello.. this is my code.. import java.util.Scanner; public class StudentGrade { double totalMarks; String grade; public void setTotalMarks(double totalMarks) { this.totalMarks
convert this code to GUI
convert this code to GUI  hello.. this is my code.. import java.util.Scanner; public class StudentGrade { double totalMarks; String grade; public void setTotalMarks(double totalMarks) { this.totalMarks
convert this code to GUI
convert this code to GUI  import java.util.Scanner; public class StudentGrade { double totalMarks; String grade; public void setTotalMarks(double totalMarks) { this.totalMarks = totalMarks; //"this" keyword
convert this code to GUI
convert this code to GUI  import java.util.Scanner; public class StudentGrade { double totalMarks; String grade; public void setTotalMarks(double totalMarks) { this.totalMarks = totalMarks; //"this" keyword
convert this code to GUI
convert this code to GUI  import java.util.Scanner; public class StudentGrade { double totalMarks; String grade; public void setTotalMarks(double totalMarks) { this.totalMarks = totalMarks; //"this" keyword
convert this code to GUI
convert this code to GUI  import java.util.Scanner; public class StudentGrade { double totalMarks; String grade; public void setTotalMarks(double totalMarks) { this.totalMarks = totalMarks; //"this" keyword
convert this code to GUI
convert this code to GUI  import java.util.Scanner; public class StudentGrade { double totalMarks; String grade; public void setTotalMarks(double totalMarks) { this.totalMarks = totalMarks; //"this" keyword
convert this code to GUI
convert this code to GUI  import java.util.*; class Author{ public String name; public BookList<Book>books=new BookList<Book>(); public Author(){ } public boolean equals(Object node){ return name.equals
Convert this code to GUI - Java Beginners
); } }  hi friend, We have convert your code into GUI...Convert this code to GUI  I have written this.i need to convert the following code to GUI:- import java.awt.*; import java.applet.*; import
GUI component
GUI component  How can a GUI component handle its own events
Road distance using Java - Java Beginners
Road distance using Java  Dear All i am calculating the straight line distance between two latlong points using the simple mathematical formula but i need the Real Road Driving Distance // protected double distance(double pX
how to refresh my GUI page
how to refresh my GUI page  how to refresh a GUI in java
java gui
java gui   friends... good day.. i have doubt in java gui. ? i created 1 java gui application. That has two text fields jtext1,jtext2. case: user... must go to the next text field(jtext2). How can i arrange this. which code can i
"Urgent" convert this code to GUI - Swing AWT
"Urgent" convert this code to GUI   please convert for me this code to GUI THE CODES ARE BELOW, CLASS ATMCaseStudy is the driver class that runs all the other classes. its very urgent. //CLASS CashDispenser public class
please convert for me this code to GUI - Swing AWT
please convert for me this code to GUI    THE CODES ARE BELOW, CLASS ATMCaseStudy is the driver class that runs all the other classes. its very urgent. // class ATM import javax.swing.*; import java.awt.*; public
please convert for me this code to GUI - Swing AWT
please convert for me this code to GUI  THE CODES ARE BELOW, CLASS ATMCaseStudy is the driver class that runs all the other classes. its very urgent. // class ATM import javax.swing.*; import java.awt.*; public class ATM
GUI problem
GUI problem  How do I make a Jbutton which is shaped like a circle. This button needs to be clicked in order to change color.   import java.awt.*; import java.awt.geom.*; import javax.swing.*; public class
how to do gui
is RM" + num1); } } } how to do gui for this coding..tq   import...how to do gui  import java.util.Scanner; import javax.swing.JFrame; import javax.swing.JOptionPane; public class ShippingCost { public static void
Convert Class File to .java File - Java Beginners
Convert Class File to .java File  Sir, How To Convert .class file to .java File. Please Help me  Use cavaj software ,which is freeware, to convert .class to .java  Use jd-gui to convert ur .class to .java
class distance
class distance  class distance-integer data member km1,km2,km3,m1,m2,m3. run()-to input the two distance and add them in the third. display()-to display the total distance. display the constructor if required

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.