tutostudio1

CONTROLE - CONVERSOR DE TEMPERATURA

May 4th, 2015
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.19 KB | None | 0 0
  1. package conversor;
  2.  
  3. import java.awt.Button;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.awt.event.WindowEvent;
  7. import java.awt.event.WindowListener;
  8. import java.text.DecimalFormat;
  9.  
  10. public class Controle implements WindowListener, ActionListener {
  11.  
  12.     private Interface gi;
  13.     private Modelo mo;
  14.     private boolean erro;
  15.  
  16.     public Controle() {
  17.         gi = new Interface();
  18.         mo = new Modelo();
  19.         gi.setVisible(true);
  20.         gi.addWindowListener(this);
  21.         gi.setActionListener(this);
  22.  
  23.     }
  24.  
  25.     public static void main(String[] args) {
  26.         Controle con = new Controle();
  27.     }
  28.  
  29.     @Override
  30.     public void windowOpened(WindowEvent e) {
  31.     }
  32.  
  33.     @Override
  34.     public void windowClosing(WindowEvent e) {
  35.         gi.dispose();
  36.     }
  37.  
  38.     @Override
  39.     public void windowClosed(WindowEvent e) {
  40.     }
  41.  
  42.     @Override
  43.     public void windowIconified(WindowEvent e) {
  44.     }
  45.  
  46.     @Override
  47.     public void windowDeiconified(WindowEvent e) {
  48.     }
  49.  
  50.     @Override
  51.     public void windowActivated(WindowEvent e) {
  52.     }
  53.  
  54.     @Override
  55.     public void windowDeactivated(WindowEvent e) {
  56.     }
  57.  
  58.     @Override
  59.     public void actionPerformed(ActionEvent e) {
  60.         Button btnAcao = (Button) e.getSource();
  61.         Double val = string2Double(gi.getTxtTemperatura());
  62.         if (erro) {
  63.             gi.exibeErro();
  64.         } else {
  65.             if (btnAcao.getActionCommand().equals("C")) {
  66.                 mo.setCel(val);
  67.                 gi.exibeLabels("ºC", mo.getFah(), mo.getKel());
  68.             } else if (btnAcao.getActionCommand().equals("F")) {
  69.                 mo.setFah(val);
  70.                 gi.exibeLabels("ºF", mo.getCel(), mo.getKel());
  71.             } else if (btnAcao.getActionCommand().equals("K")) {
  72.                 mo.setKel(val);
  73.                 gi.exibeLabels("K", mo.getCel(), mo.getFah());
  74.             }
  75.         }
  76.  
  77.     }
  78.  
  79.     public double string2Double(String a) {
  80.         double res = 0;
  81.         try {
  82.             erro = false;
  83.             res = Double.parseDouble(a);
  84.         } catch (NumberFormatException e) {
  85.             erro = true;
  86.         }
  87.         return res;
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment