0% found this document useful (0 votes)
9 views6 pages

Swing Metric Conversion App Code

Uploaded by

balagomathi003
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views6 pages

Swing Metric Conversion App Code

Uploaded by

balagomathi003
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

BUILD A SWING APPLICATION TO IMPLEMENT METRIC CONVERSION

SOURCE CODE
import [Link].*;
import [Link].*;
import [Link].*;

public class ConversionProgramSwing extends JFrame implements ItemListener,


ActionListener {
private JComboBox<String> conversionChoice, choice2;
private JTextField inputField, resultField;
private JButton convertButton;

public ConversionProgramSwing() {
setLayout(new FlowLayout());

// Create and add the dropdown (JComboBox)


conversionChoice = new JComboBox<>();
[Link]("Select Conversion");
[Link]("Kilometers");
[Link]("Kilograms");
[Link]("Celsius");
[Link]("Feet");
[Link]("Liters");
add(conversionChoice);

choice2 = new JComboBox<>();


add(choice2);

// Add input field


inputField = new JTextField(10);
add(inputField);

// Add convert button


convertButton = new JButton("Convert");
add(convertButton);

// Add result field


resultField = new JTextField(20);
[Link](false);
add(resultField);

// Set up event listeners


[Link](this);
[Link](this);

setTitle("Metric Conversion");
setSize(400, 250);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}

public void itemStateChanged(ItemEvent ie) {


[Link]("");
[Link]("");
if ([Link]() == conversionChoice) {
String sitem = (String) [Link]();
[Link]();
if ([Link]("Kilometers")) {
[Link]("Meter");
[Link]("Centimeter");
[Link]("Miles");
}
if ([Link]("Kilograms")) {
[Link]("Grams");
}
if ([Link]("Celsius")) {
[Link]("Fahrenheit");
}
if ([Link]("Feet")) {
[Link]("Inches");
}
if ([Link]("Liters")) {
[Link]("Milliliters");
}
}
}

public void actionPerformed(ActionEvent ae) {


try {
String s1 = (String) [Link]();
String s2 = (String) [Link]();
double inputValue = [Link]([Link]());
double result = 0;

if (s1 != null && s2 != null && [Link]() != 0 && [Link]() != 0) {


if ([Link]("Kilometers") && [Link]("Meter")) {
result = inputValue * 1000;
}
if ([Link]("Kilometers") && [Link]("Centimeter")) {
result = inputValue * 100000;
}
if ([Link]("Kilograms") && [Link]("Grams")) {
result = inputValue * 1000;
}
if ([Link]("Celsius") && [Link]("Fahrenheit")) {
result = (inputValue * 9 / 5) + 32;
}
if ([Link]("Kilometers") && [Link]("Miles")) {
result = inputValue * 0.621371;
}
if ([Link]("Feet") && [Link]("Inches")) {
result = inputValue * 12;
}
if ([Link]("Liters") && [Link]("Milliliters")) {
result = inputValue * 1000;
}
} else {
[Link]("Please select a valid conversion");
return;
}
[Link]([Link](result));
} catch (NumberFormatException e) {
[Link]("Invalid input");
}
}

public static void main(String[] args) {


new ConversionProgramSwing();
}
}
OUTPUT
Kilometers to Meter

Kilometera to Centimeter

Kilometers to Miles

Kilograms to Grams
Celsius to Fahrenheit

Feet to Inches

Liters to Milliliters

You might also like