Advertisement
hercioneto

Projeto vidafumante

Nov 27th, 2023
858
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.33 KB | None | 0 0
  1. /** Criar o projeto vidafumante e copiar também a classe Cigarros, colar após o packpage
  2.  *
  3.  * @author Professor Hercio Neto
  4.  */
  5.  
  6. import java.awt.Dimension;
  7. import java.awt.GridLayout;
  8. import java.awt.event.ActionEvent;
  9. import java.awt.event.ActionListener;
  10. import java.text.DecimalFormat;
  11. import javax.swing.JButton;
  12. import javax.swing.JComboBox;
  13. import javax.swing.JDialog;
  14. import javax.swing.JFrame;
  15. import javax.swing.JLabel;
  16. import javax.swing.JPanel;
  17. import javax.swing.JTextField;
  18.  
  19. /**
  20.  *
  21.  * @author Professor
  22.  */
  23. public class Vidafumante {
  24.    
  25.      static void janelaValores(){
  26.         JFrame j = new JFrame("REDUÇÃO DE VIDA");
  27.         j.setSize(400,400);
  28.         j.setLocationRelativeTo(j);
  29.         j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  30.         JPanel p = new JPanel();
  31.        
  32.         GridLayout layout = new GridLayout(0,2,30,30);
  33.         p.setLayout(layout);
  34.        
  35.        
  36.        
  37.         JLabel jlbCigarros = new JLabel("Quantidade de cigarros:");      
  38.         jlbCigarros.setHorizontalAlignment(JLabel.RIGHT);
  39.         jlbCigarros.setHorizontalTextPosition(JLabel.RIGHT);
  40.        
  41.         JTextField jtxCigarros = new JTextField();
  42.         jtxCigarros.setPreferredSize(new Dimension( 50, 24 ));
  43.        
  44.        
  45.         JLabel jlbAnos = new JLabel("Anos fumando:");
  46.         jlbAnos.setHorizontalAlignment(JLabel.CENTER);
  47.         jlbAnos.setHorizontalTextPosition(JLabel.CENTER);
  48.         JTextField jtxAnos = new JTextField();
  49.         jtxAnos.setPreferredSize(new Dimension( 50, 24 ));
  50.        
  51.         JButton jbtCalcular=new JButton("Calcular");  
  52.         JButton jBtLimpar=new JButton("Limpar");  
  53.         jBtLimpar.setBounds(50,100,95,30);
  54.         jbtCalcular.setBounds(50,100,95,30);
  55.        
  56.         jbtCalcular.addActionListener(new ActionListener(){
  57.         public void actionPerformed(ActionEvent ae)
  58.               {
  59.         Cigarros x = new Cigarros();
  60.         Cigarros c = new Cigarros();
  61.         c.setAnos(Integer.parseInt(jtxAnos.getText()));
  62.         c.setQuantidade(Integer.parseInt(jtxCigarros.getText()));
  63.         c.setdiasPerdidos();
  64.         Double dias = c.getDiasPerdidos();
  65.         DecimalFormat formatter = new DecimalFormat("#0.00");    
  66.        
  67.         JDialog d = new JDialog(j, "TEMPO PERDIDO");
  68.         JLabel l = new JLabel("Redução de " + formatter.format(dias) + " dias de vida!");
  69.         l.setHorizontalAlignment(JLabel.CENTER);
  70.         l.setHorizontalTextPosition(JLabel.CENTER);
  71.         d.add(l);
  72.         d.setSize(200, 150);
  73.         d.setLocationRelativeTo(j);
  74.         d.setVisible(true);
  75.        
  76.                  
  77.                
  78.             }
  79.         });
  80.        
  81.        
  82.        
  83.        
  84.        
  85.         jBtLimpar.addActionListener(new ActionListener(){
  86.         public void actionPerformed(ActionEvent ae)
  87.               {
  88.                jtxCigarros.setText("");
  89.                jtxAnos.setText("");
  90.                
  91.                
  92.             }
  93.         });
  94.        
  95.             p.add(jlbCigarros);
  96.             p.add(jtxCigarros);
  97.             p.add(jlbAnos);
  98.             p.add(jtxAnos);
  99.            
  100.             p.add(jbtCalcular );
  101.             p.add(jBtLimpar);
  102.            
  103.         j.add(p);
  104.         j.pack();
  105.         j.setVisible(true);
  106.     }
  107.  
  108.     public static void main(String[] args) {
  109.         janelaValores();
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement