brainfrz

Untitled

Oct 25th, 2015
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.65 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.util.Scanner;
  4. import javax.swing.*;
  5.  
  6. public class TempConverterGUI extends JFrame {
  7.     private TempConverter converter;
  8.  
  9.     public TempConverterGUI() {
  10.         converter = new TempConverter(TempConverter.TEMP_TYPE.Celsius);
  11.  
  12.         initComponents();
  13.     }
  14.  
  15.  
  16.  
  17.  
  18.     private void convertButtonActionPerformed(ActionEvent evt) {
  19.         getConvertedTemp();
  20.     }
  21.  
  22.     private void tempTextFieldMouseClicked(MouseEvent evt) {
  23.         tempTextField.setText("");
  24.     }
  25.  
  26.     private void tempTextFieldKeyReleased(KeyEvent evt) {
  27.         if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
  28.             getConvertedTemp();
  29.         }
  30.     }
  31.  
  32.     private void convertButtonKeyReleased(KeyEvent evt) {
  33.         if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
  34.             getConvertedTemp();
  35.         }
  36.     }
  37.  
  38.     private void tempsComboBoxItemStateChanged(ItemEvent evt) {
  39.         String tempStr = tempsComboBox.getSelectedItem().toString();
  40.  
  41.         tempTextField.setText("Temperature");
  42.         if (tempStr.equals("Convert Celsius to Fahrenheit")) {
  43.             converter = new TempConverter(TempConverter.TEMP_TYPE.Celsius);
  44.         }
  45.         else if (tempStr.equals("Convert Fahrenheit to Celsius")) {
  46.             converter = new TempConverter(TempConverter.TEMP_TYPE.Fahrenheit);
  47.         }
  48.         else {
  49.             throw new TempConverter.TempTypeDoesntExistException("Temperature type from combo box isn't supported.");
  50.         }
  51.  
  52.         convertFromLabel.setText(converter.fromTempTypeStr());
  53.         convertToLabel.setText(converter.toTempTypeStr());
  54.         tempTextField.setToolTipText("Enter a temperature in degrees " + converter.fromTempTypeStr());
  55.  
  56.     }
  57.  
  58.     private void getConvertedTemp() {
  59.         double fromTemp = 0.0;
  60.         int toTemp = 0;
  61.         boolean validInput = false;
  62.  
  63.         // Parse degrees Celsius asa double and convert to Fahrenheit.
  64.         try {
  65.             fromTemp = new Scanner(tempTextField.getText()).nextDouble();
  66.             validInput = true;
  67.         }
  68.         catch (java.util.InputMismatchException e) {
  69.             System.out.println("**ERROR**: Entered temperature isn't valid.");
  70.         }
  71.         catch (java.util.NoSuchElementException e) {
  72.             System.out.println("**ERROR**: No temperature entered.");
  73.         }
  74.  
  75.  
  76.         if (validInput) {
  77.             toTemp = converter.convertTemp(fromTemp);
  78.             convertToLabel.setText(toTemp + " " + converter.toTempTypeStr());
  79.         }
  80.         else {
  81.             convertToLabel.setText(converter.toTempTypeStr());
  82.         }
  83.  
  84.         pack();
  85.     }
  86.  
  87.  
  88.     /**
  89.      * This method is called from within the constructor to initialize the Netbeans form.
  90.      */
  91.     private void initComponents() {
  92.  
  93.         convertFromLabel = new JLabel();
  94.         convertToLabel = new JLabel();
  95.         convertButton = new JButton();
  96.         tempTextField = new JTextField();
  97.         tempsComboBox = new JComboBox();
  98.  
  99.         setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  100.         setTitle("Temperature Converter");
  101.  
  102.         convertFromLabel.setLabelFor(convertFromLabel);
  103.         convertFromLabel.setText("Celsius");
  104.  
  105.         convertToLabel.setText("Fahrenheit");
  106.  
  107.         convertButton.setText("Convert");
  108.         convertButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
  109.         convertButton.addActionListener(new ActionListener() {
  110.             public void actionPerformed(ActionEvent evt) {
  111.                 convertButtonActionPerformed(evt);
  112.             }
  113.         });
  114.         convertButton.addKeyListener(new KeyAdapter() {
  115.             public void keyReleased(KeyEvent evt) {
  116.                 convertButtonKeyReleased(evt);
  117.             }
  118.         });
  119.  
  120.         tempTextField.setText("Temperature");
  121.         tempTextField.setToolTipText("Enter a temperature in degrees Celsius");
  122.         tempTextField.addMouseListener(new MouseAdapter() {
  123.             public void mouseClicked(MouseEvent evt) {
  124.                 tempTextFieldMouseClicked(evt);
  125.             }
  126.         });
  127.         tempTextField.addKeyListener(new KeyAdapter() {
  128.             public void keyReleased(KeyEvent evt) {
  129.                 tempTextFieldKeyReleased(evt);
  130.             }
  131.         });
  132.  
  133.         tempsComboBox.setMaximumRowCount(2);
  134.         tempsComboBox.setModel(new DefaultComboBoxModel(new String[] { "Convert Celsius to Fahrenheit", "Convert Fahrenheit to Celsius" }));
  135.         tempsComboBox.addItemListener(new ItemListener() {
  136.             public void itemStateChanged(ItemEvent evt) {
  137.                 tempsComboBoxItemStateChanged(evt);
  138.             }
  139.         });
  140.  
  141.         GroupLayout layout = new GroupLayout(getContentPane());
  142.         getContentPane().setLayout(layout);
  143.         layout.setHorizontalGroup(
  144.             layout.createParallelGroup(GroupLayout.Alignment.LEADING)
  145.             .addGroup(layout.createSequentialGroup()
  146.                 .addContainerGap()
  147.                 .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
  148.                     .addGroup(layout.createSequentialGroup()
  149.                         .addComponent(convertButton)
  150.                         .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
  151.                         .addComponent(convertToLabel)
  152.                         .addGap(0, 0, Short.MAX_VALUE))
  153.                     .addGroup(layout.createSequentialGroup()
  154.                         .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
  155.                             .addGroup(layout.createSequentialGroup()
  156.                                 .addComponent(tempTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
  157.                                 .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
  158.                                 .addComponent(convertFromLabel))
  159.                             .addComponent(tempsComboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
  160.                         .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
  161.         );
  162.  
  163.         layout.linkSize(SwingConstants.HORIZONTAL, new Component[] {convertButton, tempTextField});
  164.  
  165.         layout.setVerticalGroup(
  166.             layout.createParallelGroup(GroupLayout.Alignment.LEADING)
  167.             .addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  168.                 .addContainerGap()
  169.                 .addComponent(tempsComboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
  170.                 .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
  171.                 .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
  172.                     .addComponent(tempTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
  173.                     .addComponent(convertFromLabel))
  174.                 .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
  175.                 .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
  176.                     .addComponent(convertButton)
  177.                     .addComponent(convertToLabel))
  178.                 .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  179.         );
  180.  
  181.         pack();
  182.     }
  183.  
  184.  
  185.     /**
  186.      * @param args Command-line arguments aren't currently supported
  187.      */
  188.     public static void main(String args[]) {
  189.         /* Set the Nimbus look and feel */
  190.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  191.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  192.          */
  193.         try {
  194.             for (UIManager.LookAndFeelInfo info
  195.                     : UIManager.getInstalledLookAndFeels())
  196.             {
  197.                 if ("Nimbus".equals(info.getName())) {
  198.                     UIManager.setLookAndFeel(info.getClassName());
  199.                     break;
  200.                 }
  201.             }
  202.         } catch (ClassNotFoundException | InstantiationException | IllegalAccessException
  203.                     | UnsupportedLookAndFeelException ex)
  204.         {
  205.             java.util.logging.Logger.getLogger(TempConverterGUI.class.getName())
  206.                                         .log(java.util.logging.Level.SEVERE, null, ex);
  207.         }
  208.  
  209.         /* Create and display the form */
  210.         EventQueue.invokeLater(() -> {
  211.             new TempConverterGUI().setVisible(true);
  212.         });
  213.     }
  214.  
  215.     // Variables declaration - do not modify
  216.     private JButton convertButton;
  217.     private JLabel convertFromLabel;
  218.     private JLabel convertToLabel;
  219.     private JTextField tempTextField;
  220.     private JComboBox tempsComboBox;
  221.     // End of variables declaration
  222. }
Advertisement
Add Comment
Please, Sign In to add comment