a simple java program

Metric Convertor GUI

“Metric Convertor GUI” is a simple java program to convert some metric units.

The MetricConvertor Class:


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
import javax.swing.border.*;

public class MetricConvertor extends JFrame implements ActionListener {

//first components inch to cm and vice versa
private JTextField cmText = new JTextField(6);
private JLabel cmLabel = new JLabel("Cm");
private JButton cmToInchButton = new JButton(" ===> ");
private JButton inchToCmButton = new JButton(" <=== ");
private JPanel cmInchButtons = new JPanel();
private JTextField inchText = new JTextField(6);
private JLabel inchLabel = new JLabel("Inches");
private JPanel cmInchPanel = new JPanel();

//Second components Miles to KM and vice versa
private JTextField kmText = new JTextField(6);
private JLabel kmLabel = new JLabel("Km");
private JButton kmToMileButton = new JButton(" ===> ");
private JButton mileToKmButton = new JButton(" <=== ");
private JPanel mileKmButtons = new JPanel();
private JTextField mileText = new JTextField(6);
private JLabel mileLabel = new JLabel("Miles "); // extra spaces make all labels the same length
private JPanel mileKmPanel = new JPanel();

//Third components pounds to KG and vice versa
private JTextField kgText = new JTextField(6);
private JLabel kgLabel = new JLabel("Kg "); // extra spaces make all labels the same length
private JButton kgToPoundButton = new JButton(" ===> ");
private JButton poundToKgButton = new JButton(" <=== ");
private JPanel poundKgButtons = new JPanel();
private JTextField poundText = new JTextField(6);
private JLabel poundLabel = new JLabel("Lb "); // extra spaces make all labels the same length
private JPanel poundKgPanel = new JPanel();

//Forth components Celsius to Fahrenheit and vice versa
private JTextField celText = new JTextField(6);
private JLabel celLabel = new JLabel("Ce ");
private JButton celToFahrButton = new JButton(" ===> ");
private JButton fahrToCelButton = new JButton(" <=== ");
private JPanel celFahrButtons = new JPanel();
private JTextField fahrText = new JTextField(6);
private JLabel fahrLabel = new JLabel("F "); // extra spaces make all labels the same length
private JPanel fahrCelPanel = new JPanel();
//Methods
public MetricConvertor() {
cmInchButtons.setLayout(new BorderLayout());
cmInchButtons.add("North", cmToInchButton);
cmInchButtons.add("South", inchToCmButton);
cmInchPanel.add(cmText);
cmInchPanel.add(cmLabel);
cmInchPanel.add(cmInchButtons);
cmInchPanel.add(inchText);
cmInchPanel.add(inchLabel);

mileKmButtons.setLayout(new BorderLayout());
mileKmButtons.add("North", kmToMileButton);
mileKmButtons.add("South", mileToKmButton);
mileKmPanel.add(kmText);
mileKmPanel.add(kmLabel);
mileKmPanel.add(mileKmButtons);
mileKmPanel.add(mileText);
mileKmPanel.add(mileLabel);

poundKgButtons.setLayout(new BorderLayout());
poundKgButtons.add("North", kgToPoundButton);
poundKgButtons.add("South", poundToKgButton);
poundKgPanel.add(kgText);
poundKgPanel.add(kgLabel);
poundKgPanel.add(poundKgButtons);
poundKgPanel.add(poundText);
poundKgPanel.add(poundLabel);

celFahrButtons.setLayout(new BorderLayout());
celFahrButtons.add("North", celToFahrButton);
celFahrButtons.add("South", fahrToCelButton);
fahrCelPanel.add(celText);
fahrCelPanel.add(celLabel);
fahrCelPanel.add(celFahrButtons);
fahrCelPanel.add(fahrText);
fahrCelPanel.add(fahrLabel);

add(cmInchPanel);
add(mileKmPanel);
add(poundKgPanel);
add(fahrCelPanel);

// give raised borders to the buttons
cmToInchButton.setBorder(new BevelBorder(BevelBorder.RAISED));
inchToCmButton.setBorder(new BevelBorder(BevelBorder.RAISED));
kmToMileButton.setBorder(new BevelBorder(BevelBorder.RAISED));
mileToKmButton.setBorder(new BevelBorder(BevelBorder.RAISED));
kgToPoundButton.setBorder(new BevelBorder(BevelBorder.RAISED));
poundToKgButton.setBorder(new BevelBorder(BevelBorder.RAISED));
celToFahrButton.setBorder(new BevelBorder(BevelBorder.RAISED));
fahrToCelButton.setBorder(new BevelBorder(BevelBorder.RAISED));

cmToInchButton.addActionListener(this);
inchToCmButton.addActionListener(this);
kmToMileButton.addActionListener(this);
mileToKmButton.addActionListener(this);
kgToPoundButton.addActionListener(this);
poundToKgButton.addActionListener(this);
celToFahrButton.addActionListener(this);
fahrToCelButton.addActionListener(this);

setTitle("Metric Convertor");
setLayout(new FlowLayout());
setSize(320,300);
setLocation(200,200);
setVisible(true);

}

public void actionPerformed(ActionEvent e) {
double d;
String s;
DecimalFormat df = new DecimalFormat("0.0#");

if(e.getSource() == cmToInchButton) {
s = cmText.getText();
d = Double.parseDouble(s);
d = d / 2.54; // convert to inches
s = df.format(d);
inchText.setText(s);
}
if (e.getSource() == inchToCmButton) // convert inches to cm
{
s = new String(inchText.getText());
d = Double.parseDouble(s); // convert String to double
d = d * 2.54; // convert to cm
s = df.format(d); // format number for output
cmText.setText(s);
}
if (e.getSource() == kmToMileButton) // convert km to miles
{
s = new String(kmText.getText());
d = Double.parseDouble(s); // convert String to double
d = d / 1.609; // convert to miles
s = df.format(d); // format number for output
mileText.setText(s);
}
if (e.getSource() == mileToKmButton) // convert miles to km
{
s = new String(mileText.getText());
d = Double.parseDouble(s); // convert String to double
d = d * 1.609; // convert to km
s = df.format(d); // format number for output
kmText.setText(s);
}
if (e.getSource() == kgToPoundButton) // convert kg to pounds
{
s = new String(kgText.getText());
d = Double.parseDouble(s); // convert String to double
d = d * 2.2; // convert to pounds
s = df.format(d); // format number for output
poundText.setText(s);
}
if (e.getSource() == poundToKgButton) // convert pounds to kg
{
s = new String(poundText.getText());
d = Double.parseDouble(s); // convert String to double
d = d / 2.2; // convert to kg
s = df.format(d); // format number for output
kgText.setText(s);
}
if (e.getSource() == celToFahrButton) // convert pounds to kg
{
s = new String(celText.getText());
d = Double.parseDouble(s); // convert String to double
d = (d * (9/5)) + 32; // convert to Fahrenheit
s = df.format(d); // format number for output
fahrText.setText(s);
}
if (e.getSource() == fahrToCelButton) // convert pounds to kg
{
s = new String(fahrText.getText());
d = Double.parseDouble(s); // convert String to double
d = (d - 32) * 5/9; // convert to Fahrenheit
s = df.format(d); // format number for output
celText.setText(s);
}
}

}

The MetricConvertorTester:


public class MetricConvertorTester {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new MetricConvertor();

}

}

Leave a Reply

Your email address will not be published. Required fields are marked *