Advertisement
dcj123

TemperaturesFrame.java

Nov 6th, 2015
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.16 KB | None | 0 0
  1. import java.awt.FlowLayout;
  2. import java.awt.event.ActionListener;
  3. import java.awt.event.ActionEvent;
  4. import javax.swing.JLabel;
  5. import javax.swing.JFrame;
  6. import javax.swing.JButton;
  7. import javax.swing.Icon;
  8. import javax.swing.ImageIcon;
  9. import javax.swing.JOptionPane;
  10. public class TemperaturesFrame extends JFrame
  11. {
  12.     private JLabel header;
  13.     private JLabel Formulas;
  14.     private JLabel FtoC;
  15.     private JLabel CtoF;
  16.     private JButton FCButton;
  17.     private JButton CFButton;
  18.     public TemperaturesFrame()
  19.     {
  20.         super( "Temperature Conversion" );
  21.         setLayout( new FlowLayout() );
  22.  
  23.         header = new JLabel( "What are you converting?" );
  24.         header.setToolTipText( "header" );
  25.         add( header );
  26.  
  27.         FCButton = new JButton( "Fahrenheit to Celsius" );
  28.         add( FCButton );
  29.  
  30.         CFButton = new JButton( "Celsius to Fahrenheit" );
  31.         add( CFButton );
  32.  
  33.         Formulas = new JLabel( "    -  Formulas  -    " );
  34.         Formulas.setToolTipText( "Formulas" );
  35.         add( Formulas );
  36.  
  37.         FtoC = new JLabel( "Fahrenheit = Celsius * 5/9 + 32" );
  38.         FtoC.setToolTipText( "FtoC" );
  39.         add( FtoC );
  40.  
  41.         CtoF = new JLabel( "Celsius = (Fahrenheit - 32) * 5/9" );
  42.         CtoF.setToolTipText( "CtoF" );
  43.         add( CtoF );
  44.        
  45.         FCHandler handler1 = new FCHandler();
  46.         FCButton.addActionListener( handler1 );
  47.  
  48.         CFHandler handler2 = new CFHandler();
  49.         CFButton.addActionListener( handler2 );
  50.     }
  51.     private class FCHandler implements ActionListener
  52.     {
  53.         public void actionPerformed( ActionEvent event )
  54.         {
  55.         String fahrenheit =
  56.             JOptionPane.showInputDialog( "Enter Fahrenheit" );
  57.             int tempF = Integer.parseInt( fahrenheit );
  58.             int Celsius = (tempF - 32) * 5/9;
  59.  
  60.         JOptionPane.showMessageDialog( null, "The celsius temperature is " +Celsius,
  61.         "celsius", JOptionPane.PLAIN_MESSAGE );
  62.         }
  63.     }
  64.     private class CFHandler implements ActionListener
  65.     {
  66.         public void actionPerformed( ActionEvent event )
  67.         {
  68.         String celsius =
  69.             JOptionPane.showInputDialog( "Enter Celsius" );
  70.             int tempC = Integer.parseInt( celsius );
  71.             int Fahrenheit = tempC * 9 / 5 + 32;;
  72.  
  73.         JOptionPane.showMessageDialog( null, "The fahrenheit temperature is " +Fahrenheit,
  74.         "Fahrenheit", JOptionPane.PLAIN_MESSAGE );
  75.         }
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement