/* This is the conversion for length * */ import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.text.DecimalFormat; import java.text.NumberFormat; public class ConvertLength extends JFrame { private JPanel mainpanel, txtpanel, buttonpanel, labelpanel; private JButton convert, exit, back, clear; private JTextField txtfield; private JLabel title; private JComboBox jcombo; private double number; private double cmTotal = 0.0, inchTotal = 0.0, mTotal = 0.0, mmTotal = 0.0; private double kmTotal = 0.0, yaTotal = 0.0, ftTotal = 0.0, miTotal = 0.0; private boolean value = true; public ConvertLength() { // Sets the title of the window setTitle("Convert Lengths"); // Sets the size of the window setSize(400, 400); // Sets the start location of the window setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Creates a main panel & set the layout to border layout mainpanel = new JPanel(); mainpanel.setLayout(new BorderLayout()); // Creates a text panel txtpanel = new JPanel(); // Create a label title = new JLabel("Convert From"); // Creates a text field txtfield = new JTextField("", 20); // Create a JCombo Box jcombo = new JComboBox(); // Add items to the JCombo Box jcombo.addItem("inch"); jcombo.addItem("cm"); jcombo.addItem("ft"); jcombo.addItem("yard"); jcombo.addItem("mile"); jcombo.addItem("mm"); jcombo.addItem("m"); jcombo.addItem("km"); // Add the text area, JCombo & the label to the text panel txtpanel.add(title); txtpanel.add(txtfield); txtpanel.add(jcombo); // Add the text panel to the main panel mainpanel.add(txtpanel, BorderLayout.NORTH); // Create the buttons convert = new JButton("Convert"); exit = new JButton("Exit"); back = new JButton("Back"); clear = new JButton("Clear"); // Add the buttons to the panel buttonpanel = new JPanel(); buttonpanel.add(convert); buttonpanel.add(exit); buttonpanel.add(clear); buttonpanel.add(back); // Create the action listener for the buttons convert.addActionListener(new ButtonListener()); exit.addActionListener(new ButtonListener()); back.addActionListener(new ButtonListener()); clear.addActionListener(new ButtonListener()); // Add the button panel to the main panel mainpanel.add(buttonpanel, BorderLayout.SOUTH); // Add the main panel to the content pane add(mainpanel); setVisible(value); } private class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { /* All the calculations are done in here * */ try { NumberFormat formatter = new DecimalFormat("#0.000"); NumberFormat formatter2 = new DecimalFormat("#0.000000"); if(e.getSource() == convert) { String select = (String)jcombo.getSelectedItem(); if(select.equals("inch")) { number = Double.parseDouble(txtfield.getText()); cmTotal = number * 2.54; mTotal = number / 39.37; mmTotal = number * 25.4; ftTotal = number / 12; yaTotal = ftTotal / 3; miTotal = ftTotal / 5280; kmTotal = miTotal * 1.609; JOptionPane.showMessageDialog(null, formatter.format(cmTotal)+ " cm \n" + formatter.format(mTotal) +" m\n" + formatter.format(mmTotal) + " mm\n " + formatter.format(ftTotal)+ " ft\n" + formatter.format(yaTotal) + " yard\n" + formatter2.format(miTotal) + " mile\n" + formatter2.format(kmTotal) + " km"); number = 0.0; } if(select.equals("cm")) { number = Double.parseDouble(txtfield.getText()); inchTotal = number / 2.54; mTotal = number / 100; mmTotal = number * 10; ftTotal = inchTotal / 12; yaTotal = ftTotal / 3; miTotal = ftTotal / 5280; kmTotal = miTotal * 1.609; JOptionPane.showMessageDialog(null, formatter.format(inchTotal)+ " inch \n" + formatter.format(mTotal) +" m\n" + formatter.format(mmTotal) + " mm\n " + formatter2.format(ftTotal)+ " ft\n" + formatter.format(yaTotal) + " yard\n" + formatter2.format(miTotal) + " mile\n" + formatter2.format(kmTotal) + " km"); number = 0.0; } if(select.equals("ft")) { number = Double.parseDouble(txtfield.getText()); inchTotal = number * 12; yaTotal = number / 3; miTotal = number / 5280; kmTotal = miTotal * 1.609; cmTotal = inchTotal * 2.54; mTotal = cmTotal / 100; mmTotal = cmTotal * 10; JOptionPane.showMessageDialog(null, formatter.format(inchTotal)+ " inch \n" + formatter.format(mTotal) +" m\n" + formatter.format(mmTotal) + " mm\n " + formatter.format(cmTotal)+ " ft\n" + formatter.format(yaTotal) + " yard\n" + formatter2.format(miTotal) + " mile\n" + formatter2.format(kmTotal) + " km"); number = 0.0; } if(select.equals("yard")) { number = Double.parseDouble(txtfield.getText()); inchTotal = number * 36; ftTotal = number * 3; miTotal = ftTotal / 5280; kmTotal = miTotal * 1.609; cmTotal = inchTotal * 2.54; mTotal = cmTotal / 100; mmTotal = cmTotal * 10; JOptionPane.showMessageDialog(null, formatter.format(inchTotal)+ " inch \n" + formatter.format(mTotal) +" m\n" + formatter.format(mmTotal) + " mm\n " + formatter2.format(ftTotal)+ " ft\n" + formatter.format(cmTotal) + " cm\n" + formatter2.format(miTotal) + " mile\n" + formatter2.format(kmTotal) + " km"); number = 0.0; } if(select.equals("mile")) { number = Double.parseDouble(txtfield.getText()); ftTotal = number * 5280; kmTotal = number * 1.609; yaTotal = ftTotal * 3; inchTotal = yaTotal * 36; cmTotal = inchTotal / 100; mTotal = cmTotal / 100; mmTotal = cmTotal * 10; JOptionPane.showMessageDialog(null, formatter.format(inchTotal)+ " inch \n" + formatter.format(mTotal) +" m\n" + formatter.format(mmTotal) + " mm\n " + formatter2.format(ftTotal)+ " ft\n" + formatter.format(cmTotal) + " cm\n" + formatter2.format(yaTotal) + " yard\n" + formatter2.format(kmTotal) + " km"); number = 0.0; } if(select.equals("mm")) { number = Double.parseDouble(txtfield.getText()); cmTotal = number / 10; mTotal = cmTotal / 100; inchTotal = cmTotal / 2.54; yaTotal = inchTotal / 36; ftTotal = inchTotal / 12; miTotal = ftTotal / 5280; kmTotal = miTotal * 1.609; JOptionPane.showMessageDialog(null, formatter.format(inchTotal)+ " inch \n" + formatter.format(mTotal) +" m\n" + formatter2.format(miTotal) + " mile\n " + formatter2.format(ftTotal)+ " ft\n" + formatter.format(cmTotal) + " cm\n" + formatter2.format(yaTotal) + " yard\n" + formatter2.format(kmTotal) + " km"); number = 0.0; } if(select.equals("m")) { number = Double.parseDouble(txtfield.getText()); cmTotal = number * 100; mmTotal = cmTotal * 10; inchTotal = cmTotal / 2.54; yaTotal = inchTotal / 36; ftTotal = inchTotal / 12; miTotal = ftTotal / 5280; kmTotal = miTotal * 1.609; JOptionPane.showMessageDialog(null, formatter.format(inchTotal)+ " inch \n" + formatter.format(mmTotal) +" mm\n" + formatter2.format(miTotal) + " mile\n " + formatter2.format(ftTotal)+ " ft\n" + formatter.format(cmTotal) + " cm\n" + formatter2.format(yaTotal) + " yard\n" + formatter2.format(kmTotal) + " km"); number = 0.0; } if(select.equals("km")) { number = Double.parseDouble(txtfield.getText()); mTotal = number * 1000; cmTotal = mTotal * 100; mmTotal = cmTotal * 10; inchTotal = cmTotal / 2.54; yaTotal = inchTotal / 36; ftTotal = inchTotal /12; miTotal = ftTotal / 5280; JOptionPane.showMessageDialog(null, formatter.format(inchTotal)+ " inch \n" + formatter.format(mmTotal) +" mm\n" + formatter2.format(miTotal) + " mile\n " + formatter2.format(ftTotal)+ " ft\n" + formatter.format(cmTotal) + " cm\n" + formatter2.format(yaTotal) + " yard\n" + formatter2.format(mTotal) + " m"); number = 0.0; } } if(e.getSource() == back) { value = false; setVisible(value); new ConvertSelection(); } if(e.getSource() == clear) { txtfield.setText(""); } if(e.getSource() == exit) { JOptionPane.showMessageDialog(null, "Thank you for using the program"); System.exit(0); } } catch(NumberFormatException nf) { } } } }