Advertisement
daniel199410

Número De Suerte

Feb 6th, 2013
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.41 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package javaapplication130;
  6. import java.awt.event.*;
  7. import javax.swing.*;
  8. /**
  9.  *
  10.  * @author Daniel
  11.  */
  12. public class NúmeroDeSuerte extends JFrame implements ActionListener, KeyListener{
  13.  
  14.     private JLabel label1, label2, label3, label4;
  15.     private JTextField texto3;
  16.     private JButton boton1;
  17.     private JComboBox combo1, combo2;
  18.    
  19.     public NúmeroDeSuerte(){
  20.         super("Número De La Suerte");
  21.         this.setLayout(null);
  22.         label1=new JLabel("Día:");
  23.         label1.setBounds(10,10,200,20);
  24.         add(label1);
  25.        
  26.         combo1=new JComboBox();
  27.         combo1.setBounds(40,10,50,20);
  28.         for(int i=1; i<=31; i++){
  29.             combo1.addItem(i);
  30.         }
  31.         add(combo1);
  32.        
  33.         label2=new JLabel("Mes:");
  34.         label2.setBounds(100,10,50,20);
  35.         add(label2);
  36.        
  37.         combo2=new JComboBox();
  38.         combo2.setBounds(140, 10, 90, 20);
  39.         String[] meses={"Enero", "Febero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre"};
  40.         for(int i=0; i<meses.length; i++){
  41.             combo2.addItem(meses[i]);
  42.         }
  43.         add(combo2);
  44.        
  45.         label3=new JLabel("Año:");
  46.         label3.setBounds(240, 10, 50, 20);
  47.         add(label3);
  48.        
  49.         texto3=new JTextField();
  50.         texto3.setBounds(280,10,50,20);
  51.         texto3.addKeyListener(this);
  52.         add(texto3);
  53.        
  54.         boton1=new JButton("Adivinar");
  55.         boton1.setBounds(30, 40, 100, 20);
  56.         boton1.addActionListener(this);
  57.         boton1.addKeyListener(this);
  58.         add(boton1);
  59.        
  60.         label4=new JLabel(" ");
  61.         label4.setBounds(140,40,200,20);
  62.         add(label4);
  63.     }
  64.    
  65.     public void numeroDeLaSuerte(){
  66.         try{
  67.             int dia=(Integer)combo1.getSelectedItem();
  68.             int mes=(Integer)combo2.getSelectedIndex()+1;
  69.             int año=Integer.parseInt(texto3.getText());
  70.             if(dia<=31 && dia>=1){
  71.                 int suma=dia+mes+año;
  72.                 int temp=0;
  73.                 while(suma !=0){
  74.                     temp+=suma%10;
  75.                     suma/=10;
  76.                 }
  77.  
  78.                 suma=temp;
  79.                 temp=0;
  80.  
  81.                 while(suma !=0){
  82.                     temp+=suma%10;
  83.                     suma/=10;
  84.                 }
  85.                 if(temp==10)
  86.                     temp=1;
  87.  
  88.                 label4.setText("Su Número De La Suerte Es: "+Integer.valueOf(temp));
  89.             }
  90.         }catch(Exception e){
  91.             label4.setText("Escriba Un Número, Idiota");
  92.         }
  93.     }
  94.    
  95.     public void actionPerformed(ActionEvent e){
  96.         if(e.getSource()==boton1){
  97.             numeroDeLaSuerte();
  98.         }
  99.     }
  100.     public void keyTyped(KeyEvent e) {
  101.  
  102.     }
  103.  
  104.     public void keyPressed(KeyEvent e) {
  105.         switch(e.getKeyCode()){
  106.             case KeyEvent.VK_ENTER:
  107.                 numeroDeLaSuerte();
  108.                 break;
  109.         }
  110.     }
  111.  
  112.     public void keyReleased(KeyEvent e) {
  113.  
  114.     }
  115.    
  116.     public static void main(String[] args) {
  117.         NúmeroDeSuerte f1=new NúmeroDeSuerte();
  118.         f1.setBounds(0,0,360,120);
  119.         f1.setLocation(700, 440);
  120.         f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  121.         f1.setVisible(true);
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement