Advertisement
tsnaik

AWT - Basic temperature conversion

Oct 22nd, 2014
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.36 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5. class MyFrame extends Frame
  6. {
  7.     public MyFrame(String title)
  8.     {
  9.         super(title);
  10.         setLayout(new GridLayout(2,2));
  11.     //setLocationRelativeTo(null);
  12.    
  13.     Dimension dim = getToolkit().getScreenSize();  
  14.     int screenWidth = dim.width;  
  15.     int screenHeight = dim.height;
  16.  
  17.     setLocation((screenWidth-100)/2,(screenHeight-170)/2);
  18.         setSize(170,100);
  19.         setVisible(true);
  20.         setResizable(false);
  21.  
  22.     Label lc = new Label("Celsius");
  23.         add(lc);
  24.  
  25.         TextField cel = new TextField(40);
  26.         add(cel);
  27.  
  28.         Label lf = new Label("Fahrenheit");
  29.         add(lf);
  30.  
  31.         TextField fah = new TextField(40);
  32.         add(fah);
  33.  
  34.         cel.addActionListener(new TempListener(cel,fah,this));
  35.         fah.addActionListener(new TempListener(cel,fah,this)); //Fahrenheit
  36.  
  37.         addWindowListener(new WindowClose());
  38.  
  39.  
  40.     }
  41. }
  42.  
  43. class TempListener extends Component implements ActionListener
  44. {
  45.     TextField fah,cel;
  46.     Frame f;
  47.     public TempListener(TextField cel, TextField fah,Frame f)
  48.     {
  49.         this.fah=fah;
  50.         this.f=f;
  51.         this.cel=cel;
  52.     }
  53.  
  54.     public void actionPerformed(ActionEvent ae)
  55.     {
  56.  
  57.         try
  58.         {
  59.             Object source = ae.getSource();
  60.             String text;
  61.             double originalValue;
  62.             double convertedValue;
  63.  
  64.             if(source==cel)
  65.             {
  66.                 text=cel.getText();
  67.                 originalValue = Double.parseDouble(text);
  68.                 convertedValue=((originalValue*(9.0/5.0)+32));
  69.                 fah.setText(" " + convertedValue);
  70.             }
  71.             else
  72.             {
  73.                 text=fah.getText();
  74.                 originalValue = Double.parseDouble(text);
  75.                 convertedValue = (originalValue-32)*(5.0/9.0);
  76.                 cel.setText(" "+ convertedValue);
  77.             }
  78.         }
  79.         catch(Exception e)
  80.     {
  81.         JOptionPane.showMessageDialog(f,"Error: " +e.getMessage() + "\n\nTry entering again", "Input error", JOptionPane.ERROR_MESSAGE);
  82.         cel.setText("");
  83.         fah.setText("");
  84.     }
  85.     }
  86. }
  87.  
  88. class WindowClose extends WindowAdapter
  89. {
  90.     public void windowClosing(WindowEvent we)
  91.     {
  92.         System.exit(0);
  93.     }
  94. }
  95.  
  96. public class Temperature
  97. {
  98.     public static void main(String args[])
  99.     {
  100.         Frame f = new MyFrame("C-F");
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement