Inches to Centimeters Converter in Java

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 
import javax.swing.*;
 
public class MainFrame extends JFrame implements ActionListener {
 
      private final float CENTIMETERS_IN_INCH = 2.54f;
 
      JLabel inchLabel = new JLabel("Inches:");
      JLabel cmLabel = new JLabel("Centimeters: ");
      JTextField inchTextField = new JTextField(10);
      JTextField cmTextField = new JTextField(10);
      JButton inchToCmConvertButton = new JButton("Inch -> Cm");
      JButton cmToInchConvertButton = new JButton("Cm -> Inch");
 
      public MainFrame(String title) {
            super(title);
            setBounds(200, 200, 250, 130);          
 
            setLayout(new FlowLayout());
            add(inchLabel);
            add(inchTextField);
            add(cmLabel);
            add(cmTextField);
            add(inchToCmConvertButton);
            add(cmToInchConvertButton);
 
            inchToCmConvertButton.addActionListener(this);
            cmToInchConvertButton.addActionListener(this);
 
            setVisible(true);
      }
 
      public void actionPerformed(ActionEvent e) {
            if (e.getSource() == inchToCmConvertButton) {
                  float inch, cm;
                  try {
                        inch = Float.parseFloat(inchTextField.getText());
                        if (inch <= 0)
                             throw new NumberFormatException();
                        cm = inch * CENTIMETERS_IN_INCH;
                        cmTextField.setText(Float.toString(cm));
                  } catch (NumberFormatException ex) {
                        JOptionPane
                                   .showMessageDialog(null,
                                               "Inches value is invalid. It must be positive float number!");
                        inchTextField.setText("");
                        inchTextField.grabFocus();
                  }
            }
            if (e.getSource() == cmToInchConvertButton) {
                  float inch, cm;
                  try {
                        cm = Float.parseFloat(cmTextField.getText());
                        if (cm <= 0)
                             throw new NumberFormatException();
                        inch = cm / CENTIMETERS_IN_INCH;
                        inchTextField.setText(Float.toString(inch));
                  } catch (NumberFormatException ex) {
                        JOptionPane
                                   .showMessageDialog(null,
                                               "Centimeters value is invalid. It must be positive float number!");
                        cmTextField.setText("");
                        cmTextField.grabFocus();
                  }
            }
      }
 
      public static void main(String[] args) {
            JFrame mainFrame = new MainFrame("Inch2Cm Converter");
            mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      }
}

java homework help 2007-2015