/* This is the conversion for volume * */ import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.text.DecimalFormat; import java.text.NumberFormat; public class ConvertVolume 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 literTotal = 0.0, mlTotal = 0.0, galTotal = 0.0; private boolean value = true; public ConvertVolume() { // Sets the title of the window setTitle("Convert Volume"); // 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("liter"); jcombo.addItem("ml"); jcombo.addItem("gal"); // 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 buttonpanel 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 calculation are done 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("liter")) { number = Double.parseDouble(txtfield.getText()); mlTotal = number * 1000.00; galTotal = number / 3.785412; JOptionPane.showMessageDialog(null, formatter.format(mlTotal) + " mL\n" + formatter.format(galTotal) +" gallon\n"); number = 0.0; } if(select.equals("ml")) { number = Double.parseDouble(txtfield.getText()); literTotal = number / 1000.00; galTotal = literTotal / 3.785412; JOptionPane.showMessageDialog(null, formatter.format(literTotal) + " liter\n" + formatter2.format(galTotal) +" gallon\n"); number = 0.0; } if(select.equals("gal")) { number = Double.parseDouble(txtfield.getText()); literTotal = number * 3.785412; mlTotal = literTotal * 1000.00; JOptionPane.showMessageDialog(null, formatter.format(literTotal) + " liter\n" + formatter.format(mlTotal) +" ml\n"); 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) { } } } }